protected SemanticContext[] GetPredsForAmbigAlts(BitSet ambigAlts,
ATNConfigSet configSet,
int nalts)
{
// REACH=[1|1|[]|0:0, 1|2|[]|0:1]
/* altToPred starts as an array of all null contexts. The entry at index i
* corresponds to alternative i. altToPred[i] may have one of three values:
* 1. null: no ATNConfig c is found such that c.alt==i
* 2. SemanticContext.NONE: At least one ATNConfig c exists such that
* c.alt==i and c.semanticContext==SemanticContext.NONE. In other words,
* alt i has at least one unpredicated config.
* 3. Non-NONE Semantic Context: There exists at least one, and for all
* ATNConfig c such that c.alt==i, c.semanticContext!=SemanticContext.NONE.
*
* From this, it is clear that NONE||anything==NONE.
*/
SemanticContext[] altToPred = new SemanticContext[nalts + 1];
foreach (ATNConfig c in configSet.configs)
{
if (ambigAlts[c.alt])
{
altToPred[c.alt] = SemanticContext.OrOp(altToPred[c.alt], c.semanticContext);
}
}
int nPredAlts = 0;
for (int i = 1; i <= nalts; i++)
{
if (altToPred[i] == null)
{
altToPred[i] = SemanticContext.NONE;
}
else if (altToPred[i] != SemanticContext.NONE)
{
nPredAlts++;
}
}
// // Optimize away p||p and p&&p TODO: optimize() was a no-op
// for (int i = 0; i < altToPred.length; i++) {
// altToPred[i] = altToPred[i].optimize();
// }
// nonambig alts are null in altToPred
if (nPredAlts == 0) altToPred = null;
if (debug) Console.WriteLine("getPredsForAmbigAlts result " + Arrays.ToString(altToPred));
return altToPred;
}