5 Purpose: ParseNode is used to represent a parse production
13 public class ParseNode implements Walkable {
16 private ParseNode parent;
17 private ParseNodeVector children;
20 //private SymbolTable st;
22 public ParseNode(String label) {
26 children = new ParseNodeVector();
29 public ParseNode ( String label, int line ) {
33 children = new ParseNodeVector();
36 public void setLabel( String label ) {
40 public String getLabel() {
45 public void setSymbolTable(SymbolTable st) {
47 throw new IRException("symboltable is null!");
52 public SymbolTable getSymbolTable() {
55 return parent.getSymbolTable();
65 public int getLine() {
70 return parent.getLine();
77 public void setParent( ParseNode parent ) {
81 public ParseNode getParent() {
85 public ParseNode insertChild(ParseNode child) {
87 throw new NullPointerException("Can't add null node to parse tree");
90 children.insertElementAt(child, 0);
91 child.setParent(this);
95 public ParseNode insertChild(String newlabel) {
96 ParseNode child = new ParseNode(newlabel, -1);
97 return insertChild(child);
100 public ParseNode addChild( ParseNode child ) {
103 throw new NullPointerException("Can't add null node to parse tree");
105 children.addElement (child);
106 child.setParent(this);
110 public ParseNode addChild( String newlabel ) {
112 ParseNode child = new ParseNode(newlabel, -1);
113 children.addElement(child);
114 child.setParent(this);
118 public ParseNode addChild (String newlabel, int line) {
119 ParseNode child = new ParseNode(newlabel, line);
120 children.addElement(child);
121 child.setParent(this);
125 public ParseNodeVector getChildren() {
129 public ParseNode getChild (String label) {
133 for (i = 0; i < children.size(); i++) {
134 p = children.elementAt(i);
135 if (p.getLabel().equals(label)) {
143 public ParseNode getRoot() {
144 return (parent == null) ? this : parent.getRoot();
147 public String getTerminal () {
148 ParseNode pn = children.elementAt(0);
152 return pn.getLabel();
157 public ParseNodeVector getChildren(String label) {
159 ParseNodeVector v = new ParseNodeVector();
161 for (i = 0; i < children.size(); i++) {
162 ParseNode pn = children.elementAt(i);
163 if (pn.getLabel().equals(label))
170 public String getNodeName() {
171 return label + " - " + getLine();
174 public int getNeighborCount() {
175 return children.size();
178 public Object getNeighbor(int index) {
179 return children.elementAt(index);
182 public String doIndent(int indent) {
184 String output = new String();
185 for(int i=0;i<indent;i++) output += " ";
189 public String PPrint(int indent, boolean recursive) {
191 String output = new String();
193 if (children.size()==0) {
194 output += doIndent(indent) + "<" + label + "/>\n";
196 output += doIndent(indent) + "<" + label + ">\n";
200 for (int i = 0; i < children.size(); i++) {
201 Walkable w = (Walkable)children.elementAt(i);
202 output += w.PPrint(indent, true);
205 for (int i = 0; i < children.size(); i++) {
206 Walkable w = (Walkable)children.elementAt(i);
207 output += doIndent(indent) + "<" + w.getNodeName() + "/>\n";
212 output += doIndent(indent) + "</" + label + ">\n";