KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > LLkParser


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
9 import java.io.IOException JavaDoc;
10
11 /**An LL(k) parser.
12  *
13  * @see persistence.antlr.Token
14  * @see persistence.antlr.TokenBuffer
15  */

16 public class LLkParser extends Parser {
17     int k;
18
19     public LLkParser(int k_) {
20         k = k_;
21     }
22
23     public LLkParser(ParserSharedInputState state, int k_) {
24         super(state);
25         k = k_;
26     }
27
28     public LLkParser(TokenBuffer tokenBuf, int k_) {
29         k = k_;
30         setTokenBuffer(tokenBuf);
31     }
32
33     public LLkParser(TokenStream lexer, int k_) {
34         k = k_;
35         TokenBuffer tokenBuf = new TokenBuffer(lexer);
36         setTokenBuffer(tokenBuf);
37     }
38
39     /**Consume another token from the input stream. Can only write sequentially!
40      * If you need 3 tokens ahead, you must consume() 3 times.
41      * <p>
42      * Note that it is possible to overwrite tokens that have not been matched.
43      * For example, calling consume() 3 times when k=2, means that the first token
44      * consumed will be overwritten with the 3rd.
45      */

46     public void consume() {
47         inputState.input.consume();
48     }
49
50     public int LA(int i) throws TokenStreamException {
51         return inputState.input.LA(i);
52     }
53
54     public Token LT(int i) throws TokenStreamException {
55         return inputState.input.LT(i);
56     }
57
58     private void trace(String JavaDoc ee, String JavaDoc rname) throws TokenStreamException {
59         traceIndent();
60         System.out.print(ee + rname + ((inputState.guessing > 0)?"; [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             }
68             else {
69                 System.out.print("LA(" + i + ")==null");
70             }
71         }
72         System.out.println("");
73     }
74
75     public void traceIn(String JavaDoc rname) throws TokenStreamException {
76         traceDepth += 1;
77         trace("> ", rname);
78     }
79
80     public void traceOut(String JavaDoc rname) throws TokenStreamException {
81         trace("< ", rname);
82         traceDepth -= 1;
83     }
84 }
85
Popular Tags