public virtual int GetUniquelyPredictedAlt()
{
if ( _cachedUniquelyPredicatedAlt.HasValue )
{
return _cachedUniquelyPredicatedAlt.Value;
}
int alt = NFA.INVALID_ALT_NUMBER;
int numConfigs = _nfaConfigurations.Count;
for ( int i = 0; i < numConfigs; i++ )
{
NFAConfiguration configuration = _nfaConfigurations[i];
// ignore anything we resolved; predicates will still result
// in transitions out of this state, so must count those
// configurations; i.e., don't ignore resolveWithPredicate configs
if ( configuration.Resolved )
{
continue;
}
if ( alt == NFA.INVALID_ALT_NUMBER )
{
alt = configuration.Alt; // found first nonresolved alt
}
else if ( configuration.Alt != alt )
{
return NFA.INVALID_ALT_NUMBER;
}
}
this._cachedUniquelyPredicatedAlt = alt;
return alt;
}
protected virtual void OptimizeExitBranches(DFAState d) { int sI = d.stateNumber; if (_visited.Contains(sI)) { return; // already visited } _visited.Add(sI); int nAlts = d.dfa.NumberOfAlts; for (int i = 0; i < d.NumberOfTransitions; i++) { Transition edge = (Transition)d.Transition(i); DFAState edgeTarget = ((DFAState)edge.target); /* * [email protected](d.stateNumber+"-"+ * edge.label.toString(d.dfa.nfa.grammar)+"->"+ * edgeTarget.stateNumber); */ // if target is an accept state and that alt is the exit alt if (edgeTarget.IsAcceptState && edgeTarget.GetUniquelyPredictedAlt() == nAlts) { /* * [email protected]("ignoring transition "+i+" to max alt "+ * d.dfa.getNumberOfAlts()); */ d.RemoveTransition(i); i--; // back up one so that i++ of loop iteration stays within bounds } OptimizeExitBranches(edgeTarget); } }