how print parse-tree using python2 runtime with antlr4

百般思念 提交于 2019-12-07 07:13:14

问题


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.


回答1:


It looks like you took the wrong toStringStree function.

Take a look at the java docs.

This explains the error message "object does not support indexing". The function you chose expects a list of rule names and not the parser.




回答2:


Oddly, toStringTree is a class method in the Python runtimes. You can call it like this to get the lisp style parse tree including stringified tokens:

from antlr4 import *
from antlr4.tree.Trees import Trees
# import your parser & lexer here

# setup your lexer, stream, parser and tree like normal

print(Trees.toStringTree(tree, None, parser))

# the None is an optional rule names list


来源:https://stackoverflow.com/questions/25136463/how-print-parse-tree-using-python2-runtime-with-antlr4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!