简体   繁体   English

ANTLR 的树语法 AST 图形视图

[英]ANTLR's tree-grammar AST graphical view

Im currently building a Javascript compiler in ANTLR and JAVA.我目前在 ANTLR 和 JAVA 中构建 Javascript 编译器。 I use ANTLR's tree-grammar for generating ASTs.我使用 ANTLR 的树语法来生成 AST。 (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). (仍然怀疑这是否比使用手动定义的 Abstract class 生成节点的异构方法更智能,但这是另一个话题)。

My problem is that when i have parsed some input, lets say, var x = 5 ;我的问题是,当我解析了一些输入时,可以说var x = 5 this is internally represented as;这在内部表示为; VARDECL as root and x as left child and 5 as right child. VARDECL作为根, x作为左孩子, 5作为右孩子。 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?我现在可以选择使用toStringTree()命令打印这棵树,该命令输出 (VARDECL x 5) - 这种表示在较大的程序中很难理解,所以我想知道是否存在使用这个的第三方工具文本树表示作为输入,output 可以很好地图形化树的 model 吗? (Or do i have to implement that as well) (或者我也必须实现它)

Regards Sune.问候孙。

Checkout this previous Q&A how to create a graphical tree of your AST using Graphviz ' DOT language.查看之前的问答,如何使用GraphvizDOT语言创建 AST 的图形树。

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.万一您正在编写自己的 JavaScript 语法,请查看ANTLR wiki 上的语法列表:您可以使用许多可用的 ECMA/JS 语法。

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.当然,由于语言更复杂(赋值、函数、范围等),您将拥有更多不同的节点,但您可以从该示例开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM