KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > ParseTree


1 package persistence.antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/license.html
6  */

7
8 import persistence.antlr.*;
9 import persistence.antlr.collections.AST;
10
11 public abstract class ParseTree extends BaseAST {
12
13     /** Walk parse tree and return requested number of derivation steps.
14      * If steps <= 0, return node text. If steps == 1, return derivation
15      * string at step.
16      */

17     public String JavaDoc getLeftmostDerivationStep(int step) {
18         if ( step<=0 ) {
19             return toString();
20         }
21         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(2000);
22         getLeftmostDerivation(buf, step);
23         return buf.toString();
24     }
25
26     public String JavaDoc getLeftmostDerivation(int maxSteps) {
27         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(2000);
28         buf.append(" "+this.toString());
29         buf.append("\n");
30         for (int d=1; d<maxSteps; d++) {
31             buf.append(" =>");
32             buf.append(getLeftmostDerivationStep(d));
33             buf.append("\n");
34         }
35         return buf.toString();
36     }
37
38     /** Get derivation and return how many you did (less than requested for
39      * subtree roots.
40      */

41     protected abstract int getLeftmostDerivation(StringBuffer JavaDoc buf, int step);
42
43     // just satisfy BaseAST interface; unused as we manually create nodes
44

45     public void initialize(int i, String JavaDoc s) {
46     }
47     public void initialize(AST ast) {
48     }
49     public void initialize(Token token) {
50     }
51 }
52
Popular Tags