Add Tree Directory
authorbdemsky <bdemsky>
Thu, 9 Feb 2006 19:02:17 +0000 (19:02 +0000)
committerbdemsky <bdemsky>
Thu, 9 Feb 2006 19:02:17 +0000 (19:02 +0000)
Robust/src/IR/Tree/ParseNode.java [new file with mode: 0644]
Robust/src/IR/Tree/ParseNodeDOTVisitor.java [new file with mode: 0644]
Robust/src/IR/Tree/ParseNodeVector.java [new file with mode: 0644]
Robust/src/IR/Tree/Walkable.java [new file with mode: 0644]

diff --git a/Robust/src/IR/Tree/ParseNode.java b/Robust/src/IR/Tree/ParseNode.java
new file mode 100644 (file)
index 0000000..6d414bb
--- /dev/null
@@ -0,0 +1,231 @@
+/*
+ Class: ParseNode
+ Author: Dan Roy
+ Purpose: ParseNode is used to represent a parse production
+
+*/
+
+package IR.Tree;
+
+import java.util.*;
+
+public class ParseNode implements Walkable {
+
+    private String label;
+    private ParseNode parent;
+    private ParseNodeVector children;
+    private int line;
+    private Object literal;
+
+    //private SymbolTable st;
+
+    public ParseNode(String label) {
+       this.label = label;
+       this.line = -1;
+       this.parent = null;
+       this.literal=null;
+       children = new ParseNodeVector();
+    }
+
+    public ParseNode ( String label, int line ) {
+       this.label = label;
+       this.line = line;
+       this.parent = null;
+       this.literal=null;
+       children = new ParseNodeVector();
+    }
+
+    public void setLabel( String label ) {
+       this.label = label;
+    }
+
+    public String getLabel() {
+       return label;
+    }
+
+    public void setLiteral(Object o) {
+       literal=o;
+    }
+
+    public Object getLiteral(Object o) {
+       return literal;
+    }
+
+    /*
+    public void setSymbolTable(SymbolTable st) {
+       if (st == null) {
+           throw new IRException("symboltable is null!");
+       }
+       this.st = st;
+    }
+
+    public SymbolTable getSymbolTable() {
+       if (st == null) {
+           if (parent != null) {
+               return parent.getSymbolTable();
+           } else {
+               return null;
+           }
+       } else {
+           return st;
+       }
+    }
+    */
+
+    public int getLine() {
+       if (line >= 0) {
+           return line;
+       } else {
+           if (parent != null) {
+               return parent.getLine();
+           } else {
+               return 0;
+           }
+       }
+    }
+
+    public void setParent( ParseNode parent ) {
+       this.parent = parent;
+    }
+
+    public ParseNode getParent() {
+       return parent;
+    }
+
+    public ParseNode insertChild(ParseNode child) {
+       if (child == null) {
+           throw new NullPointerException("Can't add null node to parse tree");
+       }
+
+       children.insertElementAt(child, 0);
+       child.setParent(this);
+       return child;
+    }
+
+    public ParseNode insertChild(String newlabel) {
+       ParseNode child = new ParseNode(newlabel, -1);
+       return insertChild(child);
+    }
+
+    public ParseNode addChild( ParseNode child ) {
+
+       if (child == null) {
+           throw new NullPointerException("Can't add null node to parse tree");
+       }
+
+       children.addElement (child);
+       child.setParent(this);
+       return child;
+    }
+
+    public ParseNode addChild( String newlabel ) {
+       
+       ParseNode child = new ParseNode(newlabel, -1);
+       children.addElement(child);
+       child.setParent(this);
+       return child;
+    }
+
+    public ParseNode addChild (String newlabel, int line) {
+       ParseNode child = new ParseNode(newlabel, line);
+       children.addElement(child);
+       child.setParent(this);
+       return child;
+    }
+
+    public ParseNodeVector getChildren() {
+       return children;
+    }
+
+    public ParseNode getChild (String label) {
+       int i;
+       ParseNode p;
+
+       for (i = 0; i < children.size(); i++) {
+           p = children.elementAt(i);
+           if (p.getLabel().equals(label)) {
+               return p;
+           }
+       }
+
+       return null;
+    }
+
+    public ParseNode getRoot() {
+        return (parent == null) ? this : parent.getRoot();
+    }
+
+    public String getTerminal () {
+       ParseNode pn = children.elementAt(0);
+       if (pn == null) {
+           return null;
+       } else {
+           return pn.getLabel();
+       }
+    }
+
+
+    public ParseNodeVector getChildren(String label) {
+       int i;
+       ParseNodeVector v = new ParseNodeVector();
+
+       for (i = 0; i < children.size(); i++) {
+           ParseNode pn = children.elementAt(i);
+           if (pn.getLabel().equals(label))
+               v.addElement(pn);
+       }
+
+       return v;
+    }
+
+    public String getNodeName() {
+       return label + " - " + getLine();
+    }
+
+    public int getNeighborCount() {
+       return children.size();
+    }
+
+    public Object getNeighbor(int index) {
+       return children.elementAt(index);
+    }
+
+    public String doIndent(int indent) {
+
+       String output = new String();
+        for(int i=0;i<indent;i++) output += " ";
+       return output;
+    }
+
+    public String PPrint(int indent, boolean recursive) {
+
+        String output = new String();
+
+       if (children.size()==0) {
+           output += doIndent(indent) + "<" + label + "/>\n";
+       } else {
+           output += doIndent(indent) + "<" + label + ">\n";
+           indent += 2;
+           
+           if (recursive) {
+               for (int i = 0; i < children.size(); i++) {
+                   Walkable w = (Walkable)children.elementAt(i);
+                   output += w.PPrint(indent, true);
+               }
+           } else {
+               for (int i = 0; i < children.size(); i++) {
+                   Walkable w = (Walkable)children.elementAt(i);
+                   output += doIndent(indent) + "<" + w.getNodeName() + "/>\n";
+               }          
+           }
+           
+           indent -= 2;
+           output += doIndent(indent) + "</" + label + ">\n";
+       }
+
+       return output;  
+    }
+
+}
+
diff --git a/Robust/src/IR/Tree/ParseNodeDOTVisitor.java b/Robust/src/IR/Tree/ParseNodeDOTVisitor.java
new file mode 100644 (file)
index 0000000..e990728
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ Class: ParseNodeDOTVisitor
+ Author: Dan Roy
+ Purpose: Traverses a ParseNode tree and generates a DOT file that represents the parse
+          tree.
+
+*/
+
+package IR.Tree;
+
+import java.util.*;
+
+public class ParseNodeDOTVisitor {
+    
+    java.io.PrintWriter output;
+    int tokennumber;
+    int color;
+
+    private ParseNodeDOTVisitor(java.io.OutputStream output) {
+        tokennumber = 0;
+        color = 0;
+        this.output = new java.io.PrintWriter(output, true);
+    }
+
+    private String getNewID(String name) {
+        tokennumber = tokennumber + 1;
+        return new String (name+tokennumber);
+    }
+
+    public static void visit(java.io.OutputStream output, ParseNode root) {
+        ParseNodeDOTVisitor visitor = new ParseNodeDOTVisitor(output);
+        visitor.make(root);
+    }
+    
+    private void make(ParseNode root) {
+        output.println("digraph dotvisitor {");
+        output.println("\tsize=\"7, 10\";");
+        traverse(root, getNewID("root"));
+        output.println("}\n");
+    }
+
+    private String newColor() {
+
+
+        if (color == 0) {
+            color++;
+            return new String("red");
+        } else if (color == 1) {
+            color++;
+            return new String("green");
+        } else {
+            color = 0;
+            return new String("blue");            
+        }
+    }
+
+    private void traverse(ParseNode node, String nodeid) {
+        output.println("\t" + nodeid + " [label=\"" + node.getLabel() + "\",shape=box];");       
+        ParseNodeVector children = node.getChildren();
+        for (int i = 0; i < children.size(); i++) {
+            ParseNode child = children.elementAt(i);
+            String childid = getNewID("node");
+            output.println("\t" + nodeid + " -> " + childid + ";");
+            if (child.getLabel()=="rule") {
+                output.println("\tnode [color=" + newColor() + "];");
+            }
+            traverse(child, childid);
+        }
+    }
+}
diff --git a/Robust/src/IR/Tree/ParseNodeVector.java b/Robust/src/IR/Tree/ParseNodeVector.java
new file mode 100644 (file)
index 0000000..efedcf6
--- /dev/null
@@ -0,0 +1,27 @@
+package IR.Tree;
+
+import java.util.Vector;
+
+public class ParseNodeVector {
+    private Vector v;
+
+    public ParseNodeVector() {
+       v = new Vector();
+    }
+
+    public void addElement(ParseNode pn) {
+        v.addElement(pn);
+    }
+
+    public void insertElementAt(ParseNode pn, int n) {
+       v.insertElementAt(pn, n);
+    }
+
+    public ParseNode elementAt(int i) {
+        return (ParseNode) v.elementAt(i);
+    }
+
+    public int size() {
+        return v.size();
+    }
+}
diff --git a/Robust/src/IR/Tree/Walkable.java b/Robust/src/IR/Tree/Walkable.java
new file mode 100644 (file)
index 0000000..38b6b5f
--- /dev/null
@@ -0,0 +1,34 @@
+package IR.Tree;
+
+/**
+ * The Walkable interface specifies a set of methods that defines a web. 
+ */
+
+public interface Walkable {
+
+    /**
+     * Returns the name of the node
+     */
+    public String getNodeName();
+
+
+    /**
+     * Returns the number of neighbors from this node
+     */
+    public int getNeighborCount();
+
+
+    /**
+     * Returns a specific neighbor
+     */
+    public Object getNeighbor(int index);
+
+    /**
+     * Returns a pretty print of the representation of the node. 
+     *
+     * @param indent    number of blank spaces to skip for a new line
+     * @param recursive if true, recursively print children
+     */
+    public String PPrint(int indent, boolean recursive);
+}
+