Пример #1
0
    def term(self):
        r = None
        n = self.factor()

        if self.tkr.cur() in ['*', '/']:
            r = Node()
            r.val = self.tkr.get()
            if r.val == '*':
                r.type = 'mul'
            else:
                r.type = 'div'
            r.rhs = n
            r.lhs = self.term()
        else:
            r = n
        return r
Пример #2
0
    def expr(self):
        r = None
        n = self.term()

        if self.tkr.cur() in ['+', '-']:
            r = Node()
            r.val = self.tkr.get()
            if r.val == '+':
                r.type = 'add'
            else:
                r.type = 'sub'
            r.rhs = n
            r.lhs = self.expr()
        else:
            r = n
        return r
Пример #3
0
    def factor(self):
        n = None

        if self.tkr.cur() == '(':
            self.tkr.get()
            n = self.expr()
            self.tkr.get()
        else:
            n = Node()
            n.val = int(self.tkr.get())
            n.type = 'number'

        return n