Question: May you write python code for this? Make sure all the starter files are in your code folder. The expressiontree.py starter file contains comment instructions

May you write python code for this?

Make sure all the starter files are in your code folder. The "expressiontree.py" starter file contains comment instructions that tell you where to add your code to complete the expression tree LeafNode and InteriorNode classes. Do not change any of the other starter files.

To test your code, open and run the testExpressions.py script.

If you want to test other expressions, use the parserapp.py script. It allows you to type in an expression to test.

Make sure you read the "Guidance" document.

Make sure your output matches the "Expected Output" document.

Guidance document ================================= 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 division with integer 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. Python notes: In Python, the exponentiation operator is not "^". In Python, a single backslash character (\) must be represented as '\\'. In Python, the floating point division operator is /. In Python, the integer division operator is //.

Expected output:

Expression: 7 - 2 * 5 Prefix: - 7 * 2 5 Infix: (7 - (2 * 5)) Postfix: 7 2 5 * - Value: -3 Expression: (7 - 2) * 5 Prefix: * - 7 2 5 Infix: ((7 - 2) * 5) Postfix: 7 2 - 5 * Value: 25 Expression: 9 * 5 + 10 Prefix: + * 9 5 10 Infix: ((9 * 5) + 10) Postfix: 9 5 * 10 + Value: 55 Expression: 6 + 5 - 2 + 3 Prefix: + - + 6 5 2 3 Infix: (((6 + 5) - 2) + 3) Postfix: 6 5 + 2 - 3 + Value: 12 Expression: 5 * 7 ^ 2 Prefix: * 5 ^ 7 2 Infix: (5 * (7 ^ 2)) Postfix: 5 7 2 ^ * Value: 245 Expression: (4 * (2 + 3) ^ (2 + 3) Error: Parsing error -- ')' expected Expression so far = (4 * (2 + 3) ^ (2 + 3) Expression: 4 * (2 + 3) ^ (2 + 3) Prefix: * 4 ^ + 2 3 + 2 3 Infix: (4 * ((2 + 3) ^ (2 + 3))) Postfix: 4 2 3 + 2 3 + ^ * Value: 12500 Expression: 10 / 5 * 5 Prefix: * / 10 5 5 Infix: ((10 / 5) * 5) Postfix: 10 5 / 5 * Value: 10.0 Expression: 9 / 4 Prefix: / 9 4 Infix: (9 / 4) Postfix: 9 4 / Value: 2.25 Expression: 9 \ 4 Prefix: \ 9 4 Infix: (9 \ 4) Postfix: 9 4 \ Value: 2

Codes:

File: parserapp.py

View for the infix expression parser. Handles user interaction.

Source: Textbook chapter 10 Parsing and Expression Trees case study. Adapted by Derrf Seitz.

The following files must be in the same folder: expressiontree.py parsers.py scanner.py tokens.py """

from parsers import Parser

class ParserView(object):

def run(self): parser = Parser() while True: sourceStr = input("Enter an infix expression: ") if sourceStr == "": break try: tree = parser.parse(sourceStr) print("Prefix:", tree.prefix()) print("Infix:", tree.infix()) print("Postfix:", tree.postfix()) print("Value:", tree.value()) except Exception as e: print("Error:") print(e)

ParserView().run()

File: testExpressions.py

Test expression trees.

The following files must be in the same folder: expressiontree.py parsers.py scanner.py tokens.py """

from parsers import Parser

testList = [ "7 - 2 * 5", # -3 "(7 - 2) * 5", # 25 "9 * 5 + 10", # 55 "6 + 5 - 2 + 3", # 12 "5 * 7 ^ 2", # 245 "(4 * (2 + 3) ^ (2 + 3)", # Parsing error -- ')' expected "4 * (2 + 3) ^ (2 + 3)", # 12500 "10 / 5 * 5", # 10.0 "9 / 4", # 2.25 "9 \\ 4" # 2 ("\\" is a single \) ]

parser = Parser() for expression in testList: try: print("Expression:", expression) tree = parser.parse(expression) print("Prefix:", tree.prefix()) print("Infix:", tree.infix()) print("Postfix:", tree.postfix()) print("Value:", tree.value()) except Exception as e: print("Error:") print(e) print() # blank line to separate test cases

File: expressiontree.py

Expression tree nodes.

Source: Textbook chapter 10 Parsing and Expression Trees case study. Adapted by Derrf Seitz. """

#

# Replace with your name. # Replace any "" comments with your own code statement(s) # to accomplish the specified task. # DO NOT CHANGE ANY OTHER CODE.

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): # # Part 2: def prefix(self): #

def postfix(self): return str(self)

# Part 3: def value(self): #

class InteriorNode(object): """Represents an operator and its two operands."""

def __init__(self, op, leftOperand, rightOperand): self.operator = op self.leftOperand = leftOperand self.rightOperand = rightOperand

# Part 4: def infix(self): #

# Part 5: def prefix(self): # def postfix(self): return self.leftOperand.postfix() + " " + \ self.rightOperand.postfix() + " " + \ self.operator

# Part 6: def value(self): #

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!