def expression(token): # Expressions, Grammer: term (op term)* print "<expression>" token = term(token) tmp_tkn = tokanizer.next_token_temp() t_type_o = op_checking(tmp_tkn[0]) if t_type_o == "op": token = tokanizer.next_token() while t_type_o == "op": print_token(token[0], token[1]) token = tokanizer.next_token() token = term(token) tmp_tkn = tokanizer.next_token_temp() t_type_o = op_checking(tmp_tkn) if t_type_o == "op": token = tokanizer.next_token() print "</expression>" return token
def expression_list(token): # Expression List, Grammer: (expression (',' expression)* )? print "<expressionList>" if token[0] != ")": token = expression(token) tmp_tkn = tokanizer.next_token_temp() if tmp_tkn[0] == ",": token = tokanizer.next_token() while token[0] == ",": print_token(token[0], token[1]) token = tokanizer.next_token() token = expression(token) token = tokanizer.next_token() print "</expressionList>" return token
def term(token): # term, Grammer: integerConstant | stringConstant | keywordConstant | varName | varName '[' expression ']' | subroutineCall | '(' expression ')' | unaryOp term print "<term>" if token[1] == "integerConstant" or token[1] == "stringConstant": print_token(token[0], token[1]) if token[1] == "keyword": print_token(token[0], token[1]) if token[1] == "identifier": tmp_token = tokanizer.next_token_temp() if tmp_token[0] != "." and tmp_token[0] != "(" and tmp_token[0] != "[": print_token(token[0], token[1]) if tmp_token[0] == "[": print_token(token[0], token[1]) token = tokanizer.next_token() if token[0] == "[": print_token(token[0], token[1]) token = tokanizer.next_token() token = expression(token) token = tokanizer.next_token() if token[0] == "]": print_token(token[0], token[1]) if tmp_token[0] == "(" or tmp_token[0] == ".": """ print_token(token[0], token[1]) token = tokanizer.next_token() print_token(token[0], token[1]) token = tokanizer.next_token()""" token = subroutine_call(token) t_type = keyword_constant_checking(token[0]) if t_type == "keywordConstant": print_token(token[0], token[1]) if token[0] == "(": print_token(token[0], token[1]) token = tokanizer.next_token() token = expression(token) token = tokanizer.next_token() if token[0] == ")": print_token(token[0], token[1]) if token[0] == "~" or token[0] == "-": print_token(token[0], "unaryOp") token = tokanizer.next_token() token = term(token) print "</term>" return token