Write a Java program using JFIexCup to interpret WAE express
Solution
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(\"-\"))
{
try
{
for(int i=0; i<count; i++)
{
if(item[count].equals(\"+\"))
sum += values[findVariableIndex(item[i])];
else sum -= values[findVariableIndex(item[i])];
}
}
catch(ArrayIndexOutOfBoundsException ae)
{
result = \"Semantic Error\";
return;
}
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]);
}
}
WAETester.java
package wae;
import java.util.Scanner;
public class WAETester {
/*
* 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);
WAEInterpreter interpreter = new WAEInterpreter();
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 + \" \";
}
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 t 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 {z 5}
{+ x {+ m z}}
}
}
}
Semantic Error
Enter your choice(WAE or exit): wae
WAE> {with {x 3}
{with {y 4}
{with {z 5}
{+ x {+ m z}}
}
}
Syntax Error
Enter your choice(WAE or exit): exit







