Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help with this coding question. Answer must be given in java code. Thank

ID: 3870640 • Letter: P

Question

Please help with this coding question. Answer must be given in java code. Thank you

Look over the given Java code for a simple expression grammar and figure out how it works. It is a simple parser that parses and calcualtes the values for a simple expression grammar (given below) { ") "." => { ( "r" l "7) priy-number l expression> The grammars are almost the same except for the addition of exponentiation. make sure your solution works correctly for exponents (and left associativity) for example 4-2 => 16 and 232 512

Explanation / Answer

Answer:

import java.util.Stack;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class Sol{
    public static void main(String[] args) {
        Stack<String> stk = new Stack<String>();
        Stack<Double>values = new Stack<Double>();

        while (!StdIn.isEmpty()) {
            String s1 = StdIn.readString();
            if      (s1.equals("("))               ;
            else if (s1.equals("+"))    stk.push(s1);
            else if (s1.equals("-"))    stk.push(s1);
            else if (s1.equals("*"))    stk.push(s1);
            else if (s1.equals("/"))    stk.push(s1);
            else if (s1.equals("^")) stk.push(s1);
            else if (s1.equals(")")) {
                String op = stk.pop();
                double val = values.pop();
                if      (op.equals("+"))    val = values.pop() + val;
                else if (op.equals("-"))    val = values.pop() - val;
                else if (op.equals("*"))    val = values.pop() * val;
                else if (op.equals("/"))    val = values.pop() / val;
                else if (op.equals("^")) val = (int)Math.pow(values.pop(),val);
                values.push(v);
            }
            else values.push(Double.parseDouble(s1));
        }
        StdOut.println(values.pop());
    }
}

If you StdIn ans StdOut error then make the siutable change in your editor netbeans or eclipse.