Updated documentation in PredictionMode

This commit is contained in:
Sam Harwell 2013-01-03 04:03:34 -06:00
parent 98d2ba8fb5
commit d95cdab065
1 changed files with 384 additions and 234 deletions

View File

@ -96,92 +96,138 @@ public enum PredictionMode {
} }
/** /**
SLL prediction termination. * Computes the SLL prediction termination condition.
*
There are two cases: the usual combined SLL+LL parsing and * <p/>
pure SLL parsing that has no fail over to full LL. *
* This method computes the SLL prediction termination condition for both of
COMBINED SLL+LL PARSING * the following cases.
*
SLL can decide to give up any point, even immediately, * <ul>
failing over to full LL. To be as efficient as possible, * <li>The usual SLL+LL fallback upon SLL conflict</li>
though, SLL should fail over only when it's positive it can't get * <li>Pure SLL without LL fallback</li>
anywhere on more lookahead without seeing a conflict. * </ul>
*
Assuming combined SLL+LL parsing, an SLL confg set with only * <p/>
conflicting subsets should failover to full LL, even if the *
config sets don't resolve to the same alternative like {1,2} * <strong>COMBINED SLL+LL PARSING</strong>
and {3,4}. If there is at least one nonconflicting set of *
configs, SLL could continue with the hopes that more lookahead * <p/>
will resolve via one of those nonconflicting configs. *
* When LL-fallback is enabled upon SLL conflict, correct predictions are
Here's the prediction termination rule them: SLL (for SLL+LL * ensured regardless of how the termination condition is computed by this
parsing) stops when it sees only conflicting config subsets. * method. Due to the substantially higher cost of LL prediction, the
In contrast, full LL keeps going when there is uncertainty. * prediction should only fall back to LL when the additional lookahead
* cannot lead to a unique SLL prediction.
HEURISTIC *
* <p/>
As a heuristic, we stop prediction when we see any conflicting subset *
unless we see a state that only has one alternative associated with * Assuming combined SLL+LL parsing, an SLL configuration set with only
it. The single-alt-state thing lets prediction continue upon rules * conflicting subsets should fall back to full LL, even if the
like (otherwise, it would admit defeat too soon): * configuration sets don't resolve to the same alternative (e.g.
* {@code {1,2}} and {@code {3,4}}. If there is at least one non-conflicting
// [12|1|[], 6|2|[], 12|2|[]]. * configuration, SLL could continue with the hopes that more lookahead will
s : (ID | ID ID?) ';' ; * resolve via one of those non-conflicting configurations.
*
When the ATN simulation reaches the state before ';', it has a DFA * <p/>
state that looks like: [12|1|[], 6|2|[], 12|2|[]]. Naturally 12|1|[] *
and 12|2|[] conflict, but we cannot stop processing this node because * Here's the prediction termination rule them: SLL (for SLL+LL parsing)
alternative to has another way to continue, via [6|2|[]]. * stops when it sees only conflicting configuration subsets. In contrast,
* full LL keeps going when there is uncertainty.
It also let's us continue for this rule: *
* <p/>
// [1|1|[], 1|2|[], 8|3|[]] *
a : A | A | A B ; * <strong>HEURISTIC</strong>
*
After matching input A, we reach the stop state for rule A, state 1. * <p/>
State 8 is the state right before B. Clearly alternatives 1 and 2 *
conflict and no amount of further lookahead will separate the two. * As a heuristic, we stop prediction when we see any conflicting subset
However, alternative 3 will be able to continue and so we do not stop * unless we see a state that only has one alternative associated with it.
working on this state. In the previous example, we're concerned with * The single-alt-state thing lets prediction continue upon rules like
states associated with the conflicting alternatives. Here alt 3 is not * (otherwise, it would admit defeat too soon):
associated with the conflicting configs, but since we can continue *
looking for input reasonably, don't declare the state done. * <p/>
*
PURE SLL PARSING * {@code [12|1|[], 6|2|[], 12|2|[]]. s : (ID | ID ID?) ';' ;}
*
To handle pure SLL parsing, all we have to do is make sure that we * <p/>
combine stack contexts for configurations that differ only by semantic *
predicate. From there, we can do the usual SLL termination heuristic. * When the ATN simulation reaches the state before {@code ';'}, it has a
* DFA state that looks like: {@code [12|1|[], 6|2|[], 12|2|[]]}. Naturally
PREDICATES IN SLL+LL PARSING * {@code 12|1|[]} and {@code 12|2|[]} conflict, but we cannot stop
* processing this node because alternative to has another way to continue,
SLL decisions don't evaluate predicates until after they reach DFA * via {@code [6|2|[]]}.
stop states because they need to create the DFA cache that *
works in all (semantic) situations. (In contrast, full LL * <p/>
evaluates predicates collected during start state computation *
so it can ignore predicates thereafter.) This means that SLL * It also let's us continue for this rule:
termination detection can totally ignore semantic predicates. *
* <p/>
Of course, implementation-wise, ATNConfigSets combine stack *
contexts but not semantic predicate contexts so we might see * {@code [1|1|[], 1|2|[], 8|3|[]] a : A | A | A B ;}
two configs like this: *
* <p/>
(s, 1, x, {}), (s, 1, x', {p}) *
* After matching input A, we reach the stop state for rule A, state 1.
Before testing these configurations against others, we have * State 8 is the state right before B. Clearly alternatives 1 and 2
to merge x and x' (w/o modifying the existing configs). For * conflict and no amount of further lookahead will separate the two.
example, we test (x+x')==x'' when looking for conflicts in * However, alternative 3 will be able to continue and so we do not stop
the following configs. * working on this state. In the previous example, we're concerned with
* states associated with the conflicting alternatives. Here alt 3 is not
(s, 1, x, {}), (s, 1, x', {p}), (s, 2, x'', {}) * associated with the conflicting configs, but since we can continue
* looking for input reasonably, don't declare the state done.
If the configuration set has predicates, which we can test *
quickly, this algorithm makes a copy of the configs and * <p/>
strip out all of the predicates so that a standard *
ATNConfigSet will merge everything ignoring * <strong>PURE SLL PARSING</strong>
predicates. *
*/ * <p/>
*
* To handle pure SLL parsing, all we have to do is make sure that we
* combine stack contexts for configurations that differ only by semantic
* predicate. From there, we can do the usual SLL termination heuristic.
*
* <p/>
*
* <strong>PREDICATES IN SLL+LL PARSING</strong>
*
* <p/>
*
* SLL decisions don't evaluate predicates until after they reach DFA stop
* states because they need to create the DFA cache that works in all
* semantic situations. In contrast, full LL evaluates predicates collected
* during start state computation so it can ignore predicates thereafter.
* This means that SLL termination detection can totally ignore semantic
* predicates.
*
* <p/>
*
* Implementation-wise, {@link ATNConfigSet} combines stack contexts but not
* semantic predicate contexts so we might see two configurations like the
* following.
*
* <p/>
*
* {@code (s, 1, x, {}), (s, 1, x', {p})}
*
* <p/>
*
* Before testing these configurations against others, we have to merge
* {@code x} and {@code x'} (without modifying the existing configurations).
* For example, we test {@code (x+x')==x''} when looking for conflicts in
* the following configurations.
*
* <p/>
*
* {@code (s, 1, x, {}), (s, 1, x', {p}), (s, 2, x'', {})}
*
* <p/>
*
* If the configuration set has predicates (as indicated by
* {@link ATNConfigSet#hasSemanticContext}), this algorithm makes a copy of
* the configurations to strip out all of the predicates so that a standard
* {@link ATNConfigSet} will merge everything ignoring predicates.
*/
public static boolean hasSLLConflictTerminatingPrediction(PredictionMode mode, @NotNull ATNConfigSet configs) { public static boolean hasSLLConflictTerminatingPrediction(PredictionMode mode, @NotNull ATNConfigSet configs) {
/* Configs in rule stop states indicate reaching the end of the decision /* Configs in rule stop states indicate reaching the end of the decision
* rule (local context) or end of start rule (full context). If all * rule (local context) or end of start rule (full context). If all
@ -258,145 +304,211 @@ public enum PredictionMode {
} }
/** /**
Full LL prediction termination. * Full LL prediction termination.
*
Can we stop looking ahead during ATN simulation or is there some * <p/>
uncertainty as to which alternative we will ultimately pick, after *
consuming more input? Even if there are partial conflicts, we might * Can we stop looking ahead during ATN simulation or is there some
know that everything is going to resolve to the same minimum * uncertainty as to which alternative we will ultimately pick, after
alt. That means we can stop since no more lookahead will change that * consuming more input? Even if there are partial conflicts, we might know
fact. On the other hand, there might be multiple conflicts that * that everything is going to resolve to the same minimum alternative. That
resolve to different minimums. That means we need more look ahead to * means we can stop since no more lookahead will change that fact. On the
decide which of those alternatives we should predict. * other hand, there might be multiple conflicts that resolve to different
* minimums. That means we need more look ahead to decide which of those
The basic idea is to split the set of configurations, C, into * alternatives we should predict.
conflicting (s, _, ctx, _) subsets and singleton subsets with *
non-conflicting configurations. Two config's conflict if they have * <p/>
identical state and rule stack contexts but different alternative *
numbers: (s, i, ctx, _), (s, j, ctx, _) for i!=j. * The basic idea is to split the set of configurations {@code C}, into
* conflicting subsets {@code (s, _, ctx, _)} and singleton subsets with
Reduce these config subsets to the set of possible alternatives. You * non-conflicting configurations. Two configurations conflict if they have
can compute the alternative subsets in one go as follows: * identical {@link ATNConfig#state} and {@link ATNConfig#context} values
* but different {@link ATNConfig#alt} value, e.g. {@code (s, i, ctx, _)}
A_s,ctx = {i | (s, i, ctx, _) for in C holding s, ctx fixed} * and {@code (s, j, ctx, _)} for {@code i!=j}.
*
Or in pseudo-code: * <p/>
*
for c in C: * Reduce these configuration subsets to the set of possible alternatives.
map[c] U= c.alt # map hash/equals uses s and x, not alt and not pred * You can compute the alternative subsets in one pass as follows:
*
Then map.values is the set of A_s,ctx sets. * <p/>
*
If |A_s,ctx|=1 then there is no conflict associated with s and ctx. * {@code A_s,ctx = {i | (s, i, ctx, _)}} for each configuration in
* {@code C} holding {@code s} and {@code ctx} fixed.
Reduce the subsets to singletons by choosing a minimum of each subset. *
If the union of these alternatives sets is a singleton, then no amount * <p/>
of more lookahead will help us. We will always pick that *
alternative. If, however, there is more than one alternative, then we * Or in pseudo-code, for each configuration {@code c} in {@code C}:
are uncertain which alt to predict and must continue looking for *
resolution. We may or may not discover an ambiguity in the future, * <pre>
even if there are no conflicting subsets this round. * map[c] U= c.{@link ATNConfig#alt alt} # map hash/equals uses s and x, not
* alt and not pred
The biggest sin is to terminate early because it means we've made a * </pre>
decision but were uncertain as to the eventual outcome. We haven't *
used enough lookahead. On the other hand, announcing a conflict too * <p/>
late is no big deal; you will still have the conflict. It's just *
inefficient. It might even look until the end of file. * The values in {@code map} are the set of {@code A_s,ctx} sets.
*
Semantic predicates for full LL aren't involved in this decision * <p/>
because the predicates are evaluated during start state computation. *
This set of configurations was derived from the initial subset with * If {@code |A_s,ctx|=1} then there is no conflict associated with
configurations holding false predicate stripped out. * {@code s} and {@code ctx}.
*
CONFLICTING CONFIGS * <p/>
*
Two configurations, (s, i, x) and (s, j, x'), conflict when i!=j but * Reduce the subsets to singletons by choosing a minimum of each subset. If
x = x'. Because we merge all (s, i, _) configurations together, that * the union of these alternative subsets is a singleton, then no amount of
means that there are at most n configurations associated with state s * more lookahead will help us. We will always pick that alternative. If,
for n possible alternatives in the decision. The merged stacks * however, there is more than one alternative, then we are uncertain which
complicate the comparison of config contexts, x and x'. Sam checks to * alternative to predict and must continue looking for resolution. We may
see if one is a subset of the other by calling merge and checking to * or may not discover an ambiguity in the future, even if there are no
see if the merged result is either x or x'. If the x associated with * conflicting subsets this round.
lowest alternative i is the superset, then i is the only possible *
prediction since the others resolve to min i as well. If, however, x * <p/>
is associated with j>i then at least one stack configuration for j is *
not in conflict with alt i. The algorithm should keep going, looking * The biggest sin is to terminate early because it means we've made a
for more lookahead due to the uncertainty. * decision but were uncertain as to the eventual outcome. We haven't used
* enough lookahead. On the other hand, announcing a conflict too late is no
For simplicity, I'm doing a equality check between x and x' that lets * big deal; you will still have the conflict. It's just inefficient. It
the algorithm continue to consume lookahead longer than necessary. * might even look until the end of file.
The reason I like the equality is of course the simplicity but also *
because that is the test you need to detect the alternatives that are * <p/>
actually in conflict. *
* No special consideration for semantic predicates is required because
CONTINUE/STOP RULE * predicates are evaluated on-the-fly for full LL prediction, ensuring that
* no configuration contains a semantic context during the termination
Continue if union of resolved alt sets from nonconflicting and * check.
conflicting alt subsets has more than one alt. We are uncertain about *
which alternative to predict. * <p/>
*
The complete set of alternatives, [i for (_,i,_)], tells us * <strong>CONFLICTING CONFIGS</strong>
which alternatives are still in the running for the amount of input *
we've consumed at this point. The conflicting sets let us to strip * <p/>
away configurations that won't lead to more states (because we *
resolve conflicts to the configuration with a minimum alternate for * Two configurations {@code (s, i, x)} and {@code (s, j, x')}, conflict
given conflicting set.) * when {@code i!=j} but {@code x=x'}. Because we merge all
* {@code (s, i, _)} configurations together, that means that there are at
CASES: * most {@code n} configurations associated with state {@code s} for
* {@code n} possible alternatives in the decision. The merged stacks
* no conflicts & > 1 alt in set => continue * complicate the comparison of configuration contexts {@code x} and
* {@code x'}. Sam checks to see if one is a subset of the other by calling
* (s, 1, x), (s, 2, x), (s, 3, z) * merge and checking to see if the merged result is either {@code x} or
(s', 1, y), (s', 2, y) * {@code x'}. If the {@code x} associated with lowest alternative {@code i}
yields nonconflicting set {3} U conflicting sets min({1,2}) U min({1,2}) = {1,3} * is the superset, then {@code i} is the only possible prediction since the
=> continue * others resolve to {@code min(i)} as well. However, if {@code x} is
* associated with {@code j>i} then at least one stack configuration for
* (s, 1, x), (s, 2, x), * {@code j} is not in conflict with alternative {@code i}. The algorithm
(s', 1, y), (s', 2, y) * should keep going, looking for more lookahead due to the uncertainty.
(s'', 1, z) *
yields nonconflicting set you this {1} U conflicting sets min({1,2}) U min({1,2}) = {1} * <p/>
=> stop and predict 1 *
* For simplicity, I'm doing a equality check between {@code x} and
* (s, 1, x), (s, 2, x), * {@code x'} that lets the algorithm continue to consume lookahead longer
(s', 1, y), (s', 2, y) * than necessary. The reason I like the equality is of course the
yields conflicting, reduced sets {1} U {1} = {1} * simplicity but also because that is the test you need to detect the
=> stop and predict 1, can announce ambiguity {1,2} * alternatives that are actually in conflict.
*
* (s, 1, x), (s, 2, x) * <p/>
(s', 2, y), (s', 3, y) *
yields conflicting, reduced sets {1} U {2} = {1,2} * <strong>CONTINUE/STOP RULE</strong>
=> continue *
* <p/>
* (s, 1, x), (s, 2, x) *
(s', 3, y), (s', 4, y) * Continue if union of resolved alternative sets from non-conflicting and
yields conflicting, reduced sets {1} U {3} = {1,3} * conflicting alternative subsets has more than one alternative. We are
=> continue * uncertain about which alternative to predict.
*
EXACT AMBIGUITY DETECTION * <p/>
*
If all states report the same conflicting alt set, then we know we * The complete set of alternatives, {@code [i for (_,i,_)]}, tells us which
have the real ambiguity set: * alternatives are still in the running for the amount of input we've
* consumed at this point. The conflicting sets let us to strip away
|A_i|>1 and A_i = A_j for all i, j. * configurations that won't lead to more states because we resolve
* conflicts to the configuration with a minimum alternate for the
In other words, we continue examining lookahead until all A_i have * conflicting set.
more than one alt and all A_i are the same. If A={{1,2}, {1,3}}, then *
regular LL prediction would terminate because the resolved set is * <p/>
{1}. To determine what the real ambiguity is, we have to know whether *
the ambiguity is between one and two or one and three so we keep * <strong>CASES</strong>
going. We can only stop prediction when we need exact ambiguity *
detection when the sets look like A={{1,2}} or {{1,2},{1,2}} etc... * <ul>
*
* <li>no conflicts and more than 1 alternative in set =&gt; continue</li>
*
* <li> {@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s, 3, z)},
* {@code (s', 1, y)}, {@code (s', 2, y)} yields non-conflicting set
* {@code {3}} U conflicting sets {@code min({1,2})} U {@code min({1,2})} =
* {@code {1,3}} =&gt; continue
* </li>
*
* <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 1, y)},
* {@code (s', 2, y)}, {@code (s'', 1, z)} yields non-conflicting set
* {@code {1}} U conflicting sets {@code min({1,2})} U {@code min({1,2})} =
* {@code {1}} =&gt; stop and predict 1</li>
*
* <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 1, y)},
* {@code (s', 2, y)} yields conflicting, reduced sets {@code {1}} U
* {@code {1}} = {@code {1}} =&gt; stop and predict 1, can announce
* ambiguity {@code {1,2}}</li>
*
* <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 2, y)},
* {@code (s', 3, y)} yields conflicting, reduced sets {@code {1}} U
* {@code {2}} = {@code {1,2}} =&gt; continue</li>
*
* <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 3, y)},
* {@code (s', 4, y)} yields conflicting, reduced sets {@code {1}} U
* {@code {3}} = {@code {1,3}} =&gt; continue</li>
*
* </ul>
*
* <strong>EXACT AMBIGUITY DETECTION</strong>
*
* <p/>
*
* If all states report the same conflicting set of alternatives, then we
* know we have the exact ambiguity set.
*
* <p/>
*
* <code>|A_<em>i</em>|&gt;1</code> and
* <code>A_<em>i</em> = A_<em>j</em></code> for all <em>i</em>, <em>j</em>.
*
* <p/>
*
* In other words, we continue examining lookahead until all {@code A_i}
* have more than one alternative and all {@code A_i} are the same. If
* {@code A={{1,2}, {1,3}}}, then regular LL prediction would terminate
* because the resolved set is {@code {1}}. To determine what the real
* ambiguity is, we have to know whether the ambiguity is between one and
* two or one and three so we keep going. We can only stop prediction when
* we need exact ambiguity detection when the sets look like
* {@code A={{1,2}}} or {@code {{1,2},{1,2}}}, etc...
*/ */
public static int resolvesToJustOneViableAlt(Collection<BitSet> altsets) { public static int resolvesToJustOneViableAlt(@NotNull Collection<BitSet> altsets) {
return getSingleViableAlt(altsets); return getSingleViableAlt(altsets);
} }
public static boolean allSubsetsConflict(Collection<BitSet> altsets) { /**
* Determines if every alternative subset in {@code altsets} contains more
* than one alternative.
*
* @param altsets a collection of alternative subsets
* @return {@code true} if every {@link BitSet} in {@code altsets} has
* {@link BitSet#cardinality cardinality} &gt; 1, otherwise {@code false}
*/
public static boolean allSubsetsConflict(@NotNull Collection<BitSet> altsets) {
return !hasNonConflictingAltSet(altsets); return !hasNonConflictingAltSet(altsets);
} }
/** return (there exists len(A_i)==1 for some A_i in altsets A) */ /**
public static boolean hasNonConflictingAltSet(Collection<BitSet> altsets) { * Determines if any single alternative subset in {@code altsets} contains
* exactly one alternative.
*
* @param altsets a collection of alternative subsets
* @return {@code true} if {@code altsets} contains a {@link BitSet} with
* {@link BitSet#cardinality cardinality} 1, otherwise {@code false}
*/
public static boolean hasNonConflictingAltSet(@NotNull Collection<BitSet> altsets) {
for (BitSet alts : altsets) { for (BitSet alts : altsets) {
if ( alts.cardinality()==1 ) { if ( alts.cardinality()==1 ) {
return true; return true;
@ -405,8 +517,15 @@ public enum PredictionMode {
return false; return false;
} }
/** return (there exists len(A_i)>1 for some A_i in altsets A) */ /**
public static boolean hasConflictingAltSet(Collection<BitSet> altsets) { * Determines if any single alternative subset in {@code altsets} contains
* more than one alternative.
*
* @param altsets a collection of alternative subsets
* @return {@code true} if {@code altsets} contains a {@link BitSet} with
* {@link BitSet#cardinality cardinality} &gt; 1, otherwise {@code false}
*/
public static boolean hasConflictingAltSet(@NotNull Collection<BitSet> altsets) {
for (BitSet alts : altsets) { for (BitSet alts : altsets) {
if ( alts.cardinality()>1 ) { if ( alts.cardinality()>1 ) {
return true; return true;
@ -415,7 +534,14 @@ public enum PredictionMode {
return false; return false;
} }
public static boolean allSubsetsEqual(Collection<BitSet> altsets) { /**
* Determines if every alternative subset in {@code altsets} is equivalent.
*
* @param altsets a collection of alternative subsets
* @return {@code true} if every member of {@code altsets} is equal to the
* others, otherwise {@code false}
*/
public static boolean allSubsetsEqual(@NotNull Collection<BitSet> altsets) {
Iterator<BitSet> it = altsets.iterator(); Iterator<BitSet> it = altsets.iterator();
BitSet first = it.next(); BitSet first = it.next();
while ( it.hasNext() ) { while ( it.hasNext() ) {
@ -425,14 +551,28 @@ public enum PredictionMode {
return true; return true;
} }
/**
public static int getUniqueAlt(Collection<BitSet> altsets) { * Returns the unique alternative predicted by all alternative subsets in
* {@code altsets}. If no such alternative exists, this method returns
* {@link ATN#INVALID_ALT_NUMBER}.
*
* @param altsets a collection of alternative subsets
*/
public static int getUniqueAlt(@NotNull Collection<BitSet> altsets) {
BitSet all = getAlts(altsets); BitSet all = getAlts(altsets);
if ( all.cardinality()==1 ) return all.nextSetBit(0); if ( all.cardinality()==1 ) return all.nextSetBit(0);
return ATN.INVALID_ALT_NUMBER; return ATN.INVALID_ALT_NUMBER;
} }
public static BitSet getAlts(Collection<BitSet> altsets) { /**
* Gets the complete set of represented alternatives for a collection of
* alternative subsets. This method returns the union of each {@link BitSet}
* in {@code altsets}.
*
* @param altsets a collection of alternative subsets
* @return the set of represented alternatives in {@code altsets}
*/
public static BitSet getAlts(@NotNull Collection<BitSet> altsets) {
BitSet all = new BitSet(); BitSet all = new BitSet();
for (BitSet alts : altsets) { for (BitSet alts : altsets) {
all.or(alts); all.or(alts);
@ -441,10 +581,15 @@ public enum PredictionMode {
} }
/** /**
* This function gets the conflicting alt subsets from a configuration set. * This function gets the conflicting alt subsets from a configuration set.
* for c in configs: * For each configuration {@code c} in {@code configs}:
* map[c] U= c.alt # map hash/equals uses s and x, not alt and not pred *
*/ * <pre>
* map[c] U= c.{@link ATNConfig#alt alt} # map hash/equals uses s and x, not
* alt and not pred
* </pre>
*/
@NotNull
public static Collection<BitSet> getConflictingAltSubsets(ATNConfigSet configs) { public static Collection<BitSet> getConflictingAltSubsets(ATNConfigSet configs) {
AltAndContextMap configToAlts = new AltAndContextMap(); AltAndContextMap configToAlts = new AltAndContextMap();
for (ATNConfig c : configs) { for (ATNConfig c : configs) {
@ -458,11 +603,16 @@ public enum PredictionMode {
return configToAlts.values(); return configToAlts.values();
} }
/** Get a map from state to alt subset from a configuration set. /**
* for c in configs: * Get a map from state to alt subset from a configuration set. For each
* map[c.state] U= c.alt * configuration {@code c} in {@code configs}:
*
* <pre>
* map[c.{@link ATNConfig#state state}] U= c.{@link ATNConfig#alt alt}
* </pre>
*/ */
public static Map<ATNState, BitSet> getStateToAltMap(ATNConfigSet configs) { @NotNull
public static Map<ATNState, BitSet> getStateToAltMap(@NotNull ATNConfigSet configs) {
Map<ATNState, BitSet> m = new HashMap<ATNState, BitSet>(); Map<ATNState, BitSet> m = new HashMap<ATNState, BitSet>();
for (ATNConfig c : configs) { for (ATNConfig c : configs) {
BitSet alts = m.get(c.state); BitSet alts = m.get(c.state);
@ -475,7 +625,7 @@ public enum PredictionMode {
return m; return m;
} }
public static boolean hasStateAssociatedWithOneAlt(ATNConfigSet configs) { public static boolean hasStateAssociatedWithOneAlt(@NotNull ATNConfigSet configs) {
Map<ATNState, BitSet> x = getStateToAltMap(configs); Map<ATNState, BitSet> x = getStateToAltMap(configs);
for (BitSet alts : x.values()) { for (BitSet alts : x.values()) {
if ( alts.cardinality()==1 ) return true; if ( alts.cardinality()==1 ) return true;
@ -483,7 +633,7 @@ public enum PredictionMode {
return false; return false;
} }
public static int getSingleViableAlt(Collection<BitSet> altsets) { public static int getSingleViableAlt(@NotNull Collection<BitSet> altsets) {
BitSet viableAlts = new BitSet(); BitSet viableAlts = new BitSet();
for (BitSet alts : altsets) { for (BitSet alts : altsets) {
int minAlt = alts.nextSetBit(0); int minAlt = alts.nextSetBit(0);