Antlr4 Javascript target - issue with Visitor and labeled alternative
Asked Answered
M

2

2

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:

  1. Implementation for visitExpr in the base visitor prototype.
  2. 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?

Meehan answered 21/4, 2016 at 2:23 Comment(0)
R
2

I believed this is a bug. In runtime source code, ParseTreeVisitor.visit in the latest javascript runtime(4.5.2) is a bit different from python2 version(4.5.3). In python2 version, ParseTreeVisitor.visit leverage RuleContext.accept method to trigger different visitor events. I assumed developers of Antlr4 forgot to update javascript runtime.

There is quick workaround.

antlr4/tree/Tree.js

ParseTreeVisitor.prototype.visit = function(ctx) {
    // if (Utils.isArray(ctx)) {
    //  var self = this;
    //  return ctx.map(function(child) { return visitAtom(self, child)});
    // } else {
    //  return visitAtom(this, ctx);
    // }
    return ctx.accept(this)
};

There is a better way which doesn't modify library function.

ValidatorVisitor.prototype.visitExpr = function(ctx) {
    return ctx.accept(this);
}
Radiant answered 27/8, 2016 at 10:41 Comment(1)
Thanks - this works great. As I prefer not to modify the library function, I used the same concept in my implementation of ValidatorVisitor.prototype.visitExpr()Meehan
S
1

I believe that this problem has been fixed in antlr 4.7:

ParseTreeVisitor.prototype.visit = function(ctx) {
    if (Array.isArray(ctx)) {
        return ctx.map(function(child) {
            return child.accept(this);
        }, this);
    } else {
        return ctx.accept(this);
    }
};
Shoemake answered 8/1, 2018 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.