-
-
Save BiaChaudhry/2842cd790cbfe4e38d4c4502329deac4 to your computer and use it in GitHub Desktop.
chapter4 of antlr4---Building a Calculator Using a Visitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* Excerpted from "The Definitive ANTLR 4 Reference", | |
* published by The Pragmatic Bookshelf. | |
* Copyrights apply to this code. It may not be used to create training material, | |
* courses, books, articles, and the like. Contact us if you are in doubt. | |
* We make no guarantees that this code is fit for any purpose. | |
* Visit http://www.pragmaticprogrammer.com/titles/tpantlr2 for more book information. | |
***/ | |
import org.antlr.v4.runtime.*; | |
import org.antlr.v4.runtime.tree.ParseTree; | |
import java.io.FileInputStream; | |
import java.io.InputStream; | |
public class Calc { | |
public static void main(String[] args) throws Exception { | |
String inputFile = null; | |
if ( args.length>0 ) inputFile = args[0]; | |
InputStream is = System.in; | |
if ( inputFile!=null ) is = new FileInputStream(inputFile); | |
ANTLRInputStream input = new ANTLRInputStream(is); | |
LabeledExprLexer lexer = new LabeledExprLexer(input); | |
CommonTokenStream tokens = new CommonTokenStream(lexer); | |
LabeledExprParser parser = new LabeledExprParser(tokens); | |
ParseTree tree = parser.prog(); // parse | |
EvalVisitor eval = new EvalVisitor(); | |
eval.visit(tree); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
antlr4 -no-listener -visitor LabeledExpr.g4 # -visitor is required!!! | |
javac Calc.java LabeledExpr*.java | |
java Calc t.expr | |
#answer | |
# 193 | |
# 17 | |
# 9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* Excerpted from "The Definitive ANTLR 4 Reference", | |
* published by The Pragmatic Bookshelf. | |
* Copyrights apply to this code. It may not be used to create training material, | |
* courses, books, articles, and the like. Contact us if you are in doubt. | |
* We make no guarantees that this code is fit for any purpose. | |
* Visit http://www.pragmaticprogrammer.com/titles/tpantlr2 for more book information. | |
***/ | |
import java.util.HashMap; | |
import java.util.Map; | |
public class EvalVisitor extends LabeledExprBaseVisitor<Integer> { | |
/** "memory" for our calculator; variable/value pairs go here */ | |
Map<String, Integer> memory = new HashMap<String, Integer>(); | |
/** ID '=' expr NEWLINE */ | |
@Override | |
public Integer visitAssign(LabeledExprParser.AssignContext ctx) { | |
String id = ctx.ID().getText(); // id is left-hand side of '=' | |
int value = visit(ctx.expr()); // compute value of expression on right | |
memory.put(id, value); // store it in our memory | |
return value; | |
} | |
/** expr NEWLINE */ | |
@Override | |
public Integer visitPrintExpr(LabeledExprParser.PrintExprContext ctx) { | |
Integer value = visit(ctx.expr()); // evaluate the expr child | |
System.out.println(value); // print the result | |
return 0; // return dummy value | |
} | |
/** INT */ | |
@Override | |
public Integer visitInt(LabeledExprParser.IntContext ctx) { | |
return Integer.valueOf(ctx.INT().getText()); | |
} | |
/** ID */ | |
@Override | |
public Integer visitId(LabeledExprParser.IdContext ctx) { | |
String id = ctx.ID().getText(); | |
if ( memory.containsKey(id) ) return memory.get(id); | |
return 0; | |
} | |
/** expr op=('*'|'/') expr */ | |
@Override | |
public Integer visitMulDiv(LabeledExprParser.MulDivContext ctx) { | |
int left = visit(ctx.expr(0)); // get value of left subexpression | |
int right = visit(ctx.expr(1)); // get value of right subexpression | |
if ( ctx.op.getType() == LabeledExprParser.MUL ) return left * right; | |
return left / right; // must be DIV | |
} | |
/** expr op=('+'|'-') expr */ | |
@Override | |
public Integer visitAddSub(LabeledExprParser.AddSubContext ctx) { | |
int left = visit(ctx.expr(0)); // get value of left subexpression | |
int right = visit(ctx.expr(1)); // get value of right subexpression | |
if ( ctx.op.getType() == LabeledExprParser.ADD ) return left + right; | |
return left - right; // must be SUB | |
} | |
/** '(' expr ')' */ | |
@Override | |
public Integer visitParens(LabeledExprParser.ParensContext ctx) { | |
return visit(ctx.expr()); // return child expr's value | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grammar LabeledExpr; // rename to distinguish from Expr.g4 | |
prog: stat+ ; | |
stat: expr NEWLINE # printExpr | |
| ID '=' expr NEWLINE # assign | |
| NEWLINE # blank | |
; | |
expr: expr op=('*'|'/') expr # MulDiv | |
| expr op=('+'|'-') expr # AddSub | |
| INT # int | |
| ID # id | |
| '(' expr ')' # parens | |
; | |
MUL : '*' ; // assigns token name to '*' used above in grammar | |
DIV : '/' ; | |
ADD : '+' ; | |
SUB : '-' ; | |
ID : [a-zA-Z]+ ; // match identifiers | |
INT : [0-9]+ ; // match integers | |
NEWLINE:'\r'? '\n' ; // return newlines to parser (is end-statement signal) | |
WS : [ \t]+ -> skip ; // toss out whitespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 | |
a = 5 | |
b = 6 | |
a+b*2 | |
(1+2)*3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Forked