def parse_declare_stmt(cls): try: node = Node(Node.DECLARE_STMT) var_node = Node(Node.VAR) if cls.get_next_token().get_type() in [Token.INT, Token.DOUBLE]: current_token = cls.iterator.next() data_type = Token.INT if current_token.get_type() == Token.INT else Token.DOUBLE var_node.set_data_type(data_type) else: next_token = cls.get_next_token() raise ErrorParse(next_token.get_line_num(), next_token.get_type()) if cls.get_next_token().get_type() == Token.ID: current_token = cls.iterator.next() var_node.set_value(current_token.get_value()) else: next_token = cls.get_next_token() raise ErrorParse(next_token.get_line_num(), next_token.get_type()) if cls.get_next_token().get_type() == Token.ASSIGN: cls.read_token(Token.ASSIGN) node.set_middle(cls.parse_exp()) elif cls.get_next_token().get_type() == Token.LBRACKET: cls.read_token(Token.LBRACKET) var_node.set_left(cls.parse_exp()) cls.read_token(Token.RBRACKET) cls.read_token(Token.SEMI) node.set_left(var_node) return node except ErrorParse(cls.get_next_token().get_line_num(), cls.get_next_token().get_type()) as e: print e.content
def parse_assign_stmt(cls): try: node = Node(Node.ASSIGN_STMT) node.set_left(cls.identifier()) cls.read_token(Token.ASSIGN) node.set_middle(cls.parse_exp()) cls.read_token(Token.SEMI) return node except ErrorParse(cls.get_next_token().get_line_num(), cls.get_next_token().get_type()) as e: print e.content
def parse_while_stmt(cls): try: node = Node(Node.WHILE_STMT) cls.read_token(Token.WHILE) cls.read_token(Token.LPARENT) node.set_left(cls.parse_exp()) cls.read_token(Token.RPARENT) node.set_middle(cls.switch_stmt()) return node except ErrorParse(cls.get_next_token().get_line_num(), cls.get_next_token().get_type()) as e: print e.content
def parse_term_exp(cls): try: node = Node(Node.EXP) node.set_data_type(Token.TERM_EXP) left_node = cls.parse_factor() if cls.get_next_token().get_type() in [Token.MUL, Token.DIV]: node.set_left(left_node) node.set_middle(cls.parse_mul_div_op()) node.set_right(cls.parse_term_exp()) return node return left_node except ErrorParse(cls.get_next_token().get_line_num(), cls.get_next_token().get_type()) as e: print e.content
def parse_exp(cls): try: node = Node(Node.EXP) node.set_data_type(Token.LOGIC_EXP) left_node = cls.parse_multi_term_exp() if cls.get_next_token().get_type() in [Token.GT, Token.GET, Token.LT, Token.LET, Token.EQ, Token.NEQ]: node.set_left(left_node) node.set_middle(cls.parse_logic_op()) node.set_right(cls.parse_multi_term_exp()) return node return left_node except ErrorParse(cls.get_next_token().get_line_num(), cls.get_next_token().get_type()) as e: print e.content
def parse_if_stmt(cls): try: node = Node(Node.IF_STMT) cls.read_token(Token.IF) cls.read_token(Token.LPARENT) node.set_left(cls.parse_exp()) cls.read_token(Token.RPARENT) node.set_middle(cls.switch_stmt()) if cls.iterator.has_next() and cls.get_next_token().get_type() == Token.ELSE: cls.read_token(Token.ELSE) node.set_right(cls.switch_stmt()) return node except ErrorParse(cls.get_next_token().get_line_num(), cls.get_next_token().get_type()) as e: print e.content
def parse_multi_term_exp(cls): try: node = Node(Node.EXP) node.set_data_type(Token.MULTI_TERM_EXP) left_node = cls.parse_term_exp() next_token_type = cls.get_next_token().get_type() if next_token_type == Token.PLUS: node.set_left(left_node) node.set_middle(cls.parse_add_minus_op()) node.set_right(cls.parse_multi_term_exp()) elif next_token_type == Token.MINUS: node.set_left(left_node) node.set_middle(Node(Node.OP, m_data_type=Token.PLUS)) node.set_right(cls.parse_multi_term_exp()) else: return left_node return node except ErrorParse(cls.get_next_token().get_line_num(), cls.get_next_token().get_type()) as e: print e.content
def parse_factor(cls): try: if cls.iterator.has_next(): node = Node(Node.FACTOR) next_token_type = cls.get_next_token().get_type() if next_token_type in [Token.LITERAL_INT, Token.LITERAL_DOU]: node.set_left(cls.parse_literal()) elif next_token_type == Token.LPARENT: cls.read_token(Token.LBRACKET) node.set_middle(cls.parse_exp()) cls.read_token(Token.RPARENT) elif next_token_type == Token.MINUS: node.set_data_type(Token.MINUS) cls.currentToken = cls.iterator.next() node.set_left(cls.parse_term_exp()) elif next_token_type == Token.PLUS: cls.currentToken == cls.iterator.next() node.set_left(cls.parse_term_exp()) elif next_token_type == Token.ID: return cls.identifier() return node raise ErrorParse(cls.get_next_token().get_line_num(), "factor") except ErrorParse as e: print e.content