问题描述
有关的宠物项目,我开始与ANTLR捣鼓。下面的一些教程后,我现在正试图创建我自己的语言的语法和生成AST。
For a pet project I started to fiddle with ANTLR. After following some tutorials I'm now trying to create the grammar for my very own language and to generate an AST.
有关现在我乱搞在ANTLRWorks居多,但现在,我已经验证了分析树似乎要被罚款,我想(迭代,因为我还在学习,仍然需要作出关于一些决定树的最终结构)创建的AST。看来antlrworks不会进行可视化(或至少不使用国米preTER功能,调试不工作在任何我的机器)。
For now I'm messing around in ANTLRWorks mostly, but now that I have validated that the parse tree seems to be fine I'd like to (iteratively, because I'm still learning and still need to make some decisions regarding the final structure of the tree) create the AST. It seems that antlrworks won't visualize it (or at least not using the "Interpreter" feature, Debug's not working on any of my machines).
底线:是可视化的AST的唯一途径手工的方式,穿越/显示它或打印树串重新presentation到控制台?
Bottom line: Is the only way to visualize the AST the manual way, traversing/showing it or printing the tree in string representation to a console?
我要找的是一个简单的方式,从输入,语法去 - >视觉AST重新presentation一拉ANTLRWorks的国米preTER功能。任何想法?
What I'm looking for is a simple way to go from input, grammar -> visual AST representation a la the "Interpreter" feature of ANTLRWorks. Any ideas?
推荐答案
正确,跨preTER只能说明什么规则在分析过程中使用,而忽略任何AST重写规则。
Correct, the interpreter only shows what rules are used in the parsing process, and ignores any AST rewrite rules.
你可以做的就是使用 StringTemplate的
以创建一个 DOT-文件。建立这样一个后,您可以使用一些第三方浏览器显示该树(图)
What you can do is use StringTemplate
to create a Graphviz DOT-file. After creating such a DOT-file, you use some 3rd party viewer to display this tree (graph).
下面是Java中的快速演示(我知道小C#,对不起)。
Here's a quick demo in Java (I know little C#, sorry).
以产生一个AST以下(过于简单化)的前pression语法:
Take the following (overly simplistic) expression grammar that produces an AST:
grammar ASTDemo;
options {
output=AST;
}
tokens {
ROOT;
EXPRESSION;
}
parse
: (expression ';')+ -> ^(ROOT expression+) // omit the semi-colon
;
expression
: addExp -> ^(EXPRESSION addExp)
;
addExp
: multExp
( '+'^ multExp
| '-'^ multExp
)*
;
multExp
: powerExp
( '*'^ powerExp
| '/'^ powerExp
)*
;
powerExp
: atom ('^'^ atom)*
;
atom
: Number
| '(' expression ')' -> expression // omit the parenthesis
;
Number
: Digit+ ('.' Digit+)?
;
fragment
Digit
: '0'..'9'
;
Space
: (' ' | '\t' | '\r' | '\n') {skip();}
;
首先,让ANTLR产生词法和语法分析器文件从它:
First let ANTLR generate lexer and parser files from it:
java -cp antlr-3.2.jar org.antlr.Tool ASTDemo.g
然后创建一个小的测试工具,它分析了前pressions 12 *(5 - 6); 2 ^ 3 ^(4 + 1);
并且将输出:
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;
public class MainASTDemo {
public static void main(String[] args) throws Exception {
ANTLRStringStream in = new ANTLRStringStream("12 * (5 - 6); 2^3^(4 + 1);");
ASTDemoLexer lexer = new ASTDemoLexer(in);
CommonTokenStream tokens = new CommonTokenStream(lexer);
ASTDemoParser parser = new ASTDemoParser(tokens);
ASTDemoParser.parse_return returnValue = parser.parse();
CommonTree tree = (CommonTree)returnValue.getTree();
DOTTreeGenerator gen = new DOTTreeGenerator();
StringTemplate st = gen.toDOT(tree);
System.out.println(st);
}
}
全部编译的.java
文件:
// *nix & MacOS
javac -cp .:antlr-3.2.jar *.java
// Windows
javac -cp .;antlr-3.2.jar *.java
然后运行主类和管道输出到一个名为 ast-tree.dot
:
// *nix & MacOS
java -cp .:antlr-3.2.jar MainASTDemo > ast-tree.dot
// Windows
java -cp .;antlr-3.2.jar MainASTDemo > ast-tree.dot
文件 ast-tree.dot
现在包含:
digraph {
ordering=out;
ranksep=.4;
bgcolor="lightgrey"; node [shape=box, fixedsize=false, fontsize=12, fontname="Helvetica-bold", fontcolor="blue"
width=.25, height=.25, color="black", fillcolor="white", style="filled, solid, bold"];
edge [arrowsize=.5, color="black", style="bold"]
n0 [label="ROOT"];
n1 [label="EXPRESSION"];
n1 [label="EXPRESSION"];
n2 [label="*"];
n2 [label="*"];
n3 [label="12"];
n4 [label="EXPRESSION"];
n4 [label="EXPRESSION"];
n5 [label="-"];
n5 [label="-"];
n6 [label="5"];
n7 [label="6"];
n8 [label="EXPRESSION"];
n8 [label="EXPRESSION"];
n9 [label="^"];
n9 [label="^"];
n10 [label="^"];
n10 [label="^"];
n11 [label="2"];
n12 [label="3"];
n13 [label="EXPRESSION"];
n13 [label="EXPRESSION"];
n14 [label="+"];
n14 [label="+"];
n15 [label="4"];
n16 [label="1"];
n0 -> n1 // "ROOT" -> "EXPRESSION"
n1 -> n2 // "EXPRESSION" -> "*"
n2 -> n3 // "*" -> "12"
n2 -> n4 // "*" -> "EXPRESSION"
n4 -> n5 // "EXPRESSION" -> "-"
n5 -> n6 // "-" -> "5"
n5 -> n7 // "-" -> "6"
n0 -> n8 // "ROOT" -> "EXPRESSION"
n8 -> n9 // "EXPRESSION" -> "^"
n9 -> n10 // "^" -> "^"
n10 -> n11 // "^" -> "2"
n10 -> n12 // "^" -> "3"
n9 -> n13 // "^" -> "EXPRESSION"
n13 -> n14 // "EXPRESSION" -> "+"
n14 -> n15 // "+" -> "4"
n14 -> n16 // "+" -> "1"
}
它可以与href=\"http://www.graphviz.org/Resources.php\">许多观众的周围的 http://graph.gafol.net/
在喂养它 ast-tree.dot
的内容,下面的图像产生:
When feeding it the contents of ast-tree.dot
, the following image is produced:
这篇关于可视化与ANTLR创建的AST(在.NET环境中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!