Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Description: project involves writing a program that accepts an arithmetic expression of unsigned integers in postfix notation and builds the arithmetic expression tree that represents

Description: project involves writing a program that accepts an arithmetic expression of unsigned integers in postfix notation and builds the arithmetic expression tree that represents that expression. From that tree, the corresponding fully parenthesized infix expression should be displayed and a file should be generated that contains the three address format instructions

--------------------------------------------------------------------------------------------------------

Can Someone Please repair my code.. I have received the following feedback from my professor...

This solution is good, but is neither complete (no three address instruction generated) nor fully correct (some expression types incorrect handled examples below).

Documentation contains the mandatory sections.

Test cases does not discuss the instruction format (should have been eat least proposed, to have the overview of the expected output in the envisioned cases).

Wrong evaluations:

The postfix expression:

23+4-5*6/

Should be the infix:

(((2+3)-4)*5)/6

And you generate:

((null*((null-((null+(2+3)-4))*5/6)

And the instruction set (completely missing):

ADD R0 2 3

SUB R1 R0 4

MUL R2 R1 5

DIV R3 R2 6

Also, for:

23+4-5*6/78+9*/

Should be the infix:

(((2+3)-4)*5)/6 / (7+8)*9

And you have: (null/(((null*((null-((null+(2+3))-4))*5))/6)+(7

And the instruction set (also missing):

ADD R0 2 3

SUB R1 R0 4

MUL R2 R1 5

DIV R3 R2 6

ADD R4 7 8

MUL R5 R4 9

DIV R6 R3 R5

--------------------------------------------------------------------------------------------------

Code Below:

------------PostfixtoInfix.java---------------------------------------

//package PExpression; import javax.swing.*; import java.awt.*; import java.awt.event.*;

public class PostfixToInfix extends JFrame {

private JButton construct; private JTextField input; private JLabel Label; private JLabel Label2; private JLabel output; public PostfixToInfix () { GUI(); }

void GUI() {

input = new JTextField(); Label = new JLabel(); construct = new JButton(); Label2 = new JLabel(); output = new JLabel();

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setTitle("Three address generator");

Label.setText("Enter postfix Expression");

construct.setText("Construct Tree"); construct.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { constructActionPerformed(evt); } });

Label2.setText("Infix Expression ");

GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(230, 230, 230) .addComponent(construct)) .addGroup(layout.createSequentialGroup() .addGap(17, 17, 17) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(Label, GroupLayout.PREFERRED_SIZE, 172, GroupLayout.PREFERRED_SIZE) .addComponent(Label2)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(input, GroupLayout.PREFERRED_SIZE, 377, GroupLayout.PREFERRED_SIZE) .addComponent(output, GroupLayout.PREFERRED_SIZE, 358, GroupLayout.PREFERRED_SIZE)))) .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(input, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(Label)) .addGap(18, 18, 18) .addComponent(construct) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(Label2) .addComponent(output)) .addGap(18, 18, 18)) );

pack(); }

private void constructActionPerformed(ActionEvent evt) { try { String data=input.getText(); ConstructTree t=new ConstructTree(data); output.setText(t.toString()); } catch (Exception e) { JOptionPane.showMessageDialog(null,e.getMessage(),"Please enter Postfix Expression",1); } }

public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { new PostfixToInfix().setVisible(true); } }); }

}

---------------------------------------------------------

-------------------Operator.java-------------------------- public class Operator extends Node{ public Operator(char data) { this.value=data; this.left=null; this.right=null; }

public Node getLeft() { return left; }

public void setLeft(Node left) { this.left = left; }

public Node getRight() { return right; }

public void setRight(Node right) { this.right = right; } @Override public String toString() { return "( "+this.left+" "+value+" "+this.right+" )"; } }

-------------------------------------------------------------

------------Operand.java-----------------

public class Operand extends Node{ public Operand(char data) { this.value=data; this.left=null; this.right=null; } @Override public String toString() { return value+""; } }

----------------------------------------------------------------

----------------Node.java------------------

//package PExpression;

ss Node { char value; Node left; Node right;

public char getValue() { return value; }

public void setValue(char value) { this.value = value; } }

---------------------------------------------------

-------------------ConstructTree.java------------------------ public class ConstructTree { Node root; boolean added=false; public ConstructTree(String data) throws Exception { for(int i=data.length()-1;i>=0;i--) { added=false; root=addNode(root, data.charAt(i)); } } private Node addNode(Node node,char data) throws Exception { if(node!=null) { if(node instanceof Operator) { if(node.right==null||node.right instanceof Operator) { node.right=addNode(node.right, data); } else if(!added&&node.right!=null&&node.right instanceof Operand) { if(node.left==null||node.left instanceof Operand) { node.left=addNode(node.left, data); } } if(!added&&node.left==null||node.left instanceof Operator) { node.left=addNode(node.left, data); } } } else { added=true; if(isOperator(data)) { return new Operator(data); } else { return new Operand(data); } } return node; } private boolean isOperator(char c) throws Exception{ if(c == '+' || c == '-' || c == '*' || c =='/' || c == '^') return true; if(c>='0'&&c<='9') return false; throw new Exception("Invalid token "+c); } @Override public String toString() { return root.toString(); } }

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

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

More Books

Students also viewed these Databases questions

Question

Select suitable tools to analyze service problems.

Answered: 1 week ago