def term(self): """ term ::= [atom/*factor] """ lbranch = self.atom() t = Operator('') t.add_branch(lbranch) # flag == True ==> We don't wrap tree as term. Keep it atom. flag = True # onetime == True ==> Every iteration in while() we build new tree, where old tree = left branch of it. onetime = False while self.symbol.value in ['/', '*']: flag = False tt = t if onetime: tt = Operator('') tt.add_branch(t) tt.mark = self.symbol.value if self.symbol.value == '/': rbranch = self.div() tt.name = '/' if self.symbol.value == '*': rbranch = self.mul() tt.name = '*' tt.add_branch(rbranch) onetime = True t = tt if (flag): t = lbranch return t
def term(self): """ term ::= [atom/*factor] """ lbranch = self.atom() t = Operator('') t.add_branch(lbranch) # flag == True ==> We don't wrap tree as term. Keep it atom. flag = True # onetime == True ==> Every iteration in while() we build new tree, where old tree = left branch of it. onetime = False while self.symbol.value in ['/', '*']: flag = False tt = t if onetime: tt = Operator('') tt.add_branch(t) tt.mark = self.symbol.value if self.symbol.value == '/': rbranch = self.div() tt.name = '/' if self.symbol.value == '*': rbranch = self.mul() tt.name = '*' tt.add_branch(rbranch) onetime = True t = tt if(flag): t = lbranch return t
def expression(self): """ expression ::= [term+-term] """ t = Operator('') sign = 1 # I have no good idea for unary [+/-] if self.symbol.value == '+': # ignore this operator #t.add_branch(Tree(self.symbol)) self.next() if self.symbol.value == '-': t.add_branch(Node(self.symbol.value, self.symbol.type)) self.next() sign = -1 lbranch = self.term() t.add_branch(lbranch) flag = True onetime = False while self.symbol.value in ['+', '-']: flag = False if (onetime): tt = Operator('') tt.add_branch(t) else: tt = Operator('') tt.add_branch(lbranch) symbol = self.symbol if self.symbol.value == '+': rbranch = self.add() tt.name = '+' if self.symbol.value == '-': rbranch = self.sub() tt.name = '-' tt.mark = symbol.value tt.add_branch(rbranch) t = tt onetime = True if flag: t = lbranch return t
def expression(self): """ expression ::= [term+-term] """ t = Operator( '') sign = 1 # I have no good idea for unary [+/-] if self.symbol.value == '+' : # ignore this operator #t.add_branch(Tree(self.symbol)) self.next() if self.symbol.value == '-': t.add_branch(Node(self.symbol.value, self.symbol.type)) self.next() sign = -1 lbranch = self.term() t.add_branch(lbranch) flag = True onetime = False while self.symbol.value in ['+', '-']: flag = False if(onetime): tt = Operator('') tt.add_branch(t) else: tt = Operator('') tt.add_branch(lbranch) symbol = self.symbol if self.symbol.value =='+': rbranch = self.add() tt.name = '+' if self.symbol.value =='-': rbranch = self.sub() tt.name = '-' tt.mark = symbol.value tt.add_branch(rbranch) t = tt onetime = True if flag: t = lbranch return t