01: package antlr;
02:
03: /* ANTLR Translator Generator
04: * Project led by Terence Parr at http://www.cs.usfca.edu
05: * Software rights: http://www.antlr.org/license.html
06: */
07:
08: import antlr.*;
09: 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,
42: int step);
43:
44: // just satisfy BaseAST interface; unused as we manually create nodes
45:
46: public void initialize(int i, String s) {
47: }
48:
49: public void initialize(AST ast) {
50: }
51:
52: public void initialize(Token token) {
53: }
54: }
|