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: /**An LL(k) parser.
09: *
10: * @see antlr.Token
11: * @see antlr.TokenBuffer
12: */
13: public class LLkParser extends Parser {
14: int k;
15:
16: public LLkParser(int k_) {
17: k = k_;
18: }
19:
20: public LLkParser(ParserSharedInputState state, int k_) {
21: super (state);
22: k = k_;
23: }
24:
25: public LLkParser(TokenBuffer tokenBuf, int k_) {
26: k = k_;
27: setTokenBuffer(tokenBuf);
28: }
29:
30: public LLkParser(TokenStream lexer, int k_) {
31: k = k_;
32: TokenBuffer tokenBuf = new TokenBuffer(lexer);
33: setTokenBuffer(tokenBuf);
34: }
35:
36: /**Consume another token from the input stream. Can only write sequentially!
37: * If you need 3 tokens ahead, you must consume() 3 times.
38: * <p>
39: * Note that it is possible to overwrite tokens that have not been matched.
40: * For example, calling consume() 3 times when k=2, means that the first token
41: * consumed will be overwritten with the 3rd.
42: */
43: public final void consume() {
44: inputState.input.consume();
45: }
46:
47: public final int LA(int i) {
48: return inputState.input.LA(i);
49: }
50:
51: public final Token LT(int i) {
52: return inputState.input.LT(i);
53: }
54:
55: private void trace(String ee, String rname) {
56: traceIndent();
57: System.out.print(ee
58: + rname
59: + ((inputState.guessing > 0) ? "; [guessing="
60: + inputState.guessing + "]" : "; "));
61: for (int i = 1; i <= k; i++) {
62: if (i != 1) {
63: System.out.print(", ");
64: }
65: if (LT(i) != null) {
66: System.out.print("LA(" + i + ")==" + LT(i).getText());
67: } else {
68: System.out.print("LA(" + i + ")==null");
69: }
70: }
71: System.out.println("");
72: }
73:
74: public void traceIn(String rname) {
75: traceDepth += 1;
76: trace("> ", rname);
77: }
78:
79: public void traceOut(String rname) {
80: trace("< ", rname);
81: traceDepth -= 1;
82: }
83:
84: //vk++ for analyzing time spent in guessing
85: protected void syntacticPredicateStarted(int id, int nestingLevel,
86: int line) {
87: }
88:
89: protected void syntacticPredicateFailed(int id, int nestingLevel) {
90: }
91:
92: protected void syntacticPredicateSucceeded(int id, int nestingLevel) {
93: }
94: //vk--
95: }
|