Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//Sorry I didnt include the code, it is to be coded in Java as well. I was unable to find the proper postfix method in

image text in transcribed

//Sorry I didnt include the code, it is to be coded in Java as well. I was unable to find the proper postfix method in my book.

import java.util.*;

public class InfixToPostfixParens { private static final String OPERATORS = "-+*/()"; private static final int[] PRECEDENCE = {1, 1, 2, 2, -1, -1}; private static final String PATTERN = "\\d+\\.\\d*|\\d+|" + "\\p{L}[\\p{L}\\p{N}]*" + "|[" + OPERATORS + "]"; private final Deque operatorStack = new ArrayDeque(); private final StringJoiner postfix = new StringJoiner(" ");

public static String convert(String infix) throws SyntaxErrorException { InfixToPostfixParens infixToPostfixParens = new InfixToPostfixParens(); infixToPostfixParens.convertToPostfix(infix); return infixToPostfixParens.getPostfix(); }

private String getPostfix() { return postfix.toString(); }

private void convertToPostfix(String infix) throws SyntaxErrorException { try { String nextToken; Scanner scan = new Scanner(infix); while ((nextToken = scan.findInLine(pattern)) != null) { char firstChar = nextToken.charAt(0); if (Character.isLetter(firstChar) || Character.isDigit(firstChar)) { postfix.add(nextToken); } else if(isOperator(firstChar)) { processOperator(firstChar); } else { throw new SyntaxErrorException ("Unexpected Character Encountered: " + firstChar); } } while (!operatorStack.isEmpty()) { char op = operatorStack.pop(); if (op == '(') throw new SyntaxErrorException ("Unmatched opening parenthesis"); postfix.add(new Character(op).toString()); } return postfix.toString(); } catch (NoSuchElementException ex) { throw new SyntaxErrorException("Syntax Error: The stack is empty"); } } private void processOperator(char op) { if (operatorStack.isEmpty() || op == '(') { operatorStack.push(op); } else { char topOp = operatorStack.peek(); if (precedence(op) > precedence(topOp)) { operatorStack.push(op); } else { while (!operatorStack.isEmpty() && precedence(op) // top of stack operator precedence. if (op != ')') operatorStack.push(op); } } } public static class SyntaxErrorException extends Exception {

SyntaxErrorException(String message) { super(message); } } private static boolean isOperator(char ch) { return OPERATORS.indexOf(ch) != -1; }

private static int precedence(char op) { return PRECEDENCE[OPERATORS.indexOf(op)] ; }

}

Consider the infix-postfix conversion algorithm given in your textbook (pages 174-176). Implement the algorithm but do not assume the infix expressions are error free. For each infix expression (your program must handle any number of them) convert and display the correct postfix equivalent. If the infix expression is syntactically incorrect display a message which indicates the type of error. Your program must place the postfix expression into a queue and when conversion is complete display the queue. The algorithm in the book uses a String for the postfix expression, you must use a queue. Operands are single character identifiers. Consider the binary operators of +, -, * and/, with normal operator precedence, parentheses are used to change normal precedence rules. Expressions may contain blanks (any number of them) as delimiters. Your error checking must be incorporated into the translation algorithm given in the book; it cannot be handled in a separate pre-translation pass of the expression. You should be able to recognize at least the following three errors; mismatched parentheses, missing operand, and missing operator. You are to use exceptions as the method of alerting of the error, and must raise a different exception for each of the three errors. A sample run might be: (a + b)*c rightarrow ab + c* (a + b * c rightarrow mismatched parentheses a + + b rightarrow missing operand a + rightarrow missing operand a b + rightarrow missing operator

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_2

Step: 3

blur-text-image_3

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 Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

Students also viewed these Databases questions

Question

2. How will the team select a leader?

Answered: 1 week ago