I try to follow the Antlr4 reference book, with the Python3 target, but I got stuck in the calculator example. On the Antlr4 docs it says
The Python implementation of AntLR is as close as possible to the Java one, so you shouldn't find it difficult to adapt the examples for Python
but I don't get it yet.
The java code visitor has a .visit
method and in python I don't have this method. I think it's because in java the visit method had parameter overloads of the tokens. In python we have visitProg()
, visitAssign()
, visitId()
etc. But now I can't write value = self.visit(ctx.expr())
because we don't know what visit to call?
Or am I missing an instruction somewhere?
Looks like sometime in the last 3+ years this was fixed. I generated a parser from a grammar and targeted Python 3, using:
antlr4 -Dlanguage=Python3 -no-listener -visitor mygrammar.g4
It generates a visitor class that subclasses ParseTreeVisitor
, which is a class in the antlr4-python3-runtime. Looking at the ParseTreeVisitor
class, there is a visit
method.
For those interested in working through the The Definitive ANTLR 4 Reference using Python, the ANTLR4 documentation points you towards this github repo: