Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# This program exercises expressiontrees. # Replace any comments with your own code statement(s) # to accomplish the specified task. # Do not

# This program exercises expressiontrees.

# Replace any "" comments with your own code statement(s) # to accomplish the specified task. # Do not change any other code.

# The following files must be in the same folder: # tokens.py # parserapps.py # scanner.py # parsers.py

expressiontree.py

""" Expression tree nodes.

Source: Textbook chapter 10 Parsing and Expression Trees case study student files. """

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, leftOper, rightOper): self._operator = op self._leftOperand = leftOper self._rightOperand = rightOper

# 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): #

# 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()

***OUTPUT**

Enter an infix expression: 7 - 2 * 5 Prefix: - 7 * 2 5 Infix: (7 - (2 * 5)) Postfix: 7 2 5 * - Value: -3 Enter an infix expression: (7 - 2) * 5 Prefix: * - 7 2 5 Infix: ((7 - 2) * 5) Postfix: 7 2 - 5 * Value: 25 Enter an infix expression: 9 * 5 + 10 Prefix: + * 9 5 10 Infix: ((9 * 5) + 10) Postfix: 9 5 * 10 + Value: 55 Enter an infix expression: 6 + 5 - 2 + 3 Prefix: + - + 6 5 2 3 Infix: (((6 + 5) - 2) + 3) Postfix: 6 5 + 2 - 3 + Value: 12 Enter an infix expression: 5 * 7 ^ 2 Prefix: * 5 ^ 7 2 Infix: (5 * (7 ^ 2)) Postfix: 5 7 2 ^ * Value: 245 Enter an infix expression: (4 * (2 + 3) ^ (2 + 3) Error: Parsing error -- ')' expected Expression so far = (4 * (2 + 3) ^ (2 + 3) Enter an infix 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 Enter an infix expression: >>>

***PLEASE ASSIST ME WITH THE CODES NEEDED IN BOLD. IN ORDER TO RUN THE PROGRAM, YOU HAVE TO RUN THE PARSERAPP.PY FILE AFTER INPUTTING THE CODES. THE OUTPUT IS POSTED ALONG WITH THIS. PLEASE MAKE SURE THAT EVERYTHING IS THERE. THANKS! I WILL RATE YOU!***

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

More Books

Students also viewed these Databases questions