使用Java在ANTLR4中绘制解析树 [英] Drawing parse tree in ANTLR4 using Java
问题描述
当我第一次在命令行中尝试使用ANTLR4时,我正在使用grun
和gui
参数.现在,我正在开发Java应用程序,并且希望在执行Java程序时显示相同的对话框.
I am new to ANTLR4, when I was first trying it out in command line I was using the grun
with gui
parameter. Now I am developing a Java application and I want to display the same dialog while executing my Java program.
我成功生成了ParseTree,并且可以浏览它.但我也想展示它.我认为它与TreeViewer
类有关,但我不知道如何使用它.
I generated the ParseTree successfully, and I can navigate through it. But I want to display it as well. I think it has something to do with TreeViewer
class but I couldn't figure out how to use it.
谢谢
推荐答案
TreeViewer是一个Swing组件,因此您应该能够将其添加到其他任何SwingComponent,例如JPanel.
TreeViewer is a Swing Component so you should be able to add it to any other SwingComponent, e.g a JPanel.
要实例化TreeViewer(List<String> rules, Tree tree)
,您必须提供:
To instantiate a TreeViewer(List<String> rules, Tree tree)
you will have to provide:
- 完整的规则名称列表,您可以在此处使用
null
,但是使用Parser.getRuleNames()
的结果会产生更好的结果 - 一棵树,这是您解析的结果(类似于
XXXContext
).
- a complete list of rule names, you can use
null
here, but using the result ofParser.getRuleNames()
produces a better result - a tree, which is the result of your parsing (something like
XXXContext
).
这篇关于使用Java在ANTLR4中绘制解析树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!