Use java to write a translator that translates infix additive and subtractive ar
ID: 3623594 • Letter: U
Question
Use java to write a translator that translates infix additive and subtractive arithmetic expressions intoequivalent prefix expressions. For example, given the infix expression 9-5+2 as input, your translator should
produce +-9 5 2. Your translator must handle multi-digit numbers but does not need to handle white space
or line endings (such as carriage returns) in input expressions. Note, however, that since you cannot rely on
infix operators to separate the numbers in prefix expressions, you should print white space in your output
strings to separate numbers.
Next, develop a syntax-directed translation scheme by adding semantic actions at the appropriate points in
the grammar. Finally, since this grammar will still be left-recursive, apply the left-recursion elimination
technique to produce the final right-recursive translation scheme that can be encoded
Use a driver for this program.
Explanation / Answer
Dear user, Here below is the java code: import java.awt.*; import javax.swing.*; public class InfixToPostfixConverter { public static void main( String args[] ) { StringBuffer infix = new StringBuffer(JOptionPane.showInputDialog( "Enter the infix expression: " ) ); System.out.println( "The original infix expression is: " + infix + " " ); StringBuffer postfix = convertToPostfix( infix ); System.out.println( "The expression in postfix notation is: " + postfix + " " ); System.exit( 0 ); } //End of the main function private static StringBuffer convertToPostfix( StringBuffer infix ) { CharacterStack charStack = new CharacterStack(); StringBuffer temporary = new StringBuffer( "" ); charStack.pushChar( '(' ); charStack.print(); infix.append( ')' ); for ( int infixCount = 0; !charStack.isEmpty(); ++infixCount ) { if ( Character.isDigit( infix.charAt( infixCount ) ) ) temporary.append( infix.charAt( infixCount ) + " " ); else if ( infix.charAt( infixCount ) == '(' ) charStack.pushChar( '(' ); else if ( isOperator( infix.charAt( infixCount ) ) ) { while ( isOperator( charStack.stackTop() ) && precedence( harStack.stackTop(), infix.charAt( infixCount ) ) ) temporary.append( charStack.popChar() + " " ); charStack.pushChar( infix.charAt( infixCount ) ); } else if ( infix.charAt( infixCount ) == ')' ) { while ( charStack.stackTop() != '(' ) temporary.append( charStack.popChar() + " " ); charStack.popChar(); } } return temporary; } private static boolean isOperator( char c ) { if ( c == '+' || c == '-' || c == '*' || c == '/' || c == '^' ) return true; else return false; } private static boolean precedence( char operator1, char operator2 ) { if ( operator1 == '^' ) return true; else if ( operator2 == '^' ) return false; else if ( operator1 == '*' || operator1 == '/' ) return true; else if ( operator1 == '+' || operator1 == '-' ) if ( operator2 == '*' || operator2 == '/' ) return false; else return true; return false; } } class CharacterStack extends StackComposition { public char stackTop() { char temp = popChar(); pushChar( temp ); return temp; } public char popChar() { return ( ( Character )super.pop() ).charValue(); } public void pushChar( char c ) { super.push( new Character( c ) ); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.