I'm trying to use antlr4 version 4.4 and the python2 runtime. The grammar is from the antlr4 book, page 6, file: Hello.g4:
grammar Hello;
r : 'hello' ID ;
ID : [a-z]+ ;
WS : [ \t\r\n]+ -> skip ;
and I generate lexer and parser with command
antlr4 -Dlanguage=Python2 Hello.g4
the files HelloLexer.py, HelloParser.py and HelloListener.py among other, are then generated. I make a main program test.py to test the generated python parser:
from antlr4 import *
from HelloLexer import HelloLexer
from HelloParser import HelloParser
def main(argv):
input = FileStream(argv[1])
lexer = HelloLexer(input)
stream = CommonTokenStream(lexer)
parser = HelloParser(stream)
tree = parser.r()
print tree.toStringTree(parser) <= the problem is here!
if __name__ == '__main__':
import sys
main(sys.argv)
Everything seems works ok, except that I can't print the parse tree.
C:\Users\LG\antlr\tpantlr2-code\code\install>Test.py data.txt
Traceback (most recent call last):
File "C:\Users\LG\antlr\tpantlr2-code\code\install\Test.py", line 15, in <module>
main(sys.argv)
File "C:\Users\LG\antlr\tpantlr2-code\code\install\Test.py", line 11, in main
print tree.toStringTree(parser)
File "C:\Python27\lib\site-packages\antlr4\RuleContext.py", line 181, in toStringTree
return Trees.toStringTree(self, ruleNames=ruleNames, recog=recog)
File "C:\Python27\lib\site-packages\antlr4\tree\Trees.py", line 48, in toStringTree
s = escapeWhitespace(cls.getNodeText(t, ruleNames), False)
File "C:\Python27\lib\site-packages\antlr4\tree\Trees.py", line 68, in getNodeText
return ruleNames[t.getRuleContext().getRuleIndex()]
TypeError: 'HelloParser' object does not support indexing
I haven't figured out yet what the problem is.