Пример #1
0
 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
Пример #2
0
 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
Пример #3
0
 def parse_write_stmt(cls):
     try:
         node = Node(Node.WRITE_STMT)
         cls.read_token(Token.WRITE)
         node.set_left(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
Пример #4
0
 def parse_read_stmt(cls):
     try:
         node = Node(Node.READ_STMT)
         cls.read_token(Token.READ)
         node.set_left(cls.identifier())
         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
Пример #5
0
 def parse_read_stmt(cls):
     try:
         node = Node(Node.READ_STMT)
         cls.read_token(Token.READ)
         node.set_left(cls.identifier())
         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
Пример #6
0
 def parse_write_stmt(cls):
     try:
         node = Node(Node.WRITE_STMT)
         cls.read_token(Token.WRITE)
         node.set_left(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
Пример #7
0
 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
Пример #8
0
 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
Пример #9
0
 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
Пример #10
0
 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
Пример #11
0
 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
Пример #12
0
 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
Пример #13
0
 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
Пример #14
0
 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
Пример #15
0
 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
Пример #16
0
 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
Пример #17
0
 def identifier(cls):
     try:
         node = Node(Node.VAR)
         next_token_type = cls.get_next_token().get_type()
         if next_token_type == Token.ID:
             node.set_value(cls.iterator.next().get_value())
         else:
             raise ErrorParse(cls.get_next_token().get_line_num(), "identifier")
         if cls.get_next_token().get_type() == Token.LBRACKET:
             cls.read_token(Token.LBRACKET)
             node.set_left(cls.parse_exp())
             cls.read_token(Token.RBRACKET)
         return node
     except ErrorParse as e:
         print e.content
Пример #18
0
 def identifier(cls):
     try:
         node = Node(Node.VAR)
         next_token_type = cls.get_next_token().get_type()
         if next_token_type == Token.ID:
             node.set_value(cls.iterator.next().get_value())
         else:
             raise ErrorParse(cls.get_next_token().get_line_num(), "identifier")
         if cls.get_next_token().get_type() == Token.LBRACKET:
             cls.read_token(Token.LBRACKET)
             node.set_left(cls.parse_exp())
             cls.read_token(Token.RBRACKET)
         return node
     except ErrorParse as e:
         print e.content
Пример #19
0
 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
Пример #20
0
 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
Пример #21
0
 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
Пример #22
0
 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