使用 Java 在 ANTLR4 中绘制解析树 [英] Drawing parse tree in ANTLR4 using Java

查看:39
本文介绍了使用 Java 在 ANTLR4 中绘制解析树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ANTLR4 的新手,当我第一次在命令行中尝试它时,我使用的是带有 gui 参数的 grun.现在我正在开发一个 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 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 of Parser.getRuleNames() produces a better result
  • a tree, which is the result of your parsing (something like XXXContext).

这篇关于使用 Java 在 ANTLR4 中绘制解析树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆