how print parse-tree using python2 runtime with antlr4

前端 未结 2 1867
盖世英雄少女心
盖世英雄少女心 2021-01-13 11:41

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;                   


        
相关标签:
2条回答
  • 2021-01-13 11:54

    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
    
    0 讨论(0)
  • 2021-01-13 12:02

    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.

    0 讨论(0)
提交回复
热议问题