Write a Java program (using JFlex/Cup) to interpret WAE expressions. The CFG for
ID: 3789012 • Letter: W
Question
Write a Java program (using JFlex/Cup) to interpret WAE expressions. The CFG for WAE expressions is given below: !! = SEMI :: = | {+ } | {- } | {with { } } | You will write a main program, called WAE.java. that prompts the user with WAE> . The user can enter a WAE expression at the prompt or type exit to exit the WAE interpreter. If the user enters a WAE expression, your program should verify that it is a valid expression. If the expression is valid, it should be evaluated and the value of the expression should be printed: Otherwise and error message should be reported. Sample Run: $ java WAE WAE> {with {x 3} {with {y 4} {with {z 5} (+ x {+ y z}}}}} The value is 12 WAE> {with {x 3} {with {y 4} {+ y z}} Syntax Error WAE> {with {x 3} {with {y 4} {+ y z}}} Semantic ErrorExplanation / Answer
PROGRAM CODE:
WAEInterpreter.java
package wae;
import java.util.Stack;
/*
* This class is designed to interpret the WAE expressions
* It uses stack to store the tokens read from the user
* It also maintains a separate array for the all the variable names and values
*/
public class WAEInterpreter {
private Stack<String> items;
private String variables[];
private int values[];
private int variableCount;
private String result;
public WAEInterpreter() {
items = new Stack<String>();
variables = new String[10];
values = new int[10];
variableCount = 0;
}
//Returns the result
public String getResult()
{
return result;
}
//returns the index of the variable 'a' in the variable array
//returns -1 if not found
public int findVariableIndex(String a)
{
for(int m =0; m<variableCount; m++)
{
if(variables[m].equals(a))
{
return m;
}
}
return -1;
}
//evaluates the whole stack to find an output
//stores the sum or difference value in result
//stores appropriate message in case of errors
public void evaluate()
{
String item[] = new String[20];
int count = 0, sum = 0;
while(items.peek().equals("}"))
{
items.pop();
}
while(items.size()>0)
{
item[count] = items.pop();
if(item[count].equals("+") || item[count].equals("-"))
{
for(int i=0; i<count; i++)
{
int index = findVariableIndex(item[i]);
if(index == -1)
{
result = "Semantic Error";
return;
}
else if(item[count].equals("+"))
sum += values[index];
else sum -= values[index];
}
count = 0;
}
else count++;
}
result = "The value is " + sum;
}
//takes the array of tokens and creates adds them into stack or array depending on the value
public void addExpressions(String[] tokens)
{
for(int i=0; i<tokens.length; i++)
{
if(tokens[i].contains("with"))
{
variables[variableCount] = tokens[++i].replace("{", "");
tokens[i+1] = tokens[i+1].replace("}", "");
values[variableCount] = Integer.valueOf(tokens[++i]);
variableCount++;
}
else if(tokens[i].contains("+")) items.push("+");
else if(tokens[i].contains("-")) items.push("-");
else if(tokens[i].contains("}"))
{
if(tokens[i].length() > 1)
{
tokens[i]= tokens[i].replace("}", "");
items.push(tokens[i]);
}
items.push("}");
}
else items.push(tokens[i]);
}
}
//prints the stack to console
//Only for debugging
public void printStack()
{
System.out.println(items);
for(int i=0; i<variableCount; i++)
System.out.println(variables[i] + " = " + values[i]);
}
}
WAE.java
package wae;
import java.util.Scanner;
public class WAE {
/*
* After providing the WAE expression, press enter, then give a space and press
* enter again for the console to accept the input
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String input = "", value = "";
String choice = "";
//Continue to run the program until the user types exit
while(true)
{
System.out.print(" WAE> ");
while(true)
{
value = keyboard.nextLine();
if(value.equals(" "))
break;
else
input += value + " ";
}
WAEInterpreter interpreter = new WAEInterpreter();
String line = input.toString();
//code segment to find out if there is any syntax error due to brackets
int openBracketCount = input.length() - line.replace("{", "").length();
line = input.toString();
int closedBracketCount = input.length() - line.replace("}", "").length();
if(openBracketCount != closedBracketCount)
System.out.println("Syntax Error");
// calls interpreter to interpret the expression
else{
String tokens[] = input.split("\s+");
interpreter.addExpressions(tokens);
interpreter.evaluate();
System.out.println(interpreter.getResult());
}
input = "";
//Ask user if they want to continue
System.out.print(" Enter your choice(WAE or exit): ");
choice = keyboard.nextLine();
if(choice.equals("exit"))
System.exit(0);
else if(choice.toLowerCase().equals("wae"))
continue;
}
}
}
OUTPUT:
WAE> {with {x 3}
{with {y 4}
{with {z 5}
{+ x {+ y z}}
}
}
}
The value is 12
Enter your choice(WAE or exit): wae
WAE> {with {x 3}
{with {y 4}
{with {a 5}
{+ x {+ y z}}
}
}
}
Semantic Error
Enter your choice(WAE or exit): wae
WAE> {with {x 3}
{with {y 4}
{with {z 5}
{+ x {+ y z}}
}
}
Syntax Error
Enter your choice(WAE or exit): exit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.