Question
I am looking for help with the following code. Please see below for directions and the py files attached. The expressiontree.py file, part 1-6 is
I am looking for help with the following code. Please see below for directions and the py files attached. The expressiontree.py file, part 1-6 is where i need to write the code and i have attempted it but i am getting errors and it is not adding up the value like it is suppose to. Thank you for any help you can provide. I also have a token.py, scanner.py, parser.py, and parserapp.py file that runs with this and can provide if you need them. Thank you.
Directions:
Part 1 ------ Note that the LeafNode class already shows the solution for its postfix() method. postfix() simply returns a string version of itself. To do this, it calls str on itself. str calls __str__, which returns the data field converted to a string. The LeafNode infix() method does the same things as postfix(). Part 2 ------ The LeafNode prefix() method does the same things as postfix(). Part 3 ------ The LeafNode value() method simply returns the data field. Make sure to refer to this field using the provided "self" object reference. Part 4 ------ Note that the InteriorNode class already shows the solution for its postfix() method. postfix() returns a string of the form: postfix of its left operand a space postfix of its right operand a space its operator Note that this pattern results in a recursive descent of the expression tree. The InteriorNode infix() method should use the following similar pattern: a left parenthesis "(" infix of its left operand a space its operator a space infix of its right operand a right parenthesis ")" Part 5 ------ The InteriorNode prefix() method should use the following pattern: its operator a space prefix of its left operand a space prefix of its right operand Part 6 ------ The InteriorNode value() method should use the following steps: Use self.leftOperand.value() to get the value of its left operand. Use self.rightOperand.value() to get the value of its right operand. self.operator contains the mathematical operator as a single character. Use self.operator in conditional logic to calculate the value to return. The valid operators are: + for addition - for subtraction * for multiplication / for division with floating point quotient ^ for exponentiation Example: If the operator is '+' (addition), the returned value should be: value of left operand + value of right operand If the operator does not equal a valid value, return 0.
expressiontree.py: code to write in parts 1-6
class LeafNode(object): """Represents an integer."""
def __init__(self, data): self._data = data
def __str__(self): return str(self._data)
# Part 1: def infix(self): return str(self) # Part 2: def prefix(self): return str(self)
def postfix(self): return str(self)
# Part 3: def value(self): return self._data
class InteriorNode(object): """Represents an operator and its two operands."""
def __init__(self, op, leftOper, rightOper): self._operator = op self._leftOperand = leftOper self._rightOperand = rightOper
# Part 4: def infix(self): return "(" + self._leftOperand.infix() + " " +\ self._operator + " " + \ self._rightOperand.infix() + ")"
# Part 5: def prefix(self): return self._operator + " " + \ self._leftOperand.prefix() + " " + \ self._rightOperand.prefix() def postfix(self): return self._leftOperand.postfix() + " " + \ self._rightOperand.postfix() + " " + \ self._operator
# Part 6: def value(self): return(self._leftOperand.value(), self._rightOperand.value()) if self._operator() == "+": return leftOper + rightOper elif self._operator() == "-": return leftOper - rightOper elif self._operator() == "*": return leftOper * rightOper elif self._Operator() == "/": return leftOper / rightOper elif self._operator() == "^": return leftOper ** rightOper else: return 0
# A simple tester program: def main(): a = LeafNode(4) b = InteriorNode('+', LeafNode(2), LeafNode(3)) c = InteriorNode('*', a, b) c = InteriorNode('^', c, b) print("Expect ((4 * (2 + 3) ^ (2 + 3)):", c.infix()) print("Expect ^ * 4 + 2 3 + 2 3 :", c.prefix()) print("Expect 4 2 3 + * 2 3 + ^ :", c.postfix()) print("Expect 3200000 :", c.value())
if __name__ == "__main__": main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started