source: trunk/yao/share/antlr-2.7.7/antlr/AlternativeBlock.java @ 1

Last change on this file since 1 was 1, checked in by lnalod, 15 years ago

Initial import of YAO sources

File size: 7.4 KB
Line 
1package antlr;
2
3/* ANTLR Translator Generator
4 * Project led by Terence Parr at http://www.cs.usfca.edu
5 * Software rights: http://www.antlr.org/license.html
6 *
7 * $Id: //depot/code/org.antlr/release/antlr-2.7.7/antlr/AlternativeBlock.java#2 $
8 */
9
10import antlr.collections.impl.Vector;
11
12/**A list of alternatives */
13class AlternativeBlock extends AlternativeElement {
14    protected String initAction = null; // string for init action {...}
15    protected Vector alternatives;      // Contains Alternatives
16
17    protected String label;                     // can label a looping block to break out of it.
18
19    protected int alti, altj;           // which alts are being compared at the moment with
20    // deterministic()?
21    protected int analysisAlt;          // which alt are we computing look on?  Must be alti or altj
22
23    protected boolean hasAnAction = false;      // does any alt have an action?
24    protected boolean hasASynPred = false;      // does any alt have a syntactic predicate?
25
26    protected int ID = 0;                               // used to generate unique variables
27    protected static int nblks; // how many blocks have we allocated?
28    boolean not = false;                                // true if block is inverted.
29
30    boolean greedy = true;                      // Blocks are greedy by default
31    boolean greedySet = false;          // but, if not explicitly greedy, warning might be generated
32
33    protected boolean doAutoGen = true; // false if no AST (or text) to be generated for block
34
35    protected boolean warnWhenFollowAmbig = true; // warn when an empty path or exit path
36
37    protected boolean generateAmbigWarnings = true;  // the general warning "shut-up" mechanism
38    // conflicts with alt of subrule.
39    // Turning this off will suppress stuff
40    // like the if-then-else ambig.
41
42    public AlternativeBlock(Grammar g) {
43        super(g);
44        alternatives = new Vector(5);
45        this.not = false;
46        nblks++;
47        ID = nblks;
48    }
49
50    public AlternativeBlock(Grammar g, Token start, boolean not) {
51        super(g, start);
52        alternatives = new Vector(5);
53//              this.line = start.getLine();
54//              this.column = start.getColumn();
55        this.not = not;
56        nblks++;
57        ID = nblks;
58    }
59
60    public void addAlternative(Alternative alt) {
61        alternatives.appendElement(alt);
62    }
63
64    public void generate() {
65        grammar.generator.gen(this);
66    }
67
68    public Alternative getAlternativeAt(int i) {
69        return (Alternative)alternatives.elementAt(i);
70    }
71
72    public Vector getAlternatives() {
73        return alternatives;
74    }
75
76    public boolean getAutoGen() {
77        return doAutoGen;
78    }
79
80    public String getInitAction() {
81        return initAction;
82    }
83
84    public String getLabel() {
85        return label;
86    }
87
88    public Lookahead look(int k) {
89        return grammar.theLLkAnalyzer.look(k, this);
90    }
91
92    public void prepareForAnalysis() {
93        for (int i = 0; i < alternatives.size(); i++) {
94            // deterministic() uses an alternative cache and sets lookahead depth
95            Alternative a = (Alternative)alternatives.elementAt(i);
96            a.cache = new Lookahead[grammar.maxk + 1];
97            a.lookaheadDepth = GrammarAnalyzer.LOOKAHEAD_DEPTH_INIT;
98        }
99    }
100
101    /**Walk the syntactic predicate and, for a rule ref R, remove
102     * the ref from the list of FOLLOW references for R (stored
103     * in the symbol table.
104     */
105    public void removeTrackingOfRuleRefs(Grammar g) {
106        for (int i = 0; i < alternatives.size(); i++) {
107            Alternative alt = getAlternativeAt(i);
108            AlternativeElement elem = alt.head;
109            while (elem != null) {
110                if (elem instanceof RuleRefElement) {
111                    RuleRefElement rr = (RuleRefElement)elem;
112                    RuleSymbol rs = (RuleSymbol)g.getSymbol(rr.targetRule);
113                    if (rs == null) {
114                        grammar.antlrTool.error("rule " + rr.targetRule + " referenced in (...)=>, but not defined");
115                    }
116                    else {
117                        rs.references.removeElement(rr);
118                    }
119                }
120                else if (elem instanceof AlternativeBlock) {// recurse into subrules
121                    ((AlternativeBlock)elem).removeTrackingOfRuleRefs(g);
122                }
123                elem = elem.next;
124            }
125        }
126    }
127
128    public void setAlternatives(Vector v) {
129        alternatives = v;
130    }
131
132    public void setAutoGen(boolean doAutoGen_) {
133        doAutoGen = doAutoGen_;
134    }
135
136    public void setInitAction(String initAction_) {
137        initAction = initAction_;
138    }
139
140    public void setLabel(String label_) {
141        label = label_;
142    }
143
144    public void setOption(Token key, Token value) {
145        if (key.getText().equals("warnWhenFollowAmbig")) {
146            if (value.getText().equals("true")) {
147                warnWhenFollowAmbig = true;
148            }
149            else if (value.getText().equals("false")) {
150                warnWhenFollowAmbig = false;
151            }
152            else {
153                grammar.antlrTool.error("Value for warnWhenFollowAmbig must be true or false", grammar.getFilename(), key.getLine(), key.getColumn());
154            }
155        }
156        else if (key.getText().equals("generateAmbigWarnings")) {
157            if (value.getText().equals("true")) {
158                generateAmbigWarnings = true;
159            }
160            else if (value.getText().equals("false")) {
161                generateAmbigWarnings = false;
162            }
163            else {
164                grammar.antlrTool.error("Value for generateAmbigWarnings must be true or false", grammar.getFilename(), key.getLine(), key.getColumn());
165            }
166        }
167        else if (key.getText().equals("greedy")) {
168            if (value.getText().equals("true")) {
169                greedy = true;
170                greedySet = true;
171            }
172            else if (value.getText().equals("false")) {
173                greedy = false;
174                greedySet = true;
175            }
176            else {
177                grammar.antlrTool.error("Value for greedy must be true or false", grammar.getFilename(), key.getLine(), key.getColumn());
178            }
179        }
180        else {
181            grammar.antlrTool.error("Invalid subrule option: " + key.getText(), grammar.getFilename(), key.getLine(), key.getColumn());
182        }
183    }
184
185    public String toString() {
186        String s = " (";
187        if (initAction != null) {
188            s += initAction;
189        }
190        for (int i = 0; i < alternatives.size(); i++) {
191            Alternative alt = getAlternativeAt(i);
192            Lookahead cache[] = alt.cache;
193            int k = alt.lookaheadDepth;
194            // dump lookahead set
195            if (k == GrammarAnalyzer.LOOKAHEAD_DEPTH_INIT) {
196            }
197            else if (k == GrammarAnalyzer.NONDETERMINISTIC) {
198                s += "{?}:";
199            }
200            else {
201                s += " {";
202                for (int j = 1; j <= k; j++) {
203                    s += cache[j].toString(",", grammar.tokenManager.getVocabulary());
204                    if (j < k && cache[j + 1] != null) s += ";";
205                }
206                s += "}:";
207            }
208            // dump alternative including pred (if any)
209            AlternativeElement p = alt.head;
210            String pred = alt.semPred;
211            if (pred != null) {
212                s += pred;
213            }
214            while (p != null) {
215                s += p;
216                p = p.next;
217            }
218            if (i < (alternatives.size() - 1)) {
219                s += " |";
220            }
221        }
222        s += " )";
223        return s;
224    }
225
226}
Note: See TracBrowser for help on using the repository browser.