예제 #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_mul_div_op(cls):
     try:
         if cls.iterator.has_next():
             cls.currentToken = cls.iterator.next()
             current_token_type = cls.currentToken.get_type()
             if current_token_type in [Token.MUL, Token.DIV]:
                 node = Node(Node.OP)
                 node.set_data_type(current_token_type)
                 return node
         raise ErrorParse(cls.get_next_token().get_line_num(), "multiple operation")
     except ErrorParse as e:
         print e.content
예제 #4
0
 def parse_logic_op(cls):
     try:
         if cls.iterator.has_next():
             cls.currentToken = cls.iterator.next()
             current_token_type = cls.currentToken.get_type()
             if current_token_type in [Token.GT, Token.GET, Token.LT, Token.LET, Token.EQ, Token.NEQ]:
                 node = Node(Node.OP)
                 node.set_data_type(current_token_type)
                 return node
         ErrorParse(cls.get_next_token().get_line_num(), "logical operation!")
     except ErrorParse as e:
         print e.content
예제 #5
0
 def parse_add_minus_op(cls):
     try:
         if cls.iterator.has_next():
             cls.currentToken = cls.iterator.next()
             current_token_type = cls.currentToken.get_type()
             if current_token_type in [Token.PLUS, Token.MINUS]:
                 node = Node(Node.OP)
                 node.set_data_type(current_token_type)
                 return node
         raise ErrorParse(cls.get_next_token().get_line_num(), "additive operation")
     except ErrorParse as e:
         print e.content
예제 #6
0
 def parse_add_minus_op(cls):
     try:
         if cls.iterator.has_next():
             cls.currentToken = cls.iterator.next()
             current_token_type = cls.currentToken.get_type()
             if current_token_type in [Token.PLUS, Token.MINUS]:
                 node = Node(Node.OP)
                 node.set_data_type(current_token_type)
                 return node
         raise ErrorParse(cls.get_next_token().get_line_num(), "additive operation")
     except ErrorParse as e:
         print e.content
예제 #7
0
 def parse_mul_div_op(cls):
     try:
         if cls.iterator.has_next():
             cls.currentToken = cls.iterator.next()
             current_token_type = cls.currentToken.get_type()
             if current_token_type in [Token.MUL, Token.DIV]:
                 node = Node(Node.OP)
                 node.set_data_type(current_token_type)
                 return node
         raise ErrorParse(cls.get_next_token().get_line_num(), "multiple operation")
     except ErrorParse as e:
         print e.content
예제 #8
0
 def parse_logic_op(cls):
     try:
         if cls.iterator.has_next():
             cls.currentToken = cls.iterator.next()
             current_token_type = cls.currentToken.get_type()
             if current_token_type in [Token.GT, Token.GET, Token.LT, Token.LET, Token.EQ, Token.NEQ]:
                 node = Node(Node.OP)
                 node.set_data_type(current_token_type)
                 return node
         ErrorParse(cls.get_next_token().get_line_num(), "logical operation!")
     except ErrorParse as e:
         print e.content
예제 #9
0
 def parse_literal(cls):
     try:
         if cls.iterator.has_next():
             cls.currentToken = cls.iterator.next()
             current_token_type = cls.currentToken.get_type()
             node = Node(Node.LITERAL)
             node.set_data_type(current_token_type)
             node.set_value(cls.currentToken.get_value())
             if current_token_type in [Token.LITERAL_INT, Token.LITERAL_DOU]:
                 return node
         raise ErrorParse(cls.get_next_token().get_line_num(), "literal")
     except ErrorParse as e:
         print e.content
예제 #10
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
예제 #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_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
예제 #13
0
 def parse_literal(cls):
     try:
         if cls.iterator.has_next():
             cls.currentToken = cls.iterator.next()
             current_token_type = cls.currentToken.get_type()
             node = Node(Node.LITERAL)
             node.set_data_type(current_token_type)
             node.set_value(cls.currentToken.get_value())
             if current_token_type in [Token.LITERAL_INT, Token.LITERAL_DOU]:
                 return node
         raise ErrorParse(cls.get_next_token().get_line_num(), "literal")
     except ErrorParse as e:
         print e.content
예제 #14
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
예제 #15
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
예제 #16
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
예제 #17
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
예제 #18
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