def Factor(level): if tokenNow.tokenType == TokenType.PLUS or tokenNow.tokenType == TokenType.MINUS: root = ExpNode(tokenNow) MatchToken(tokenNow.tokenType) son = Factor(level + 1) root.addson(son) return root else: return Component(level + 1)
def Atom(level): #Msg(level, "Atom", 0) if tokenNow.tokenType == TokenType.CONST_ID or tokenNow.tokenType == TokenType.T: root = ExpNode(tokenNow) MatchToken(tokenNow.tokenType) #Msg(level, "Atom") return root elif tokenNow.tokenType == TokenType.FUNC: root = ExpNode(tokenNow) MatchToken(tokenNow.tokenType) MatchToken(TokenType.L_BRACKET) son = Expression(level + 1) MatchToken(TokenType.R_BRACKET) root.addson(son) #Msg(level, "Atom") return root elif tokenNow.tokenType == TokenType.L_BRACKET: MatchToken(TokenType.L_BRACKET) root = Expression(level + 1) MatchToken(TokenType.R_BRACKET) #Msg(level, "Atom") return root else: print("Atom Error!")
def Term(level): left = Factor(level) root = None while tokenNow.tokenType == TokenType.MUL or tokenNow.tokenType == TokenType.DIV: root = ExpNode(tokenNow) MatchToken(tokenNow.tokenType) right = Factor(level + 1) root.addson(left) root.addson(right) left = root return left
def Component(level): left = Atom(level) if tokenNow.tokenType == TokenType.POWER: root = ExpNode(tokenNow) MatchToken(tokenNow.tokenType) right = Component(level + 1) root.addson(left) root.addson(right) return root else: return left
def Expression(level): left = Term(level + 1) root = None while tokenNow.tokenType == TokenType.PLUS or tokenNow.tokenType == TokenType.MINUS: root = ExpNode(tokenNow) MatchToken(tokenNow.tokenType) right = Term(level + 1) root.addson(left) root.addson(right) left = root # left.dfs() print(left.dfs()) return left