Rev | Line | |
---|
[1] | 1 | package antlr; |
---|
| 2 | |
---|
| 3 | /* ANTLR Translator Generator |
---|
| 4 | * Project led by Terence Parr at http://www.cs.usfca.edu |
---|
| 5 | * Software rights: http://www.antlr.org/license.html |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | import antlr.*; |
---|
| 9 | import 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 getLeftmostDerivationStep(int step) { |
---|
| 18 | if ( step<=0 ) { |
---|
| 19 | return toString(); |
---|
| 20 | } |
---|
| 21 | StringBuffer buf = new StringBuffer(2000); |
---|
| 22 | getLeftmostDerivation(buf, step); |
---|
| 23 | return buf.toString(); |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | public String getLeftmostDerivation(int maxSteps) { |
---|
| 27 | StringBuffer buf = new StringBuffer(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 buf, int step); |
---|
| 42 | |
---|
| 43 | // just satisfy BaseAST interface; unused as we manually create nodes |
---|
| 44 | |
---|
| 45 | public void initialize(int i, String s) { |
---|
| 46 | } |
---|
| 47 | public void initialize(AST ast) { |
---|
| 48 | } |
---|
| 49 | public void initialize(Token token) { |
---|
| 50 | } |
---|
| 51 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.