Question: grammar EasyCalc; program : declar * stmt * ' $$ ' ; declar : type = ( ' bool ' | 'int' | 'real' )

grammar EasyCalc;
program : declar* stmt*'$$';
declar : type=('bool'| 'int' | 'real') ID ';';
stmt : ID ':=' expr ';' # AssignStmt
| 'read' ID ';' # ReadStmt
| 'write' expr ';' # WriteStmt
;
expr : LIT # LitExpr
| ID # IdExpr
|'(' expr ')' # ParenExpr
| op=('to_int'|'to_real')'(' expr ')' # ToExpr
| expr op=('*'|'/') expr # MulDivExpr
| expr op=('+'|'-') expr # AddSubExpr
| expr op=(''|'>') expr # LessGrtrExpr
| expr op='==' expr # EqualExpr
| expr op='and' expr # AndExpr
| expr op='or' expr # OrExpr
|'if' expr 'then' expr 'else' expr # IfExpr
;
DSTOP : '$$';
SSTOP : ';';
BOOL : 'bool';
INT : 'int';
REAL : 'real';
ASSIGN : ':=';
READ : 'read';
WRITE : 'write';
LPAREN : '(';
RRAPEN : ')';
TINT : 'to_int';
TREAL : 'to_real';
MUL : '*';
DIV : '/';
ADD : '+';
SUB : '-';
LESS : '';
GRTR : '>';
EQUAL : '==';
AND : 'and';
OR : 'or';
IF : 'if';
THEN : 'then';
ELSE : 'else';
LIT : [0-9]+|([0-9]*('.'[0-9]|[0-9]'.')[0-9]*)| 'true' |'false';
ID : [a-zA-Z][a-zA-Z0-9_]*;
WS : [\t\r
]+-> skip;
//====================
//====================
//====================
package easycalc;
import java.util.*;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import easycalc.grammar.*;
public class PrettyPrinterApp {
public static void main(String[] args){
//====================
// Read in multiple lines of input
//====================
StringBuilder sb = new StringBuilder();
Scanner scan = new Scanner(System.in);
String nextLine = scan.nextLine();
while (!nextLine.contains("$$")){
sb.append(nextLine);
scan = new Scanner(System.in);
nextLine = scan.nextLine();
}
scan.close();
sb.append(nextLine);
String str = sb.toString();
//====================
// Create the parse tree from the input stream
//====================
CharStream input = CharStreams.fromString(str);
EasyCalcLexer lexer = new EasyCalcLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
EasyCalcParser parser = new EasyCalcParser(tokens);
ParseTree tree = parser.program(); // begin parsing at program rule
//==================
// Walk the tree with the pretty-print listener
//===================
ParseTreeWalker walker = new ParseTreeWalker(); // create standard walker
PrettyPrinterListener printer = new PrettyPrinterListener();
walker.walk(printer, tree); // initiate walk of tree with listener
System.out.println(printer.getProgramString()); // pretty print program
}
//===============
//===============
//===============
package easycalc;
//import easycalc.grammar.EasyCalcBaseListener;
import easycalc.grammar.*;
import java.util.Scanner;
public class PrettyPrinterListener extends EasyCalcBaseListener {
//===================
// Read in Multiple lines of input
//====================
private final StringBuilder sb = new StringBuilder();
public String getProgramString(){
return sb.toString();
}
public void exitDeclar(EasyCalcParser.DeclarContext ctx){
sb.append(ctx.type.getText()).append("").append(ctx.ID().getText()).append(";
");
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!