So far i have searched the whole "The Definitive ANTLR 4 Reference" book as well as many sites for an answer to my question and i still can't find anything on the topic.
I am using ANTLR 4 to create a sub-set of the C language that does some basic functionality. What i have no idea about is how to implement a simple while loop in my Visitor class. So far i got this in my grammar:
grammar Expr;
prog: stat+ ;
stat: expr NEWLINE # printExpr
| ID '=' expr NEWLINE # assign
| loop NEWLINE # whileLoop
| relational NEWLINE # relat
| NEWLINE # blank
;
expr: expr op=('*'|'/') expr # MulDiv
| expr op=('+'|'-') expr # AddSub
| INT # int
| ID # id
| '(' expr ')' # parens
;
relational: expr op=(GREATER|LESS) expr # GreaterEqual
;
loop: 'while' '('relational')' NEWLINE? '{'stat*'}' #while
;
GREATER : '>' ;
LESS : '<' ;
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 way i could have multiple statements inside a while loop. My Visitor class looks like this:
public class EvalVisitor extends ExprBaseVisitor<Integer> {
/** "memory" for our calculator; variable/value pairs go here */
Map<String, Integer> memory;
public EvalVisitor() {
memory = new HashMap<String, Integer>();
}
/** ID '=' expr NEWLINE */
@Override
public Integer visitAssign(ExprParser.AssignContext ctx) {
String id = ctx.ID().getText(); // id is left-hand side of '='
int value = super.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(ExprParser.PrintExprContext ctx) {
Integer value = super.visit(ctx.expr()); // evaluate the expr child
System.out.println(value); // print the result
return 0; // return dummy value
}
/** INT */
@Override
public Integer visitInt(ExprParser.IntContext ctx) {
return Integer.valueOf(ctx.INT().getText());
}
/** ID */
@Override
public Integer visitId(ExprParser.IdContext ctx) {
String id = ctx.ID().getText();
if ( memory.containsKey(id) ) return memory.get(id);
return 0;
}
/** expr op=('*'|'/') expr */
@Override
public Integer visitMulDiv(ExprParser.MulDivContext ctx) {
int left = super.visit(ctx.expr(0)); // get value of left subexpression
int right = super.visit(ctx.expr(1)); // get value of right subexpression
if ( ctx.op.getType() == ExprParser.MUL ) return left * right;
return left / right; // must be DIV
}
/** expr op=('+'|'-') expr */
@Override
public Integer visitAddSub(ExprParser.AddSubContext ctx) {
int left = super.visit(ctx.expr(0)); // get value of left subexpression
int right = super.visit(ctx.expr(1)); // get value of right subexpression
if ( ctx.op.getType() == ExprParser.ADD ) return left + right;
return left - right; // must be SUB
}
/** '(' expr ')' */
@Override
public Integer visitParens(ExprParser.ParensContext ctx) {
return super.visit(ctx.expr()); // return child expr's value
}
@Override
public boolean visitGreaterEqual(GreaterEqualContext ctx) {
int left = super.visit(ctx.expr(0));
int right = super.visit(ctx.expr(1));
if(ctx.op.getType() == ExprParser.GREATER) {
return left > right;
}
else {
return left < right;
}
}
@Override
public Integer visitWhileLoop(WhileLoopContext ctx) {
if(visit(ctx.getRuleContext())) {
}
return super.visitWhileLoop(ctx);
}
}
Most of the code in the visitor class i took from the book because i am just starting using ANTLR 4. I find it hard to believe that, aside from the grammar for the while loop, there is no mention of how to implement any visitors/listeners or actions for a simple While loop in the book written by Terence Parr. Can somebody help me with writing a Visitor/Listener Java code for a While loop?