Question: Please fix the problem for below JAVA code, when i enter ( a + a ) $ as input I get invalid output which is

Please fix the problem for below JAVA code, when i enter (a+a) $ as input I get invalid output which is not correct: Please provide the FULL fixed code, this is the third time I'm asking for help and no one was able to fix this
import java.util.*;
public class Project2{
private static final Map> parsingTable = new HashMap>();
private static final Set terminals = new HashSet>(Arrays.asList('+','-','*','/','(',')','a','$'));
private static final Set nonTerminals = new HashSet>(Arrays.asList('E','Q','T','R','F'));
static {
Map eRow = new HashMap>();
eRow.put('(',"TQ");
eRow.put('a',"TQ");
parsingTable.put('E', eRow);
Map qRow = new HashMap>();
qRow.put('+',"+TQ");
qRow.put('-',"-TQ");
qRow.put(')',"");
qRow.put('$',"");
parsingTable.put('Q', qRow);
Map tRow = new HashMap>();
tRow.put('(',"FR");
tRow.put('a',"FR");
parsingTable.put('T', tRow);
Map rRow = new HashMap>();
rRow.put('+',"");
rRow.put('-',"");
rRow.put('*',"*FR");
rRow.put('/',"/FR");
rRow.put(')',"");
rRow.put('$',"");
parsingTable.put('R', rRow);
Map fRow = new HashMap>();
fRow.put('(',"(E)");
fRow.put('a',"a");
parsingTable.put('F', fRow);
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the input string (ending with $):");
String input = scanner.nextLine();
scanner.close();
if (parse(input)){
System.out.println("Output: String is accepted/valid.");
} else {
System.out.println("Output: String is not accepted/invalid.");
}
}
private static boolean parse(String input){
Stack stack = new Stack>();
stack.push('$');
stack.push('E');
int index =0;
while (index input.length()){
char currentChar = input.charAt(index);
if (!terminals.contains(currentChar) && currentChar !=''){
System.out.println("Invalid character in the input string.");
return false;
}
char top = stack.peek();
if (top == currentChar){
stack.pop();
index++;
} else if (nonTerminals.contains(top)){
stack.pop();
if (parsingTable.containsKey(top) && parsingTable.get(top).containsKey(currentChar)){
String production = parsingTable.get(top).get(currentChar);
for (int i = production.length()-1; i >=0; i--){
char prodChar = production.charAt(i);
if (prodChar !=''){
stack.push(prodChar);
}
}
} else {
return false;
}
} else {
return false;
}
System.out.println("Stack: "+ stack);
}
return stack.size()==1 && stack.peek()=='$';
}
}Given the following CFG and the parsing table, write a program to trace input strings
over the alphabet and ending with $.
Given the CFG and the Predictive Parsing table below:
poinls] Write a program to trace an input string given by the user, Save it as
Prog1 and upload it in canvas (either the zip file or (GitHub link). Test your
programwith the following 3 input strings:
(1)(a+a)**a$
(2)a**(aa)S
(3)a(a+a)$
[20 points] Show the content of the stack implementation/stack flow after cach match.
[20 points] Report file - Your report file should contain explanation of your code, in-built
functions used, important checkpoints [if it is present], explanation should be short and
crisp, output screenshot of your code and should not exceed more than 2 pages.
Following is the grammar, and parsing table.
Predietive parsing table
Please fix the problem for below JAVA code, when

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!