3 Class: ParseNodeDOTVisitor
5 Purpose: Traverses a ParseNode tree and generates a DOT file that represents the parse
14 public class ParseNodeDOTVisitor {
16 java.io.PrintWriter output;
20 private ParseNodeDOTVisitor(java.io.OutputStream output) {
23 this.output = new java.io.PrintWriter(output, true);
26 private String getNewID(String name) {
27 tokennumber = tokennumber + 1;
28 return new String(name+tokennumber);
31 public static void visit(java.io.OutputStream output, ParseNode root) {
32 ParseNodeDOTVisitor visitor = new ParseNodeDOTVisitor(output);
36 private void make(ParseNode root) {
37 output.println("digraph dotvisitor {");
38 output.println("\tsize=\"7, 10\";");
39 traverse(root, getNewID("root"));
40 output.println("}\n");
43 private String newColor() {
48 return new String("red");
49 } else if (color == 1) {
51 return new String("green");
54 return new String("blue");
58 private void traverse(ParseNode node, String nodeid) {
59 output.println("\t" + nodeid + " [label=\"" + node.getLabel() + "\",shape=box];");
60 ParseNodeVector children = node.getChildren();
61 for (int i = 0; i < children.size(); i++) {
62 ParseNode child = children.elementAt(i);
63 String childid = getNewID("node");
64 output.println("\t" + nodeid + " -> " + childid + ";");
65 if (child.getLabel()=="rule") {
66 output.println("\tnode [color=" + newColor() + "];");
68 traverse(child, childid);