antlr4: [Go target] Antlr Visitor does not work properly

The Visitor tree walker for the Go target does not seem to work properly, at least how it works in comparison to all the other targets. This is a continuation of https://github.com/antlr/antlr4/issues/4395. I don’t know why that was closed, the initial comment completely erased, and https://github.com/antlr/antlr4/issues/4404 opened. In any case, https://github.com/antlr/antlr4/issues/4398 is a detailed analysis on the Visitor code for the Go target.

Starting with the original grammar, I added debugging output to all visitor code “derived” in the parent struct of “BaseExprVisitor” (aka “Visitor”), the “BaseExprVisitor” struct, and the structs and functions in the Antlr4 Go runtime. The “workaround” (func (v *Visitor) Visit(t antlr.ParseTree) any ), which is claimed to fix the problem of getting func (v *Visitor) VisitStart(ctx *parser.StartContext) any to be called, does not really fix the issues with the Visitor code. The problem goes far beyond that. Replacing the VisitStart() function with a VisitExpr() or VisitDecl() function shows that the “work-around” does not work because VisitExpr() or VisitDecl() are not called. The default implementation generated by the Antlr tool does not actually traverse the parse tree! You can test this by simply adding debugging output to the generated “Visit” functions for Visit, BaseExprVisitor, and BaseParseTreeVisitor and see for yourself.

For an example that does work, see https://github.com/kaby76/antlr-4395. But, this implementation is not what I expected.

I am not clear what the Visitor code is supposed to look like. There is no example in the documentation.

VisitChildren() does not have an implementation

For all targets except Go, the base class code has an implementation that loops through all children of a node and calls Visit() on the child. There is no function like this anywhere in the Antlr Go runtime, nor is it generated by the tool. The Antlr tool generates code that contains VisitChildren() function calls.

func (v *BaseExprVisitor) VisitProg(ctx *ProgContext) interface{} {
	return v.VisitChildren(ctx)
}

For all targets, the code does exactly what is expected.

For Go, the only function that defines VisitChildren() is:

func (v *BaseParseTreeVisitor) VisitChildren(_ RuleNode) interface{}     { return nil }

I have to conclude that the Visitor code cannot work as is because there is no implementation for VisitChildren(). One must implement the function for Go.

Unclear how to define the derived Visitor struct–or interface

To use the code, all functions need to be defined for the composite visitor struct.

Is it this?

type Visitor struct {
    *parser.BaseExprVisitor
}

Or this?

type Visitor struct {
    parser.BaseExprVisitor
}

Or this?

type Visitor struct {
    parser.ExprVisitor
}

In any of these implementations, one needs to define every function associated with the visitor: Visit(), VisitChildren(), VisitProg(), VisitDecl(), VisitExpr(), VisitTerminal(), VisitErrorNode(). Otherwise, it either doesn’t work or crashes.

In order to get the code to work, I essentially needed to reimplement the entire code from scratch.

If this is true, then this is a significant departure from how most people would understand how the code should work. Yes, Go does not support Inheritance. All the more, the documentation should provide an explicit, working example, complete with driver, grammar, visitor, go.mod, and makefile.

Personally, I don’t use the Visitor code because semantic analysis is better served with the Listener code (for LR-attribute analysis), which people also claim to work.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 26 (2 by maintainers)

Most upvoted comments

@cwarden That example illustrates my point. Every single production in the grammar requires an implementation of the visit function. If you do not implement the VisitStart() function, then the expression nodes are never visited. For all other targets, that is not the case because there is an implementation for VisitChildren(). There is none in the Go runtime. For a toy example like Calc.g4, implementing a handful of extra visitor functions in the chain of notes from the root is not hard. But, for a large grammar with hundreds of rules, this becomes an issue.

There are a lot of words in this discussion, but in fact the visitor does not work for Go.

There is no need to provide an implementation of every visit function.`

If some visit function not implemented, it will call generated method which call BaseParseTreeVisitor.VisitChildren which do nothing. Custom VisitChildren implementation will not help, because generated visitor knows nothing about custom visitor which embeds it.

I would like to give this one more shot. I am building a grammar and front-end for a very large language. I want to use the Visitor pattern, not the Listener because of the flexibility it provides in controlling the visit path and error handling. Many of my Visit methods look as follows:

func (v *Visitor) VisitLinearCatalogModifyingStatementAlt(ctx *gen.LinearCatalogModifyingStatementAltContext) interface{} {
	return ctx.LinearCatalogModifyingStatement().Accept(v)
}

These nodes have a single child and essentially call Accept on that single child.

To remove a lot of boilerplate code, I’d like to be able to provide a VisitChildren method that is used by default, if a more specific method is not provided. It would look something like this:

func (v *Visitor) VisitChilrdren(ctx *?) interface{} {
    return ctx.GetChild(0).Accept()
}

The Go runtime looks like it was designed to enable this, but as it is currently constructed it does not. You have to provide an implementation of every single Visit Method. There’s no default traversal and you cannot provide a default traversal, without modifying generated code. I believe this is what @kaby76 was trying to convey as well as the fact that the other runtimes provide this capability.

As you know, Go does provide the ability to “override” functions on an embedded struct. I think the problem with the current implementation is where the VisitChildren function is declared.

A default implementation of this method is provided on the antlr.BaseParseTreeVisitor as follows.

func (v *BaseParseTreeVisitor) VisitChildren(_ RuleNode) interface{}     { return nil }

I believe if you moved this default implementation to the generated base class (e.g. BaseMVBVisitor) then it could be overriden in my visitor (e.g. MyVisitor).

This would provide significant reduction of boilerplate code for many real grammars. As has been pointed out, for toy and sample grammars this is not an issue. Just provide a Visit function for every rule.

One other thing. Error handling is very awkward! The visitor methods should all return (interface{}, error). This code is very Java-centric, and doesn’t appear to be written with a deep understanding of Go.