KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > LLkParser


1 package antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/main/main/antlr/LLkParser.java#5 $
8  */

9
10 import java.io.IOException JavaDoc;
11
12 /**An LL(k) parser.
13  *
14  * @see antlr.Token
15  * @see antlr.TokenBuffer
16  * @see antlr.LL1Parser
17  */

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

50     public void consume() {
51         inputState.input.consume();
52     }
53
54     public int LA(int i) throws TokenStreamException {
55         return inputState.input.LA(i);
56     }
57
58     public Token LT(int i) throws TokenStreamException {
59         return inputState.input.LT(i);
60     }
61
62     private void trace(String JavaDoc ee, String JavaDoc rname) throws TokenStreamException {
63         traceIndent();
64         System.out.print(ee + rname + ((inputState.guessing > 0)?"; [guessing]":"; "));
65         for (int i = 1; i <= k; i++) {
66             if (i != 1) {
67                 System.out.print(", ");
68             }
69             System.out.print("LA(" + i + ")==" + LT(i).getText());
70         }
71         System.out.println("");
72     }
73
74     public void traceIn(String JavaDoc rname) throws TokenStreamException {
75         traceDepth += 1;
76         trace("> ", rname);
77     }
78
79     public void traceOut(String JavaDoc rname) throws TokenStreamException {
80         trace("< ", rname);
81         traceDepth -= 1;
82     }
83 }
84
Popular Tags