ANTLR 的树语法 AST 图形视图
我目前正在使用 ANTLR 和 JAVA 构建 Javascript 编译器。 我使用 ANTLR 的树语法来生成 AST。 (仍然怀疑这是否比使用手动定义的抽象类来生成节点的异构方法更聪明,但这是另一个主题)。
我的问题是,当我解析一些输入时,可以说, var x = 5;这在内部表示为; VARDECL 作为根,x 作为左子节点,5 作为右子节点。 我现在可以选择使用 toStringTree() 命令打印这棵树,该命令输出 (VARDECL x 5) - 这种表示在较大的程序中很难理解,所以我想知道是否存在第三方工具可以将此文本树表示形式作为输入并可以输出树的漂亮图形模型? (或者我也必须实现这一点)
问候Sune。
Im currently building a Javascript compiler in ANTLR and JAVA.
I use ANTLR's tree-grammar for generating ASTs. (Still in doubt whether this is smarter than a heterogeneous approach with a manually defined Abstract class for generating nodes, but that's another topic).
My problem is that when i have parsed some input, lets say, var x = 5; this is internally represented as; VARDECL as root and x as left child and 5 as right child.
I now have the option to print this tree, using the toStringTree() command, which outputs (VARDECL x 5) - this representations gets quite hard to comprehend in larger programs, so i was wondering if there exists a third party tool that takes this textual tree-representation as input and can output a nice graphically model of the tree? (Or do i have to implement that as well)
Regards Sune.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看之前的问答如何使用 Graphviz 创建 AST 的图形树' DOT 语言。
如果您要编写自己的 JavaScript 语法,请查看 ANTLR wiki 上的语法列表< /a>:有许多 ECMA/JS 语法可供您使用。
最后,您可能想看看之前的问答 我发布了一个答案,展示了如何使用自定义树节点通过树语法来评估语言(在本例中为表达式)。当然,您将拥有更多不同的节点,因为语言更复杂(赋值、函数、范围等),但您可以从该示例开始。
Checkout this previous Q&A how to create a graphical tree of your AST using Graphviz' DOT language.
Just in case you're writing your own JavaScript grammar, have a look at the list of grammars on ANTLR wiki: there are many ECMA/JS grammars available that you can use.
Lastly, you may want to have a look at this previous Q&A where I posted an answer that shows how to evaluate a language (expressions, in this case) with a tree grammar using custom tree nodes. Of course, you'll have much more different nodes because the language is more complex (assignments, functions, scopes, etc.), but you could started with that example.