classparseTree:defbuildParseTree(self,tokens):returnself.parseAssignment(tokens)defparseAssignment(self,tokens):foriinrange(len(tokens)):iftoken('operator','=')==tokens[i]:ifi==0:raiseExpressionError("Assignement is not done this way")returntree('=',tokens[:i][0],self.parseAssignment(tokens[i+1:]))returnself.parseParens(tokens)

defparseAdditive(self,tokens):i=1whilei<len(tokens):<br></len(tokens):<br>iftokens[i]in['+','-']:returntree(tokens[i].value,self.parseMultiplicative(tokens[:i]),self.parseAdditive(tokens[i+1:]))i+=1iflen(tokens)==2andtokens[0]in['+','-']:returntoken(tokens[1].type,"".join([tokens[0].value,tokens[1].value]))returnself.parseMultiplicative(tokens)defparseMultiplicative(self,tokens):i=0whilei\<len(tokens):iftokens[i].valuein['\*','/','%']:returntree(tokens[i].value,self.parsePower(tokens[:i]),self.parseMultiplicative(tokens[i+1:]))i+=1iflen(tokens) \>1:returnself.parsePower(tokens)ifnotlen(tokens):raiseExpressionError("Addition is not done this way")iftokens[0].typenotin['identifier','number','node']:raiseExpressionError("Operator + operator? Not good")returntokens[0]

defparsePower(self,tokens):i=0whilei\<len(tokens):iftokens[i]==token('operator','^'):returntree(tokens[i].value,self.parseFunctions(tokens[:i]),self.parseFunctions(tokens[i+1:]))i+=1iflen(tokens) \>1:returnself.parseFunctions(tokens)ifnotlen(tokens):raiseExpressionError("Multiplication is not done this way")iftokens[0].typenotin['identifier','number','node']:raiseExpressionError("Operator and operator? Not good")returntokens[0]defparseFunctions(self,tokens):i=0whilei \<len(tokens)-1:iftokens[i].type=='identifier'andtokens[i+1].typein['node','identifier','number']:returntree(tokens[i].value,tokens[i+1])i+=1iflen(tokens) \>1:raiseExpressionError("Bad function call")iftokens[0].type=='node':returntokens[0]returntokens[0].value