Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Design, write and test a program that accepts an arithmetic expression of unsigned integers in postfix notation in which the tokens are separated by spaces

Design, write and test a program that accepts an arithmetic expression of unsigned integers in postfix notation in which the tokens are separated by spaces and builds and processes the arithmetic expression tree that represents that expression. The main class will be P2GUI. It should create a Swing based GUI shown below:

image text in transcribed

The GUI must be generated by code that you write. You may not use a drag-and-drop GUI generator.

Pressing the Construct Tree button should cause the following:

(i)the arithmetic expression tree that represents the entered postfix expression will be constructed

(ii) using that tree, the corresponding fully parenthesized

infix expression should be generated and displayed in the GUI and finally

(iii) a file should be generated that contains the 3-address format instructions corresponding to the arithmetic expression.

These topics, including relevant classes for implementing and processing an expression tree, are discussed in the week 4 reading Binary Trees, Expression Trees, BST (AVL) trees, and B-Trees section II Expression Trees.

The above example should produce the following output file containing the3-address instructions:

Add R0 5 9

Sub R1 3 R0

Mul R2 2 3

Div R3 R1 R2

It is not necessary to reuse registers within an expression as shown in the above mentioned reading and you can assume there are as many available registers as needed. Each new expression should, however, begin using registers starting at R0.

You may assume that the expression is syntactically correct with regard to the order of operators and operands, but you should check for invalid tokens, such as characters that are not valid operators or operands such as 2a, which are not valid integers. If an invalid token is detected, a checked custom exception "InvalidTokenException" should be thrown and caught by the main class and an appropriate error message should be displayed in an "JOptionPane". Below is an example:

image text in transcribed

Your program should compile without errors.

For building the expression tree from the postfix expression you have to:

1. Define a Stack of type Node, i.e. Stack

2. Tokenize the postfixExpression (use the space character " " as separator)

3. Read next token

3.1 If the token is a numeric value => generate an OperandNode and push it on the stack

3.2 If the token is an operator => generate an OperatorNode. An OperatorNode is defined by a reference to Operator (i.e. AddOperator, SubOperator, etc.) and two references to the left and right subtrees of the OperatorNode. The references to the left and right subtrees should be obtained by pop-ing two Nodes from the Stack. Push the reference to the generated OperatorNode on the Stack

4. Repeat step 3 for all tokens of the postfixExprssion.

5. At the end the stack will contain only one Node(a reference to that node) which is the root of the expression tree corresponding to the postfix expression given as parameter.

Three Adddress Generator Enter Postfix Expression 359+-23 Construct Tree Infix Expression ((3 (5+9))/ (2 3))

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

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions

Question

Explain Coulomb's law with an example

Answered: 1 week ago

Question

What is operating system?

Answered: 1 week ago

Question

What is Ohm's law and also tell about Snell's law?

Answered: 1 week ago