Question: Create an ANTLR 4 grammar for an easy calculator language based on the following specifications. program - - > declaration * statement * $$ declaration
Create an ANTLR grammar for an easy calculator language based on the following specifications.
program declaration statement $$
declaration bool identifier ; int identifier ; real identifier ;
statement identifier : expression ; read identifier ; write expression ;
expression expression operator expression identifier literal expression toint expression toreal expression if expression then expression else expression
operator and or
literal realnumber intnumber true false
Use precedence from C language to get the precedence of the operators. An identifier starts with a letter and can contain letters, numbers, and underscores. An integer number is one or more digits. A real number contains a dot and at least one digit. A printer file ParserApp. java is provided. This file should be in the and is used to test whether your created grammar is correct or not. Here are the test cases you can use to test your results. Note that you should also check whether the parsing follows correct operator precedence.
Test should NOT parse
int a;
a :;
bool b;
$$
Test should parse
int a;
bool b;
a :;
b : false;
$$
Test should parse
a :;
$$
Test should parse
a : if true then else ;
$$
Test should parse
bool a;
int ;
real ;
$$
Test should parse
a :;
read a;
write ;
$$
Test should NOT parse
read ;
$$
Test should parse
a : and ;
$$
import java.util.;
import org.antlr.vruntime.;
import org.antlr.vruntime.tree.;
import easycalc.grammar.;
public class ParserApp
public static void mainString args
Read in multiple lines of input
StringBuilder sb new StringBuilder;
Scanner scan new ScannerSystemin;
String nextLine scan.nextLine;
while nextLine.contains$$
sbappend nextLine;
scan new ScannerSystemin;
nextLine scan.nextLine;
scan.close;
sbappendnextLine;
String str sbtoString;
Create the parse tree from the input
CharStream input CharStreams.fromStringstr;
EasyCalcLexer lexer new EasyCalcLexerinput;
CommonTokenStream tokens new CommonTokenStreamlexer;
EasyCalcParser parser new EasyCalcParsertokens;
ParseTree tree parser.program; begin parsing at program rule
Print out the LISPstyle tree
System.out.printlntreetoStringTreeparser;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
