how print parse-tree using python2 runtime with antlr4

这一生的挚爱 提交于 2019-12-05 15:12:06

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.

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