how print parse-tree using python2 runtime with antlr4

前端 未结 2 1885
盖世英雄少女心
盖世英雄少女心 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
    

提交回复
热议问题