我正在使用antlr库来开发Pascal到C的翻译器。其中一个功能应该是解析器输出树的可视化,类似于Eclipse插件生成的:
我已经取得的成就是:
正如您所看到的,使用GraphViz工具生成的树是平的。以下是我用于测试的示例pascal代码:
program DataFiles;
type
StudentRecord = record
Number: Integer;
Name: String;
end;
var
Student: StudentRecord;
f: file of StudentRecord;
begin
Assign(f , 'MyFiledat');
Rewrite(f);
Student.Number := 12345;
Student.Name := 'John Smith';
Write(f , Student);
Close(f);
end.
Java代码:
Core core = Core.getInstance();
String hello = core.readFile(new File("test/test_sources/Program1.p"));
CharStream stream =
new ANTLRStringStream(hello);
PascalTreeLexer lexer = new PascalTreeLexer(stream);
TokenStream tokenStream = new CommonTokenStream(lexer);
PascalTreeParser parser = new PascalTreeParser(tokenStream);
CommonTree tree = (CommonTree) parser.program().getTree();
DOTTreeGenerator gen = new DOTTreeGenerator();
StringTemplate st = gen.toDOT(tree);
System.out.println(st);
最后,DOTTree:
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=""];
n1 [label="program"];
n2 [label="DataFiles"];
n3 [label=";"];
n4 [label="type"];
n5 [label="StudentRecord"];
n6 [label="="];
n7 [label="record"];
n8 [label="Number"];
n9 [label=":"];
n10 [label="Integer"];
n11 [label=";"];
n12 [label="Name"];
n13 [label=":"];
n14 [label="String"];
n15 [label=";"];
n16 [label="end"];
n17 [label=";"];
n18 [label="var"];
n19 [label="Student"];
n20 [label=":"];
n21 [label="StudentRecord"];
n22 [label=";"];
n23 [label="f"];
n24 [label=":"];
n25 [label="file"];
n26 [label="of"];
n27 [label="StudentRecord"];
n28 [label=";"];
n29 [label="begin"];
n30 [label="Assign"];
n31 [label="("];
n32 [label="f"];
n33 [label=","];
n34 [label="'MyFiledat'"];
n35 [label=")"];
n36 [label=";"];
n37 [label="Rewrite"];
n38 [label="("];
n39 [label="f"];
n40 [label=")"];
n41 [label=";"];
n42 [label="Student"];
n43 [label="."];
n44 [label="Number"];
n45 [label=":="];
n46 [label="12345"];
n47 [label=";"];
n48 [label="Student"];
n49 [label="."];
n50 [label="Name"];
n51 [label=":="];
n52 [label="'John Smith'"];
n53 [label=";"];
n54 [label="Write"];
n55 [label="("];
n56 [label="f"];
n57 [label=","];
n58 [label="Student"];
n59 [label=")"];
n60 [label=";"];
n61 [label="Close"];
n62 [label="("];
n63 [label="f"];
n64 [label=")"];
n65 [label=";"];
n66 [label="end"];
n67 [label="."];
n0 -> n1 // "" -> "program"
n0 -> n2 // "" -> "DataFiles"
n0 -> n3 // "" -> ";"
n0 -> n4 // "" -> "type"
n0 -> n5 // "" -> "StudentRecord"
n0 -> n6 // "" -> "="
n0 -> n7 // "" -> "record"
n0 -> n8 // "" -> "Number"
n0 -> n9 // "" -> ":"
n0 -> n10 // "" -> "Integer"
n0 -> n11 // "" -> ";"
n0 -> n12 // "" -> "Name"
n0 -> n13 // "" -> ":"
n0 -> n14 // "" -> "String"
n0 -> n15 // "" -> ";"
n0 -> n16 // "" -> "end"
n0 -> n17 // "" -> ";"
n0 -> n18 // "" -> "var"
n0 -> n19 // "" -> "Student"
n0 -> n20 // "" -> ":"
n0 -> n21 // "" -> "StudentRecord"
n0 -> n22 // "" -> ";"
n0 -> n23 // "" -> "f"
n0 -> n24 // "" -> ":"
n0 -> n25 // "" -> "file"
n0 -> n26 // "" -> "of"
n0 -> n27 // "" -> "StudentRecord"
n0 -> n28 // "" -> ";"
n0 -> n29 // "" -> "begin"
n0 -> n30 // "" -> "Assign"
n0 -> n31 // "" -> "("
n0 -> n32 // "" -> "f"
n0 -> n33 // "" -> ","
n0 -> n34 // "" -> "'MyFiledat'"
n0 -> n35 // "" -> ")"
n0 -> n36 // "" -> ";"
n0 -> n37 // "" -> "Rewrite"
n0 -> n38 // "" -> "("
n0 -> n39 // "" -> "f"
n0 -> n40 // "" -> ")"
n0 -> n41 // "" -> ";"
n0 -> n42 // "" -> "Student"
n0 -> n43 // "" -> "."
n0 -> n44 // "" -> "Number"
n0 -> n45 // "" -> ":="
n0 -> n46 // "" -> "12345"
n0 -> n47 // "" -> ";"
n0 -> n48 // "" -> "Student"
n0 -> n49 // "" -> "."
n0 -> n50 // "" -> "Name"
n0 -> n51 // "" -> ":="
n0 -> n52 // "" -> "'John Smith'"
n0 -> n53 // "" -> ";"
n0 -> n54 // "" -> "Write"
n0 -> n55 // "" -> "("
n0 -> n56 // "" -> "f"
n0 -> n57 // "" -> ","
n0 -> n58 // "" -> "Student"
n0 -> n59 // "" -> ")"
n0 -> n60 // "" -> ";"
n0 -> n61 // "" -> "Close"
n0 -> n62 // "" -> "("
n0 -> n63 // "" -> "f"
n0 -> n64 // "" -> ")"
n0 -> n65 // "" -> ";"
n0 -> n66 // "" -> "end"
n0 -> n67 // "" -> "."
}
我的问题是:为什么所有节点都直接连接到根?如何使这个树看起来与Eclipse插件类似呢?感谢