KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > AlternativeBlock


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 persistence.antlr.collections.impl.Vector;
10
11 /**A list of alternatives */
12 class AlternativeBlock extends AlternativeElement {
13     protected String JavaDoc initAction = null; // string for init action {...}
14
protected Vector alternatives; // Contains Alternatives
15

16     protected String JavaDoc label; // can label a looping block to break out of it.
17

18     protected int alti, altj; // which alts are being compared at the moment with
19
// deterministic()?
20
protected int analysisAlt; // which alt are we computing look on? Must be alti or altj
21

22     protected boolean hasAnAction = false; // does any alt have an action?
23
protected boolean hasASynPred = false; // does any alt have a syntactic predicate?
24

25     protected int ID = 0; // used to generate unique variables
26
protected static int nblks; // how many blocks have we allocated?
27
boolean not = false; // true if block is inverted.
28

29     boolean greedy = true; // Blocks are greedy by default
30
boolean greedySet = false; // but, if not explicitly greedy, warning might be generated
31

32     protected boolean doAutoGen = true; // false if no AST (or text) to be generated for block
33

34     protected boolean warnWhenFollowAmbig = true; // warn when an empty path or exit path
35

36     protected boolean generateAmbigWarnings = true; // the general warning "shut-up" mechanism
37
// conflicts with alt of subrule.
38
// Turning this off will suppress stuff
39
// like the if-then-else ambig.
40

41     public AlternativeBlock(Grammar g) {
42         super(g);
43         alternatives = new Vector(5);
44         this.not = false;
45         nblks++;
46         ID = nblks;
47     }
48
49     public AlternativeBlock(Grammar g, Token start, boolean not) {
50         super(g, start);
51         alternatives = new Vector(5);
52 // this.line = start.getLine();
53
// this.column = start.getColumn();
54
this.not = not;
55         nblks++;
56         ID = nblks;
57     }
58
59     public void addAlternative(Alternative alt) {
60         alternatives.appendElement(alt);
61     }
62
63     public void generate() {
64         grammar.generator.gen(this);
65     }
66
67     public Alternative getAlternativeAt(int i) {
68         return (Alternative)alternatives.elementAt(i);
69     }
70
71     public Vector getAlternatives() {
72         return alternatives;
73     }
74
75     public boolean getAutoGen() {
76         return doAutoGen;
77     }
78
79     public String JavaDoc getInitAction() {
80         return initAction;
81     }
82
83     public String JavaDoc getLabel() {
84         return label;
85     }
86
87     public Lookahead look(int k) {
88         return grammar.theLLkAnalyzer.look(k, this);
89     }
90
91     public void prepareForAnalysis() {
92         for (int i = 0; i < alternatives.size(); i++) {
93             // deterministic() uses an alternative cache and sets lookahead depth
94
Alternative a = (Alternative)alternatives.elementAt(i);
95             a.cache = new Lookahead[grammar.maxk + 1];
96             a.lookaheadDepth = GrammarAnalyzer.LOOKAHEAD_DEPTH_INIT;
97         }
98     }
99
100     /**Walk the syntactic predicate and, for a rule ref R, remove
101      * the ref from the list of FOLLOW references for R (stored
102      * in the symbol table.
103      */

104     public void removeTrackingOfRuleRefs(Grammar g) {
105         for (int i = 0; i < alternatives.size(); i++) {
106             Alternative alt = getAlternativeAt(i);
107             AlternativeElement elem = alt.head;
108             while (elem != null) {
109                 if (elem instanceof RuleRefElement) {
110                     RuleRefElement rr = (RuleRefElement)elem;
111                     RuleSymbol rs = (RuleSymbol)g.getSymbol(rr.targetRule);
112                     if (rs == null) {
113                         grammar.antlrTool.error("rule " + rr.targetRule + " referenced in (...)=>, but not defined");
114                     }
115                     else {
116                         rs.references.removeElement(rr);
117                     }
118                 }
119                 else if (elem instanceof AlternativeBlock) {// recurse into subrules
120
((AlternativeBlock)elem).removeTrackingOfRuleRefs(g);
121                 }
122                 elem = elem.next;
123             }
124         }
125     }
126
127     public void setAlternatives(Vector v) {
128         alternatives = v;
129     }
130
131     public void setAutoGen(boolean doAutoGen_) {
132         doAutoGen = doAutoGen_;
133     }
134
135     public void setInitAction(String JavaDoc initAction_) {
136         initAction = initAction_;
137     }
138
139     public void setLabel(String JavaDoc label_) {
140         label = label_;
141     }
142
143     public void setOption(Token key, Token value) {
144         if (key.getText().equals("warnWhenFollowAmbig")) {
145             if (value.getText().equals("true")) {
146                 warnWhenFollowAmbig = true;
147             }
148             else if (value.getText().equals("false")) {
149                 warnWhenFollowAmbig = false;
150             }
151             else {
152                 grammar.antlrTool.error("Value for warnWhenFollowAmbig must be true or false", grammar.getFilename(), key.getLine(), key.getColumn());
153             }
154         }
155         else if (key.getText().equals("generateAmbigWarnings")) {
156             if (value.getText().equals("true")) {
157                 generateAmbigWarnings = true;
158             }
159             else if (value.getText().equals("false")) {
160                 generateAmbigWarnings = false;
161             }
162             else {
163                 grammar.antlrTool.error("Value for generateAmbigWarnings must be true or false", grammar.getFilename(), key.getLine(), key.getColumn());
164             }
165         }
166         else if (key.getText().equals("greedy")) {
167             if (value.getText().equals("true")) {
168                 greedy = true;
169                 greedySet = true;
170             }
171             else if (value.getText().equals("false")) {
172                 greedy = false;
173                 greedySet = true;
174             }
175             else {
176                 grammar.antlrTool.error("Value for greedy must be true or false", grammar.getFilename(), key.getLine(), key.getColumn());
177             }
178         }
179         else {
180             grammar.antlrTool.error("Invalid subrule option: " + key.getText(), grammar.getFilename(), key.getLine(), key.getColumn());
181         }
182     }
183
184     public String JavaDoc toString() {
185         String JavaDoc s = " (";
186         if (initAction != null) {
187             s += initAction;
188         }
189         for (int i = 0; i < alternatives.size(); i++) {
190             Alternative alt = getAlternativeAt(i);
191             Lookahead cache[] = alt.cache;
192             int k = alt.lookaheadDepth;
193             // dump lookahead set
194
if (k == GrammarAnalyzer.LOOKAHEAD_DEPTH_INIT) {
195             }
196             else if (k == GrammarAnalyzer.NONDETERMINISTIC) {
197                 s += "{?}:";
198             }
199             else {
200                 s += " {";
201                 for (int j = 1; j <= k; j++) {
202                     s += cache[j].toString(",", grammar.tokenManager.getVocabulary());
203                     if (j < k && cache[j + 1] != null) s += ";";
204                 }
205                 s += "}:";
206             }
207             // dump alternative including pred (if any)
208
AlternativeElement p = alt.head;
209             String JavaDoc pred = alt.semPred;
210             if (pred != null) {
211                 s += pred;
212             }
213             while (p != null) {
214                 s += p;
215                 p = p.next;
216             }
217             if (i < (alternatives.size() - 1)) {
218                 s += " |";
219             }
220         }
221         s += " )";
222         return s;
223     }
224
225 }
226
Popular Tags