本文介绍了antlr4 / java:漂亮的打印解析树到标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
初学者提问:如何将可读版本的解析树打印到stdout?
CharStream input = CharStreams.fromFileName ( TESTDATA / test.txt的);
MyLexer lexer = new MyLexer(输入);
CommonTokenStream tokens = new CommonTokenStream(lexer);
MyParser解析器= new MyParser(令牌);
parser.setBuildParseTree(true);
RuleContext tree = parser.record();
System.out.println(tree.toStringTree(parser));
这会将整个树打印在由括号'()'分隔的一行上。
(记录(丈夫< 4601>(姓名KOHAI Nikolaus)\ n(出生* um.1872(地点Ploschitz) )(妻子(婚姻oo)\ n(姓名SCHLOTTHAUER Maria)\ n(出生* um.1877
...
我想要这样的东西
记录
丈夫
< id>
名称
< name>
...
妻子
解决方案
除了图形解析树
Beginners question: how do I print a readable version of the parse tree to stdout?
CharStream input = CharStreams.fromFileName("testdata/test.txt");
MyLexer lexer = new MyLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
MyParser parser = new MyParser(tokens);
parser.setBuildParseTree(true);
RuleContext tree = parser.record();
System.out.println(tree.toStringTree(parser));
this prints the whole tree on a single line delimited by brackets '()'.
(record (husband <4601> (name KOHAI Nikolaus) \n (birth * um.1872 (place Ploschitz)) \n\n) (wife (marriage oo) \n (name SCHLOTTHAUER Maria) \n (birth * um.1877
...
I would like to have something like this
record
husband
<id>
name
<name>
...
wife
解决方案
Besides a graphical parse tree my ANTLR4 extension for Visual Studio Code also produces a formatted text parse tree:
这篇关于antlr4 / java:漂亮的打印解析树到标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!