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 */ getLeftmostDerivationStep(int step)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 getLeftmostDerivation(int maxSteps)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 */ getLeftmostDerivation(StringBuffer buf, int step)41 protected abstract int getLeftmostDerivation(StringBuffer buf, int step); 42 43 // just satisfy BaseAST interface; unused as we manually create nodes 44 initialize(int i, String s)45 public void initialize(int i, String s) { 46 } initialize(AST ast)47 public void initialize(AST ast) { 48 } initialize(Token token)49 public void initialize(Token token) { 50 } 51 } 52