I'm using antlr4 (4.5.3) with Javascript target, and trying to implement a visitor.
Following the antlr4 book's calculator example (great book BTW) I'm trying to create a similar grammar:
...
expr: expr op=('*'|'/') expr # MulDiv
| expr op=('+'|'-') expr # AddSub
| INT # int
| '(' expr ')' # parens
;
...
The issue: visitor methods are created for the labeled alternatives (for example visitMulDiv) however 2 thing are missing:
- Implementation for
visitExpr
in the base visitor prototype. - Auto detection of the correct alternative when calling
this.visit(ctx.expr())
- meaning visiting the correct visitX method.
This is how visitor in Java is implemented in the the book.
I have worked around this by implementing a visitExpr()
and hacking the context c'tor name (similar to here) but feel the JS should work hack free, just like the Java version.
Is it a bug or am I missing something?