Generalize definition of SumExpr a little...Lets sum all elements of
[repair.git] / Repair / RepairCompiler / MCC / IR / ParseNodeDOTVisitor.java
1 /*
2  
3  Class: ParseNodeDOTVisitor
4  Author: Dan Roy
5  Purpose: Traverses a ParseNode tree and generates a DOT file that represents the parse
6           tree.
7
8 */
9
10 package MCC.IR;
11
12 import java.util.*;
13
14 public class ParseNodeDOTVisitor {
15     
16     java.io.PrintWriter output;
17     int tokennumber;
18     int color;
19
20     private ParseNodeDOTVisitor(java.io.OutputStream output) {
21         tokennumber = 0;
22         color = 0;
23         this.output = new java.io.PrintWriter(output, true);
24     }
25
26     private String getNewID(String name) {
27         tokennumber = tokennumber + 1;
28         return new String (name+tokennumber);
29     }
30
31     public static void visit(java.io.OutputStream output, ParseNode root) {
32         ParseNodeDOTVisitor visitor = new ParseNodeDOTVisitor(output);
33         visitor.make(root);
34     }
35     
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");
41     }
42
43     private String newColor() {
44
45
46         if (color == 0) {
47             color++;
48             return new String("red");
49         } else if (color == 1) {
50             color++;
51             return new String("green");
52         } else {
53             color = 0;
54             return new String("blue");            
55         }
56     }
57
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() + "];");
67             }
68             traverse(child, childid);
69         }
70     }
71 }