正确,解释器只显示在解析过程中使用了哪些规则,而忽略了任何 AST 重写规则。
您可以做的是使用StringTemplate
创建一个Graphviz DOT-file。创建这样的DOT 文件后,您可以使用一些第 3 方查看器来显示此树(图表)。
这是 Java 的快速演示(我对 C# 知之甚少,抱歉)。
采用以下产生 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 从中生成词法分析器和解析器文件:
java -cp antlr-3.2.jar org.antlr.Tool ASTDemo.g
然后创建一个小测试工具来解析表达式"12 * (5 - 6); 2^3^(4 + 1);"
并输出一个DOT 文件:
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"
}
可以在这里与其中一位观众一起观看。甚至还有在线观众。以这个为例:https ://dreampuf.github.io/GraphvizOnline/
当向它输入 的内容时ast-tree.dot
,会生成以下图像: