antlr4 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +27 -0
- data/README.md +46 -0
- data/lib/antlr4.rb +262 -0
- data/lib/antlr4/BufferedTokenStream.rb +306 -0
- data/lib/antlr4/CommonTokenFactory.rb +53 -0
- data/lib/antlr4/CommonTokenStream.rb +56 -0
- data/lib/antlr4/FileStream.rb +14 -0
- data/lib/antlr4/InputStream.rb +82 -0
- data/lib/antlr4/IntervalSet.rb +341 -0
- data/lib/antlr4/LL1Analyzer.rb +177 -0
- data/lib/antlr4/Lexer.rb +335 -0
- data/lib/antlr4/ListTokenSource.rb +140 -0
- data/lib/antlr4/Parser.rb +562 -0
- data/lib/antlr4/ParserInterpreter.rb +149 -0
- data/lib/antlr4/ParserRuleContext.rb +162 -0
- data/lib/antlr4/PredictionContext.rb +690 -0
- data/lib/antlr4/Recognizer.rb +162 -0
- data/lib/antlr4/RuleContext.rb +226 -0
- data/lib/antlr4/Token.rb +124 -0
- data/lib/antlr4/TokenFactory.rb +3 -0
- data/lib/antlr4/TokenSource.rb +4 -0
- data/lib/antlr4/TokenStream.rb +3 -0
- data/lib/antlr4/TraceListener.rb +23 -0
- data/lib/antlr4/atn/ATN.rb +133 -0
- data/lib/antlr4/atn/ATNConfig.rb +146 -0
- data/lib/antlr4/atn/ATNConfigSet.rb +215 -0
- data/lib/antlr4/atn/ATNDeserializationOptions.rb +62 -0
- data/lib/antlr4/atn/ATNDeserializer.rb +604 -0
- data/lib/antlr4/atn/ATNSimulator.rb +43 -0
- data/lib/antlr4/atn/ATNState.rb +253 -0
- data/lib/antlr4/atn/ATNType.rb +22 -0
- data/lib/antlr4/atn/LexerATNSimulator.rb +612 -0
- data/lib/antlr4/atn/LexerAction.rb +311 -0
- data/lib/antlr4/atn/LexerActionExecutor.rb +134 -0
- data/lib/antlr4/atn/ParserATNSimulator.rb +1622 -0
- data/lib/antlr4/atn/PredictionMode.rb +525 -0
- data/lib/antlr4/atn/SemanticContext.rb +355 -0
- data/lib/antlr4/atn/Transition.rb +297 -0
- data/lib/antlr4/base.rb +60 -0
- data/lib/antlr4/dfa/DFA.rb +128 -0
- data/lib/antlr4/dfa/DFASerializer.rb +77 -0
- data/lib/antlr4/dfa/DFAState.rb +133 -0
- data/lib/antlr4/error.rb +151 -0
- data/lib/antlr4/error/DiagnosticErrorListener.rb +136 -0
- data/lib/antlr4/error/ErrorListener.rb +109 -0
- data/lib/antlr4/error/ErrorStrategy.rb +742 -0
- data/lib/antlr4/tree/Chunk.rb +31 -0
- data/lib/antlr4/tree/ParseTreeMatch.rb +105 -0
- data/lib/antlr4/tree/ParseTreePattern.rb +70 -0
- data/lib/antlr4/tree/ParseTreePatternMatcher.rb +334 -0
- data/lib/antlr4/tree/RuleTagToken.rb +39 -0
- data/lib/antlr4/tree/TokenTagToken.rb +38 -0
- data/lib/antlr4/tree/Tree.rb +204 -0
- data/lib/antlr4/tree/Trees.rb +111 -0
- data/lib/antlr4/version.rb +5 -0
- data/lib/antlr4/xpath/XPath.rb +354 -0
- data/lib/double_key_map.rb +78 -0
- data/lib/java_symbols.rb +24 -0
- data/lib/uuid.rb +87 -0
- data/test/test_intervalset.rb +664 -0
- data/test/test_tree.rb +140 -0
- data/test/test_uuid.rb +122 -0
- metadata +109 -0
@@ -0,0 +1,525 @@
|
|
1
|
+
# This enumeration defines the prediction modes available in ANTLR 4 along with
|
2
|
+
# utility methods for analyzing configuration sets for conflicts and/or
|
3
|
+
# ambiguities.
|
4
|
+
|
5
|
+
class PredictionMode
|
6
|
+
include JavaSymbols
|
7
|
+
#
|
8
|
+
# The SLL(*) prediction mode. This prediction mode ignores the current
|
9
|
+
# parser context when making predictions. This is the fastest prediction
|
10
|
+
# mode, and provides correct results for many grammars. This prediction
|
11
|
+
# mode is more powerful than the prediction mode provided by ANTLR 3, but
|
12
|
+
# may result in syntax errors for grammar and input combinations which are
|
13
|
+
# not SLL.
|
14
|
+
#
|
15
|
+
# <p>
|
16
|
+
# When using this prediction mode, the parser will either return a correct
|
17
|
+
# parse tree (i.e. the same parse tree that would be returned with the
|
18
|
+
# {@link #LL} prediction mode), or it will report a syntax error. If a
|
19
|
+
# syntax error is encountered when using the {@link #SLL} prediction mode,
|
20
|
+
# it may be due to either an actual syntax error in the input or indicate
|
21
|
+
# that the particular combination of grammar and input requires the more
|
22
|
+
# powerful {@link #LL} prediction abilities to complete successfully.</p>
|
23
|
+
#
|
24
|
+
# <p>
|
25
|
+
# This prediction mode does not provide any guarantees for prediction
|
26
|
+
# behavior for syntactically-incorrect inputs.</p>
|
27
|
+
#
|
28
|
+
SLL = 0
|
29
|
+
#
|
30
|
+
# The LL(*) prediction mode. This prediction mode allows the current parser
|
31
|
+
# context to be used for resolving SLL conflicts that occur during
|
32
|
+
# prediction. This is the fastest prediction mode that guarantees correct
|
33
|
+
# parse results for all combinations of grammars with syntactically correct
|
34
|
+
# inputs.
|
35
|
+
#
|
36
|
+
# <p>
|
37
|
+
# When using this prediction mode, the parser will make correct decisions
|
38
|
+
# for all syntactically-correct grammar and input combinations. However, in
|
39
|
+
# cases where the grammar is truly ambiguous this prediction mode might not
|
40
|
+
# report a precise answer for <em>exactly which</em> alternatives are
|
41
|
+
# ambiguous.</p>
|
42
|
+
#
|
43
|
+
# <p>
|
44
|
+
# This prediction mode does not provide any guarantees for prediction
|
45
|
+
# behavior for syntactically-incorrect inputs.</p>
|
46
|
+
#
|
47
|
+
LL = 1
|
48
|
+
#
|
49
|
+
# The LL(*) prediction mode with exact ambiguity detection. In addition to
|
50
|
+
# the correctness guarantees provided by the {@link #LL} prediction mode,
|
51
|
+
# this prediction mode instructs the prediction algorithm to determine the
|
52
|
+
# complete and exact set of ambiguous alternatives for every ambiguous
|
53
|
+
# decision encountered while parsing.
|
54
|
+
#
|
55
|
+
# <p>
|
56
|
+
# This prediction mode may be used for diagnosing ambiguities during
|
57
|
+
# grammar development. Due to the performance overhead of calculating sets
|
58
|
+
# of ambiguous alternatives, this prediction mode should be avoided when
|
59
|
+
# the exact results are not necessary.</p>
|
60
|
+
#
|
61
|
+
# <p>
|
62
|
+
# This prediction mode does not provide any guarantees for prediction
|
63
|
+
# behavior for syntactically-incorrect inputs.</p>
|
64
|
+
#
|
65
|
+
LL_EXACT_AMBIG_DETECTION = 2
|
66
|
+
|
67
|
+
#
|
68
|
+
# Computes the SLL prediction termination condition.
|
69
|
+
#
|
70
|
+
# <p>
|
71
|
+
# This method computes the SLL prediction termination condition for both of
|
72
|
+
# the following cases.</p>
|
73
|
+
#
|
74
|
+
# <ul>
|
75
|
+
# <li>The usual SLL+LL fallback upon SLL conflict</li>
|
76
|
+
# <li>Pure SLL without LL fallback</li>
|
77
|
+
# </ul>
|
78
|
+
#
|
79
|
+
# <p><strong>COMBINED SLL+LL PARSING</strong></p>
|
80
|
+
#
|
81
|
+
# <p>When LL-fallback is enabled upon SLL conflict, correct predictions are
|
82
|
+
# ensured regardless of how the termination condition is computed by this
|
83
|
+
# method. Due to the substantially higher cost of LL prediction, the
|
84
|
+
# prediction should only fall back to LL when the additional lookahead
|
85
|
+
# cannot lead to a unique SLL prediction.</p>
|
86
|
+
#
|
87
|
+
# <p>Assuming combined SLL+LL parsing, an SLL configuration set with only
|
88
|
+
# conflicting subsets should fall back to full LL, even if the
|
89
|
+
# configuration sets don't resolve to the same alternative (e.g.
|
90
|
+
# {@code {1,2}} and {@code {3,4}}. If there is at least one non-conflicting
|
91
|
+
# configuration, SLL could continue with the hopes that more lookahead will
|
92
|
+
# resolve via one of those non-conflicting configurations.</p>
|
93
|
+
#
|
94
|
+
# <p>Here's the prediction termination rule them: SLL (for SLL+LL parsing)
|
95
|
+
# stops when it sees only conflicting configuration subsets. In contrast,
|
96
|
+
# full LL keeps going when there is uncertainty.</p>
|
97
|
+
#
|
98
|
+
# <p><strong>HEURISTIC</strong></p>
|
99
|
+
#
|
100
|
+
# <p>As a heuristic, we stop prediction when we see any conflicting subset
|
101
|
+
# unless we see a state that only has one alternative associated with it.
|
102
|
+
# The single-alt-state thing lets prediction continue upon rules like
|
103
|
+
# (otherwise, it would admit defeat too soon):</p>
|
104
|
+
#
|
105
|
+
# <p>{@code [12|1|[], 6|2|[], 12|2|[]]. s : (ID | ID ID?) ';' ;}</p>
|
106
|
+
#
|
107
|
+
# <p>When the ATN simulation reaches the state before {@code ';'}, it has a
|
108
|
+
# DFA state that looks like: {@code [12|1|[], 6|2|[], 12|2|[]]}. Naturally
|
109
|
+
# {@code 12|1|[]} and {@code 12|2|[]} conflict, but we cannot stop
|
110
|
+
# processing this node because alternative to has another way to continue,
|
111
|
+
# via {@code [6|2|[]]}.</p>
|
112
|
+
#
|
113
|
+
# <p>It also let's us continue for this rule:</p>
|
114
|
+
#
|
115
|
+
# <p>{@code [1|1|[], 1|2|[], 8|3|[]] a : A | A | A B ;}</p>
|
116
|
+
#
|
117
|
+
# <p>After matching input A, we reach the stop state for rule A, state 1.
|
118
|
+
# State 8 is the state right before B. Clearly alternatives 1 and 2
|
119
|
+
# conflict and no amount of further lookahead will separate the two.
|
120
|
+
# However, alternative 3 will be able to continue and so we do not stop
|
121
|
+
# working on this state. In the previous example, we're concerned with
|
122
|
+
# states associated with the conflicting alternatives. Here alt 3 is not
|
123
|
+
# associated with the conflicting configs, but since we can continue
|
124
|
+
# looking for input reasonably, don't declare the state done.</p>
|
125
|
+
#
|
126
|
+
# <p><strong>PURE SLL PARSING</strong></p>
|
127
|
+
#
|
128
|
+
# <p>To handle pure SLL parsing, all we have to do is make sure that we
|
129
|
+
# combine stack contexts for configurations that differ only by semantic
|
130
|
+
# predicate. From there, we can do the usual SLL termination heuristic.</p>
|
131
|
+
#
|
132
|
+
# <p><strong>PREDICATES IN SLL+LL PARSING</strong></p>
|
133
|
+
#
|
134
|
+
# <p>SLL decisions don't evaluate predicates until after they reach DFA stop
|
135
|
+
# states because they need to create the DFA cache that works in all
|
136
|
+
# semantic situations. In contrast, full LL evaluates predicates collected
|
137
|
+
# during start state computation so it can ignore predicates thereafter.
|
138
|
+
# This means that SLL termination detection can totally ignore semantic
|
139
|
+
# predicates.</p>
|
140
|
+
#
|
141
|
+
# <p>Implementation-wise, {@link ATNConfigSet} combines stack contexts but not
|
142
|
+
# semantic predicate contexts so we might see two configurations like the
|
143
|
+
# following.</p>
|
144
|
+
#
|
145
|
+
# <p>{@code (s, 1, x, {}), (s, 1, x', {p})}</p>
|
146
|
+
#
|
147
|
+
# <p>Before testing these configurations against others, we have to merge
|
148
|
+
# {@code x} and {@code x'} (without modifying the existing configurations).
|
149
|
+
# For example, we test {@code (x+x')==x''} when looking for conflicts in
|
150
|
+
# the following configurations.</p>
|
151
|
+
#
|
152
|
+
# <p>{@code (s, 1, x, {}), (s, 1, x', {p}), (s, 2, x'', {})}</p>
|
153
|
+
#
|
154
|
+
# <p>If the configuration set has predicates (as indicated by
|
155
|
+
# {@link ATNConfigSet#hasSemanticContext}), this algorithm makes a copy of
|
156
|
+
# the configurations to strip out all of the predicates so that a standard
|
157
|
+
# {@link ATNConfigSet} will merge everything ignoring predicates.</p>
|
158
|
+
#
|
159
|
+
def self.hasSLLConflictTerminatingPrediction(mode, configs)
|
160
|
+
# Configs in rule stop states indicate reaching the end of the decision
|
161
|
+
# rule (local context) or end of start rule (full context). If all
|
162
|
+
# configs meet this condition, then none of the configurations is able
|
163
|
+
# to match additional input so we terminate prediction.
|
164
|
+
#
|
165
|
+
if allConfigsInRuleStopStates(configs)
|
166
|
+
return true
|
167
|
+
end
|
168
|
+
|
169
|
+
# pure SLL mode parsing
|
170
|
+
if mode == PredictionMode.SLL
|
171
|
+
# Don't bother with combining configs from different semantic
|
172
|
+
# contexts if we can fail over to full LL; costs more time
|
173
|
+
# since we'll often fail over anyway.
|
174
|
+
if configs.hasSemanticContext
|
175
|
+
# dup configs, tossing out semantic predicates
|
176
|
+
dup = ATNConfigSet.new()
|
177
|
+
configs.each {|c|
|
178
|
+
c = ATNConfig.new(c,SemanticContext.NONE)
|
179
|
+
dup.add(c)
|
180
|
+
}
|
181
|
+
configs = dup
|
182
|
+
end
|
183
|
+
# now we have combined contexts for configs with dissimilar preds
|
184
|
+
end
|
185
|
+
# pure SLL or combined SLL+LL mode parsing
|
186
|
+
altsets = getConflictingAltSubsets(configs)
|
187
|
+
hasConflictingAltSet(altsets) and not hasStateAssociatedWithOneAlt(configs)
|
188
|
+
end
|
189
|
+
# Checks if any configuration in {@code configs} is in a
|
190
|
+
# {@link RuleStopState}. Configurations meeting this condition have reached
|
191
|
+
# the end of the decision rule (local context) or end of start rule (full
|
192
|
+
# context).
|
193
|
+
#
|
194
|
+
# @param configs the configuration set to test
|
195
|
+
# @return {@code true} if any configuration in {@code configs} is in a
|
196
|
+
# {@link RuleStopState}, otherwise {@code false}
|
197
|
+
def self.hasConfigInRuleStopState(configs)
|
198
|
+
configs.each{|c|
|
199
|
+
if c.state.kind_of? RuleStopState
|
200
|
+
return true
|
201
|
+
end
|
202
|
+
}
|
203
|
+
return false
|
204
|
+
end
|
205
|
+
|
206
|
+
# Checks if all configurations in {@code configs} are in a
|
207
|
+
# {@link RuleStopState}. Configurations meeting this condition have reached
|
208
|
+
# the end of the decision rule (local context) or end of start rule (full
|
209
|
+
# context).
|
210
|
+
#
|
211
|
+
# @param configs the configuration set to test
|
212
|
+
# @return {@code true} if all configurations in {@code configs} are in a
|
213
|
+
# {@link RuleStopState}, otherwise {@code false}
|
214
|
+
def self.allConfigsInRuleStopStates(configs)
|
215
|
+
configs.each{|c|
|
216
|
+
if not c.state.kind_of? RuleStopState
|
217
|
+
return false
|
218
|
+
end
|
219
|
+
}
|
220
|
+
return true
|
221
|
+
end
|
222
|
+
|
223
|
+
#
|
224
|
+
# Full LL prediction termination.
|
225
|
+
#
|
226
|
+
# <p>Can we stop looking ahead during ATN simulation or is there some
|
227
|
+
# uncertainty as to which alternative we will ultimately pick, after
|
228
|
+
# consuming more input? Even if there are partial conflicts, we might know
|
229
|
+
# that everything is going to resolve to the same minimum alternative. That
|
230
|
+
# means we can stop since no more lookahead will change that fact. On the
|
231
|
+
# other hand, there might be multiple conflicts that resolve to different
|
232
|
+
# minimums. That means we need more look ahead to decide which of those
|
233
|
+
# alternatives we should predict.</p>
|
234
|
+
#
|
235
|
+
# <p>The basic idea is to split the set of configurations {@code C}, into
|
236
|
+
# conflicting subsets {@code (s, _, ctx, _)} and singleton subsets with
|
237
|
+
# non-conflicting configurations. Two configurations conflict if they have
|
238
|
+
# identical {@link ATNConfig#state} and {@link ATNConfig#context} values
|
239
|
+
# but different {@link ATNConfig#alt} value, e.g. {@code (s, i, ctx, _)}
|
240
|
+
# and {@code (s, j, ctx, _)} for {@code i!=j}.</p>
|
241
|
+
#
|
242
|
+
# <p>Reduce these configuration subsets to the set of possible alternatives.
|
243
|
+
# You can compute the alternative subsets in one pass as follows:</p>
|
244
|
+
#
|
245
|
+
# <p>{@code A_s,ctx = {i | (s, i, ctx, _)}} for each configuration in
|
246
|
+
# {@code C} holding {@code s} and {@code ctx} fixed.</p>
|
247
|
+
#
|
248
|
+
# <p>Or in pseudo-code, for each configuration {@code c} in {@code C}:</p>
|
249
|
+
#
|
250
|
+
# <pre>
|
251
|
+
# map[c] U= c.{@link ATNConfig#alt alt} # map hash/equals uses s and x, not
|
252
|
+
# alt and not pred
|
253
|
+
# </pre>
|
254
|
+
#
|
255
|
+
# <p>The values in {@code map} are the set of {@code A_s,ctx} sets.</p>
|
256
|
+
#
|
257
|
+
# <p>If {@code |A_s,ctx|=1} then there is no conflict associated with
|
258
|
+
# {@code s} and {@code ctx}.</p>
|
259
|
+
#
|
260
|
+
# <p>Reduce the subsets to singletons by choosing a minimum of each subset. If
|
261
|
+
# the union of these alternative subsets is a singleton, then no amount of
|
262
|
+
# more lookahead will help us. We will always pick that alternative. If,
|
263
|
+
# however, there is more than one alternative, then we are uncertain which
|
264
|
+
# alternative to predict and must continue looking for resolution. We may
|
265
|
+
# or may not discover an ambiguity in the future, even if there are no
|
266
|
+
# conflicting subsets this round.</p>
|
267
|
+
#
|
268
|
+
# <p>The biggest sin is to terminate early because it means we've made a
|
269
|
+
# decision but were uncertain as to the eventual outcome. We haven't used
|
270
|
+
# enough lookahead. On the other hand, announcing a conflict too late is no
|
271
|
+
# big deal; you will still have the conflict. It's just inefficient. It
|
272
|
+
# might even look until the end of file.</p>
|
273
|
+
#
|
274
|
+
# <p>No special consideration for semantic predicates is required because
|
275
|
+
# predicates are evaluated on-the-fly for full LL prediction, ensuring that
|
276
|
+
# no configuration contains a semantic context during the termination
|
277
|
+
# check.</p>
|
278
|
+
#
|
279
|
+
# <p><strong>CONFLICTING CONFIGS</strong></p>
|
280
|
+
#
|
281
|
+
# <p>Two configurations {@code (s, i, x)} and {@code (s, j, x')}, conflict
|
282
|
+
# when {@code i!=j} but {@code x=x'}. Because we merge all
|
283
|
+
# {@code (s, i, _)} configurations together, that means that there are at
|
284
|
+
# most {@code n} configurations associated with state {@code s} for
|
285
|
+
# {@code n} possible alternatives in the decision. The merged stacks
|
286
|
+
# complicate the comparison of configuration contexts {@code x} and
|
287
|
+
# {@code x'}. Sam checks to see if one is a subset of the other by calling
|
288
|
+
# merge and checking to see if the merged result is either {@code x} or
|
289
|
+
# {@code x'}. If the {@code x} associated with lowest alternative {@code i}
|
290
|
+
# is the superset, then {@code i} is the only possible prediction since the
|
291
|
+
# others resolve to {@code min(i)} as well. However, if {@code x} is
|
292
|
+
# associated with {@code j>i} then at least one stack configuration for
|
293
|
+
# {@code j} is not in conflict with alternative {@code i}. The algorithm
|
294
|
+
# should keep going, looking for more lookahead due to the uncertainty.</p>
|
295
|
+
#
|
296
|
+
# <p>For simplicity, I'm doing a equality check between {@code x} and
|
297
|
+
# {@code x'} that lets the algorithm continue to consume lookahead longer
|
298
|
+
# than necessary. The reason I like the equality is of course the
|
299
|
+
# simplicity but also because that is the test you need to detect the
|
300
|
+
# alternatives that are actually in conflict.</p>
|
301
|
+
#
|
302
|
+
# <p><strong>CONTINUE/STOP RULE</strong></p>
|
303
|
+
#
|
304
|
+
# <p>Continue if union of resolved alternative sets from non-conflicting and
|
305
|
+
# conflicting alternative subsets has more than one alternative. We are
|
306
|
+
# uncertain about which alternative to predict.</p>
|
307
|
+
#
|
308
|
+
# <p>The complete set of alternatives, {@code [i for (_,i,_)]}, tells us which
|
309
|
+
# alternatives are still in the running for the amount of input we've
|
310
|
+
# consumed at this point. The conflicting sets let us to strip away
|
311
|
+
# configurations that won't lead to more states because we resolve
|
312
|
+
# conflicts to the configuration with a minimum alternate for the
|
313
|
+
# conflicting set.</p>
|
314
|
+
#
|
315
|
+
# <p><strong>CASES</strong></p>
|
316
|
+
#
|
317
|
+
# <ul>
|
318
|
+
#
|
319
|
+
# <li>no conflicts and more than 1 alternative in set => continue</li>
|
320
|
+
#
|
321
|
+
# <li> {@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s, 3, z)},
|
322
|
+
# {@code (s', 1, y)}, {@code (s', 2, y)} yields non-conflicting set
|
323
|
+
# {@code {3}} U conflicting sets {@code min({1,2})} U {@code min({1,2})} =
|
324
|
+
# {@code {1,3}} => continue
|
325
|
+
# </li>
|
326
|
+
#
|
327
|
+
# <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 1, y)},
|
328
|
+
# {@code (s', 2, y)}, {@code (s'', 1, z)} yields non-conflicting set
|
329
|
+
# {@code {1}} U conflicting sets {@code min({1,2})} U {@code min({1,2})} =
|
330
|
+
# {@code {1}} => stop and predict 1</li>
|
331
|
+
#
|
332
|
+
# <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 1, y)},
|
333
|
+
# {@code (s', 2, y)} yields conflicting, reduced sets {@code {1}} U
|
334
|
+
# {@code {1}} = {@code {1}} => stop and predict 1, can announce
|
335
|
+
# ambiguity {@code {1,2}}</li>
|
336
|
+
#
|
337
|
+
# <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 2, y)},
|
338
|
+
# {@code (s', 3, y)} yields conflicting, reduced sets {@code {1}} U
|
339
|
+
# {@code {2}} = {@code {1,2}} => continue</li>
|
340
|
+
#
|
341
|
+
# <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 3, y)},
|
342
|
+
# {@code (s', 4, y)} yields conflicting, reduced sets {@code {1}} U
|
343
|
+
# {@code {3}} = {@code {1,3}} => continue</li>
|
344
|
+
#
|
345
|
+
# </ul>
|
346
|
+
#
|
347
|
+
# <p><strong>EXACT AMBIGUITY DETECTION</strong></p>
|
348
|
+
#
|
349
|
+
# <p>If all states report the same conflicting set of alternatives, then we
|
350
|
+
# know we have the exact ambiguity set.</p>
|
351
|
+
#
|
352
|
+
# <p><code>|A_<em>i</em>|>1</code> and
|
353
|
+
# <code>A_<em>i</em> = A_<em>j</em></code> for all <em>i</em>, <em>j</em>.</p>
|
354
|
+
#
|
355
|
+
# <p>In other words, we continue examining lookahead until all {@code A_i}
|
356
|
+
# have more than one alternative and all {@code A_i} are the same. If
|
357
|
+
# {@code A={{1,2}, {1,3}}}, then regular LL prediction would terminate
|
358
|
+
# because the resolved set is {@code {1}}. To determine what the real
|
359
|
+
# ambiguity is, we have to know whether the ambiguity is between one and
|
360
|
+
# two or one and three so we keep going. We can only stop prediction when
|
361
|
+
# we need exact ambiguity detection when the sets look like
|
362
|
+
# {@code A={{1,2}}} or {@code {{1,2},{1,2}}}, etc...</p>
|
363
|
+
#
|
364
|
+
def self.resolvesToJustOneViableAlt( altsets)
|
365
|
+
return self.getSingleViableAlt(altsets)
|
366
|
+
end
|
367
|
+
|
368
|
+
#
|
369
|
+
# Determines if every alternative subset in {@code altsets} contains more
|
370
|
+
# than one alternative.
|
371
|
+
#
|
372
|
+
# @param altsets a collection of alternative subsets
|
373
|
+
# @return {@code true} if every {@link BitSet} in {@code altsets} has
|
374
|
+
# {@link BitSet#cardinality cardinality} > 1, otherwise {@code false}
|
375
|
+
#
|
376
|
+
def self.allSubsetsConflict(altsets)
|
377
|
+
not hasNonConflictingAltSet(altsets)
|
378
|
+
end
|
379
|
+
|
380
|
+
#
|
381
|
+
# Determines if any single alternative subset in {@code altsets} contains
|
382
|
+
# exactly one alternative.
|
383
|
+
#
|
384
|
+
# @param altsets a collection of alternative subsets
|
385
|
+
# @return {@code true} if {@code altsets} contains a {@link BitSet} with
|
386
|
+
# {@link BitSet#cardinality cardinality} 1, otherwise {@code false}
|
387
|
+
#
|
388
|
+
def self.hasNonConflictingAltSet(altsets)
|
389
|
+
altsets.each { |alts|
|
390
|
+
if alts.length==1
|
391
|
+
return true
|
392
|
+
end
|
393
|
+
}
|
394
|
+
return false
|
395
|
+
end
|
396
|
+
|
397
|
+
#
|
398
|
+
# Determines if any single alternative subset in {@code altsets} contains
|
399
|
+
# more than one alternative.
|
400
|
+
#
|
401
|
+
# @param altsets a collection of alternative subsets
|
402
|
+
# @return {@code true} if {@code altsets} contains a {@link BitSet} with
|
403
|
+
# {@link BitSet#cardinality cardinality} > 1, otherwise {@code false}
|
404
|
+
#
|
405
|
+
def self.hasConflictingAltSet(altsets)
|
406
|
+
altsets.each {|alts |
|
407
|
+
if alts.length>1
|
408
|
+
return true
|
409
|
+
end
|
410
|
+
}
|
411
|
+
return false
|
412
|
+
end
|
413
|
+
|
414
|
+
#
|
415
|
+
# Determines if every alternative subset in {@code altsets} is equivalent.
|
416
|
+
#
|
417
|
+
# @param altsets a collection of alternative subsets
|
418
|
+
# @return {@code true} if every member of {@code altsets} is equal to the
|
419
|
+
# others, otherwise {@code false}
|
420
|
+
#
|
421
|
+
def self.allSubsetsEqual(altsets)
|
422
|
+
first = nil
|
423
|
+
altsets.each {|alts|
|
424
|
+
if first.nil? then
|
425
|
+
first = alts
|
426
|
+
elsif not alts==first
|
427
|
+
return false
|
428
|
+
end
|
429
|
+
}
|
430
|
+
return true
|
431
|
+
end
|
432
|
+
|
433
|
+
#
|
434
|
+
# Returns the unique alternative predicted by all alternative subsets in
|
435
|
+
# {@code altsets}. If no such alternative exists, this method returns
|
436
|
+
# {@link ATN#INVALID_ALT_NUMBER}.
|
437
|
+
#
|
438
|
+
# @param altsets a collection of alternative subsets
|
439
|
+
#
|
440
|
+
def self.getUniqueAlt(altsets)
|
441
|
+
all = getAlts(altsets)
|
442
|
+
if all.length==1
|
443
|
+
return all[0]
|
444
|
+
else
|
445
|
+
return ATN::INVALID_ALT_NUMBER
|
446
|
+
end
|
447
|
+
end
|
448
|
+
# Gets the complete set of represented alternatives for a collection of
|
449
|
+
# alternative subsets. This method returns the union of each {@link BitSet}
|
450
|
+
# in {@code altsets}.
|
451
|
+
#
|
452
|
+
# @param altsets a collection of alternative subsets
|
453
|
+
# @return the set of represented alternatives in {@code altsets}
|
454
|
+
#
|
455
|
+
def self.getAlts(altsets)
|
456
|
+
all = Set.new()
|
457
|
+
altsets.each {|alts |
|
458
|
+
all = all | alts
|
459
|
+
}
|
460
|
+
return all
|
461
|
+
end
|
462
|
+
#
|
463
|
+
# This function gets the conflicting alt subsets from a configuration set.
|
464
|
+
# For each configuration {@code c} in {@code configs}:
|
465
|
+
#
|
466
|
+
# <pre>
|
467
|
+
# map[c] U= c.{@link ATNConfig#alt alt} # map hash/equals uses s and x, not
|
468
|
+
# alt and not pred
|
469
|
+
# </pre>
|
470
|
+
#
|
471
|
+
def self.getConflictingAltSubsets(configs)
|
472
|
+
configToAlts = Hash.new
|
473
|
+
configs.each {|c|
|
474
|
+
s = "#{c.state.stateNumber}/#{c.context}"
|
475
|
+
alts = configToAlts[s]
|
476
|
+
if alts.nil? then
|
477
|
+
alts = Set.new()
|
478
|
+
configToAlts[s] = alts
|
479
|
+
end
|
480
|
+
alts.add(c.alt)
|
481
|
+
}
|
482
|
+
return configToAlts.values()
|
483
|
+
end
|
484
|
+
#
|
485
|
+
# Get a map from state to alt subset from a configuration set. For each
|
486
|
+
# configuration {@code c} in {@code configs}:
|
487
|
+
#
|
488
|
+
# <pre>
|
489
|
+
# map[c.{@link ATNConfig#state state}] U= c.{@link ATNConfig#alt alt}
|
490
|
+
# </pre>
|
491
|
+
#
|
492
|
+
def self.getStateToAltMap(configs)
|
493
|
+
m = Hash.new
|
494
|
+
configs.each {|c|
|
495
|
+
alts = m[c.state ]
|
496
|
+
if alts.nil? then
|
497
|
+
alts = Set.new()
|
498
|
+
m[c.state] = alts
|
499
|
+
end
|
500
|
+
alts.add(c.alt)
|
501
|
+
}
|
502
|
+
return m
|
503
|
+
end
|
504
|
+
def self.hasStateAssociatedWithOneAlt(configs)
|
505
|
+
x = getStateToAltMap(configs)
|
506
|
+
x.values().each {|alts|
|
507
|
+
if alts.length()==1
|
508
|
+
return true
|
509
|
+
end
|
510
|
+
}
|
511
|
+
return false
|
512
|
+
end
|
513
|
+
|
514
|
+
def self.getSingleViableAlt(altsets)
|
515
|
+
viableAlts = Set.new()
|
516
|
+
altsets.each {|alts|
|
517
|
+
minAlt = alts.min
|
518
|
+
viableAlts.add(minAlt)
|
519
|
+
if viableAlts.length>1 # more than 1 viable alt
|
520
|
+
return ATN::INVALID_ALT_NUMBER
|
521
|
+
end
|
522
|
+
}
|
523
|
+
return viableAlts.min
|
524
|
+
end
|
525
|
+
end
|