public virtual NFAConfiguration AddNFAConfiguration( NFAState state, int alt, NFAContext context, SemanticContext semanticContext ) { NFAConfiguration c = new NFAConfiguration( state.stateNumber, alt, context, semanticContext ); AddNFAConfiguration( state, c ); return c; }
/** Add an NFA configuration to this DFA node. Add uniquely * an NFA state/alt/syntactic&semantic context (chain of invoking state(s) * and semantic predicate contexts). * * I don't see how there could be two configurations with same * state|alt|synCtx and different semantic contexts because the * semantic contexts are computed along the path to a particular state * so those two configurations would have to have the same predicate. * Nonetheless, the addition of configurations is unique on all * configuration info. I guess I'm saying that syntactic context * implies semantic context as the latter is computed according to the * former. * * As we add configurations to this DFA state, track the set of all possible * transition labels so we can simply walk it later rather than doing a * loop over all possible labels in the NFA. */ public virtual void AddNFAConfiguration( NFAState state, NFAConfiguration c ) { if ( nfaConfigurations.Contains( c ) ) { return; } nfaConfigurations.Add( c ); // track min alt rather than compute later if ( c.alt < minAltInConfigurations ) { minAltInConfigurations = c.alt; } if ( c.semanticContext != SemanticContext.EmptySemanticContext ) { _atLeastOneConfigurationHasAPredicate = true; } // update hashCode; for some reason using context.hashCode() also // makes the GC take like 70% of the CPU and is slow! _cachedHashCode += c.state + c.alt; // update reachableLabels // We're adding an NFA state; check to see if it has a non-epsilon edge if ( state.transition[0] != null ) { Label label = state.transition[0].label; if ( !( label.IsEpsilon || label.IsSemanticPredicate ) ) { // this NFA state has a non-epsilon edge, track for fast // walking later when we do reach on this DFA state we're // building. configurationsWithLabeledEdges.Add( c ); if ( state.transition[1] == null ) { // later we can check this to ignore o-A->o states in closure c.singleAtomTransitionEmanating = true; } AddReachableLabel( label ); } } }
protected virtual string GetStateLabel(State s) { if (s == null) { return("null"); } string stateLabel = s.StateNumber.ToString(); if (s is DFAState) { StringBuffer buf = new StringBuffer(250); buf.Append('s'); buf.Append(s.StateNumber); if (AntlrTool.internalOption_ShowNFAConfigsInDFA) { if (s is DFAState) { if (((DFAState)s).AbortedDueToRecursionOverflow) { buf.Append("\\n"); buf.Append("abortedDueToRecursionOverflow"); } } var alts = ((DFAState)s).AltSet; if (alts != null) { buf.Append("\\n"); // separate alts //List altList = new ArrayList(); //altList.addAll( alts ); //Collections.sort( altList ); List <int> altList = alts.OrderBy(i => i).ToList(); ICollection <NFAConfiguration> configurations = ((DFAState)s).NfaConfigurations; for (int altIndex = 0; altIndex < altList.Count; altIndex++) { object altI = altList[altIndex]; int alt = (int)altI; if (altIndex > 0) { buf.Append("\\n"); } buf.Append("alt"); buf.Append(alt); buf.Append(':'); // get a list of configs for just this alt // it will help us print better later IList <NFAConfiguration> configsInAlt = new List <NFAConfiguration>(); foreach (NFAConfiguration c in configurations) { if (c.Alt != alt) { continue; } configsInAlt.Add(c); } int n = 0; for (int cIndex = 0; cIndex < configsInAlt.Count; cIndex++) { NFAConfiguration c = configsInAlt[cIndex]; n++; buf.Append(c.ToString(false)); if ((cIndex + 1) < configsInAlt.Count) { buf.Append(", "); } if (n % 5 == 0 && (configsInAlt.Count - cIndex) > 3) { buf.Append("\\n"); } } } } } stateLabel = buf.ToString(); } if ((s is NFAState) && ((NFAState)s).IsDecisionState) { stateLabel = stateLabel + ",d=" + ((NFAState)s).DecisionNumber; if (((NFAState)s).endOfBlockStateNumber != State.INVALID_STATE_NUMBER) { stateLabel += ",eob=" + ((NFAState)s).endOfBlockStateNumber; } } else if ((s is NFAState) && ((NFAState)s).endOfBlockStateNumber != State.INVALID_STATE_NUMBER) { NFAState n = ((NFAState)s); stateLabel = stateLabel + ",eob=" + n.endOfBlockStateNumber; } else if (s is DFAState && ((DFAState)s).IsAcceptState) { stateLabel = stateLabel + "=>" + ((DFAState)s).GetUniquelyPredictedAlt(); } return('"' + stateLabel + '"'); }
private string GetStateLabel(State state) { if (state == null) { return("null"); } string stateLabel = state.StateNumber.ToString(); DFAState dfaState = state as DFAState; NFAState nfaState = state as NFAState; if (dfaState != null) { StringBuilder builder = new StringBuilder(250); builder.Append('s'); builder.Append(state.StateNumber); if (AntlrTool.internalOption_ShowNFAConfigsInDFA) { if (dfaState.AbortedDueToRecursionOverflow) { builder.AppendLine(); builder.AppendLine("AbortedDueToRecursionOverflow"); } var alts = dfaState.AltSet; if (alts != null) { builder.AppendLine(); List <int> altList = alts.OrderBy(i => i).ToList(); ICollection <NFAConfiguration> configurations = dfaState.NfaConfigurations; for (int i = 0; i < altList.Count; i++) { int alt = altList[i]; if (i > 0) { builder.AppendLine(); } builder.AppendFormat("alt{0}:", alt); // get a list of configs for just this alt // it will help us print better later List <NFAConfiguration> configsInAlt = new List <NFAConfiguration>(); foreach (NFAConfiguration c in configurations) { if (c.Alt != alt) { continue; } configsInAlt.Add(c); } int n = 0; for (int cIndex = 0; cIndex < configsInAlt.Count; cIndex++) { NFAConfiguration c = configsInAlt[cIndex]; n++; builder.Append(c.ToString(false)); if ((cIndex + 1) < configsInAlt.Count) { builder.Append(", "); } if (n % 5 == 0 && (configsInAlt.Count - cIndex) > 3) { builder.Append("\\n"); } } } } } if (dfaState.IsAcceptState) { builder.Append("⇒" + dfaState.GetUniquelyPredictedAlt()); } stateLabel = builder.ToString(); } else if (nfaState != null) { if (nfaState.IsDecisionState) { stateLabel += ",d=" + nfaState.DecisionNumber; } if (nfaState.endOfBlockStateNumber != State.INVALID_STATE_NUMBER) { stateLabel += ",eob=" + nfaState.endOfBlockStateNumber; } } return(stateLabel); }
public virtual void ReportRecursionOverflow( DFAState d, NFAConfiguration recursionNFAConfiguration ) { // track the state number rather than the state as d will change // out from underneath us; hash wouldn't return any value // left-recursion is detected in start state. Since we can't // call resolveNondeterminism() on the start state (it would // not look k=1 to get min single token lookahead), we must // prevent errors derived from this state. Avoid start state if ( d.StateNumber > 0 ) { int stateI = d.StateNumber; _stateToRecursionOverflowConfigurationsMap.Map( stateI, recursionNFAConfiguration ); } }
public AnalysisRecursionOverflowException(DFAState ovfState, NFAConfiguration proposedNFAConfiguration) { _ovfState = ovfState; _proposedNFAConfiguration = proposedNFAConfiguration; }