コード例 #1
0
 def __init__(self, path):
     self.path = path
     self.file = open(path, 'r')
     self.code = self.file.read() + '$$'
     self.file.close()
     self.lexer = Lex.Lexer(self.code)
     self.operands = Stack()
     self.operators = Stack()
     self.operators.push(Lex.Token('sentinel', 'sentinel'))
コード例 #2
0
    def parse_factor(self):
        pos_0 = self.lexer.pos
        tok = self.lexer.scan()

        if tok.token == "INT":
            self.operands.push(int(tok.att))
            return 1

        if tok.token == "ID":
            return 1

        if tok.att == "(":
            self.operators.push(Lex.Token('sentinel', 'sentinel'))
            if self.parse_expr():
                tok = self.lexer.scan()
                if tok.att == ")":
                    self.operators.pop()
                    return 1

        #reset position in text file on failure
        self.lexer.pos = pos_0
        return 0
コード例 #3
0
 def Unit(self, priority):
     if priority == 0:
         if self.current_token.type == LPAREN:
             self.eat(LPAREN)
             ans = self.Unit(Max_Priority)
             self.eat(RPAREN)
         elif self.current_token.type == NUM:
             ans = AST_Num(self.current_token.value)
             self.eat(NUM)
         elif self.current_token.type == STRING:
             ans = AST_String(self.current_token.value)
             self.eat(STRING)
         elif self.current_token.type in UnaryOp:
             token = self.current_token
             self.eat(token.type)
             ans = AST_UnaryOp(token, self.Unit(0))
         elif self.current_token.type == NAME:
             name = self.current_token.value
             self.eat(NAME)
             if self.current_token.type == LPAREN:
                 self.eat(LPAREN)
                 arglist = []
                 if self.current_token.type != RPAREN:
                     arglist.append(self.Unit(Max_Priority))
                     while self.current_token.type != RPAREN:
                         self.eat(COMMA)
                         arglist.append(self.Unit(Max_Priority))
                 self.eat(RPAREN)
                 ans = AST_FuncCall(name, arglist)
             else:
                 ans = AST_ID(name)
         elif self.current_token.type == LBRACK:
             self.eat(LBRACK)
             lst = []
             if self.current_token.type != RBRACK:
                 lst.append(self.Unit(Max_Priority))
                 while self.current_token.type == COMMA:
                     self.eat(COMMA)
                     lst.append(self.Unit(Max_Priority))
             self.eat(RBRACK)
             ans = AST_Array(lst)
         else:
             self.Error('invalid syntax at "%r" at pos %d' %
                        (self.lexer.get_local_text(), self.lexer.pos - 1l))
         while self.current_token.type == LBRACK:
             self.eat(LBRACK)
             ind = self.Unit(Max_Priority)
             self.eat(RBRACK)
             ans = AST_BinOp(Lexer.Token(INDEX, '[]'), ans, ind)
     elif Associativity[priority] == LeftAssoc:
         ans = self.Unit(priority - 1)
         while self.current_token.type in Prio and Prio[
                 self.current_token.type] == priority:
             token = self.current_token
             self.eat(token.type)
             ans = AST_BinOp(token, ans, self.Unit(priority - 1))
     else:
         ans = self.Unit(priority - 1)
         rightest_node = ans
         first = True
         while self.current_token.type in Prio and Prio[
                 self.current_token.type] == priority:
             token = self.current_token
             self.eat(token.type)
             if first:
                 ans = AST_BinOp(token, ans, self.Unit(priority - 1))
                 rightest_node = ans
                 first = False
             else:
                 rightest_node.rson = AST_BinOp(token, rightest_node.rson,
                                                self.Unit(priority - 1))
                 rightest_node = rightest_node.rson
     return ans