Write a Java program (using JFlex/Cup) to interpret WAE expressions. The CFG for
ID: 3789286 • 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 WAJE.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
import java.util.*;
import java.io.*;
import java.lang.*;
public class WAE{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
String str = in.nextLine();
//System.out.println(str);
int len = str.length();
int flag=1;
Stack expr = new Stack();
char arr[] = new char[20];
int l=0;
char opr[] = new char[10];
int m=0;
int term=1;
for(int i=0; i<len; i++){
char ch = str.charAt(i);
if(ch == '{'){
expr.push(ch);
}
if(ch == '}'){
expr.pop();
}
}
if(expr.empty()==false)
flag=0;
if(flag==0){
System.out.println("Syntax Error");
System.exit(0);
}
for(int i=0; i<len; i++){
char ch = str.charAt(i);
if(str.charAt(i)=='w' && str.charAt(i+1)=='i' && str.charAt(i+2)=='t' && str.charAt(i+3)=='h'){
i=i+5;
if(str.charAt(i)=='{' || str.charAt(i)=='}')
i=i+1;
arr[l++]=str.charAt(i);
i=i+2;
arr[l++]=str.charAt(i);
}
}
for(int i=len-1; i>=0; i--){
char ch = str.charAt(i);
if(str.charAt(i)=='+' || str.charAt(i)=='-' || str.charAt(i)=='*' || str.charAt(i)=='/'){
if(term==1){
opr[m++]=str.charAt(i+2);
opr[m++]=str.charAt(i);
opr[m++]=str.charAt(i+4);
term=0;
}
else{
opr[m++]=str.charAt(i);
opr[m++]=str.charAt(i+2);
}
}
}
int result=0;
int itr=1;
for(int i=0; i<opr.length; i++){
if(itr==1){
int val1 = getVal(arr, opr[i]);
int val2 = getVal(arr, opr[i+2]);
if(opr[i+1]=='+')
result=val1+val2;
else if(opr[i+1]=='-')
result=val1-val2;
else if(opr[i+1]=='*')
result=val1*val2;
else if(opr[i+1]=='/')
result=val1/val2;
}
else{
//System.out.print("============" + i + " " + opr[i] + " " + opr[i+1] + " " + opr[i-1]);
int val2 = getVal(arr, opr[i+1]);
if(opr[i+1]=='+')
result=result+val2;
else if(opr[i+1]=='-')
result=result-val2;
else if(opr[i+1]=='*')
result=result*val2;
else if(opr[i+1]=='/')
result=result/val2;
}
itr=2;
i=i+1;
}
System.out.println("result " + result);
}
public static int getVal(char[] arr, char ch){
for(int i=0; i<arr.length; i++){
if(arr[i]==ch){
return Character.getNumericValue(arr[i+1]);
}
}
return -1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.