I am having problems with the Python target in ANTLR4. It seems that very few examples are available, and the transition to the corresponding Java code does not seem relevant.
I use the standard Hello.g4 grammar:
Example (built from the standard Hello.g4 example):
input_ = antlr4.FileStream(_FILENAME) lexer = HelloLexer.HelloLexer(input_) stream = antlr4.CommonTokenStream(lexer) parser = HelloParser.HelloParser(stream) rule_name = 'r' tree = getattr(parser, rule_name)()
I also wrote a listener. To confirm / verify that this is correct, I will repeat here:
class HelloListener(antlr4.ParseTreeListener): def enterR(self, ctx): print("enterR") def exitR(self, ctx): print("exitR") def enterId(self, ctx): print("enterId") def exitId(self, ctx): print("exitId")
So, firstly, I cannot guarantee that the line I am giving is valid because I am not getting any output to the screen. How can I define a tree object if something was matched? How to extract the relevant rules / tokens?
A Python example would be great if possible.
python antlr antlr4
Dustin oprea
source share