Write a Java program (using JFlex/Cup) to interpret WAE expressions. The CFG for
ID: 3788943 • 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
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches {
public static void main( String args[] ) {
// String to be scanned to find the pattern.
String line = "This order was placed for QT3000! OK?";
String pattern = "(.*)(\d+)(.*)";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: " + m.group(0) );
System.out.println("Found value: " + m.group(1) );
System.out.println("Found value: " + m.group(2) );
}else {
System.out.println("NO MATCH");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.