public virtual int DefineDecisionState(DecisionState s) { decisionToState.Add(s); s.decision = decisionToState.Count - 1; decisionToDFA = Arrays.CopyOf(decisionToDFA, decisionToState.Count); decisionToDFA[decisionToDFA.Length - 1] = new DFA(s, s.decision); return(s.decision); }
public DFA(DecisionState atnStartState, int decision) { this.atnStartState = atnStartState; this.decision = decision; this.precedenceDfa = false; if (atnStartState is StarLoopEntryState && ((StarLoopEntryState)atnStartState).isPrecedenceDecision) { this.precedenceDfa = true; DFAState precedenceState = new DFAState(new ATNConfigSet()); precedenceState.edges = new DFAState[0]; precedenceState.isAcceptState = false; precedenceState.requiresFullContext = false; this.s0 = precedenceState; } }
protected internal virtual void ReadDecisions(ATN atn) { // // DECISIONS // int ndecisions = ReadInt(); for (int i_11 = 0; i_11 < ndecisions; i_11++) { int s = ReadInt(); DecisionState decState = (DecisionState)atn.states[s]; atn.decisionToState.Add(decState); decState.decision = i_11; } atn.decisionToDFA = new DFA[ndecisions]; for (int i_12 = 0; i_12 < ndecisions; i_12++) { atn.decisionToDFA[i_12] = new DFA(atn.decisionToState[i_12], i_12); } }
protected internal virtual void VerifyATN(ATN atn) { // verify assumptions foreach (ATNState state in atn.states) { if (state == null) { continue; } CheckCondition(state.OnlyHasEpsilonTransitions || state.NumberOfTransitions <= 1); if (state is PlusBlockStartState) { CheckCondition(((PlusBlockStartState)state).loopBackState != null); } if (state is StarLoopEntryState) { StarLoopEntryState starLoopEntryState = (StarLoopEntryState)state; CheckCondition(starLoopEntryState.loopBackState != null); CheckCondition(starLoopEntryState.NumberOfTransitions == 2); if (starLoopEntryState.Transition(0).target is StarBlockStartState) { CheckCondition(starLoopEntryState.Transition(1).target is LoopEndState); CheckCondition(!starLoopEntryState.nonGreedy); } else { if (starLoopEntryState.Transition(0).target is LoopEndState) { CheckCondition(starLoopEntryState.Transition(1).target is StarBlockStartState); CheckCondition(starLoopEntryState.nonGreedy); } else { throw new InvalidOperationException(); } } } if (state is StarLoopbackState) { CheckCondition(state.NumberOfTransitions == 1); CheckCondition(state.Transition(0).target is StarLoopEntryState); } if (state is LoopEndState) { CheckCondition(((LoopEndState)state).loopBackState != null); } if (state is RuleStartState) { CheckCondition(((RuleStartState)state).stopState != null); } if (state is BlockStartState) { CheckCondition(((BlockStartState)state).endState != null); } if (state is BlockEndState) { CheckCondition(((BlockEndState)state).startState != null); } if (state is DecisionState) { DecisionState decisionState = (DecisionState)state; CheckCondition(decisionState.NumberOfTransitions <= 1 || decisionState.decision >= 0); } else { CheckCondition(state.NumberOfTransitions <= 1 || state is RuleStopState); } } }
public virtual int DefineDecisionState(DecisionState s) { decisionToState.Add(s); s.decision = decisionToState.Count - 1; decisionToDFA = Arrays.CopyOf(decisionToDFA, decisionToState.Count); decisionToDFA[decisionToDFA.Length - 1] = new DFA(s, s.decision); return s.decision; }
/// <summary> /// Serialize state descriptors, edge descriptors, and decision→state map /// into list of ints: /// grammar-type, (ANTLRParser.LEXER, ...) /// max token type, /// num states, /// state-0-type ruleIndex, state-1-type ruleIndex, ... /// </summary> /// <remarks> /// Serialize state descriptors, edge descriptors, and decision→state map /// into list of ints: /// grammar-type, (ANTLRParser.LEXER, ...) /// max token type, /// num states, /// state-0-type ruleIndex, state-1-type ruleIndex, ... state-i-type ruleIndex optional-arg ... /// num rules, /// rule-1-start-state rule-1-args, rule-2-start-state rule-2-args, ... /// (args are token type,actionIndex in lexer else 0,0) /// num modes, /// mode-0-start-state, mode-1-start-state, ... (parser has 0 modes) /// num sets /// set-0-interval-count intervals, set-1-interval-count intervals, ... /// num total edges, /// src, trg, edge-type, edge arg1, optional edge arg2 (present always), ... /// num decisions, /// decision-0-start-state, decision-1-start-state, ... /// Convenient to pack into unsigned shorts to make as Java string. /// </remarks> public virtual List <int> Serialize() { List <int> data = new List <int>(); data.Add(ATNDeserializer.SerializedVersion); SerializeUUID(data, ATNDeserializer.SerializedUuid); // convert grammar type to ATN const to avoid dependence on ANTLRParser data.Add((int)(atn.grammarType)); data.Add(atn.maxTokenType); int nedges = 0; IDictionary <IntervalSet, int> setIndices = new Dictionary <IntervalSet, int>(); IList <IntervalSet> sets = new List <IntervalSet>(); // dump states, count edges and collect sets while doing so List <int> nonGreedyStates = new List <int>(); List <int> sllStates = new List <int>(); List <int> precedenceStates = new List <int>(); data.Add(atn.states.Count); foreach (ATNState s in atn.states) { if (s == null) { // might be optimized away data.Add((int)(StateType.InvalidType)); continue; } StateType stateType = s.StateType; if (s is DecisionState) { DecisionState decisionState = (DecisionState)s; if (decisionState.nonGreedy) { nonGreedyStates.Add(s.stateNumber); } if (decisionState.sll) { sllStates.Add(s.stateNumber); } } if (s is RuleStartState && ((RuleStartState)s).isPrecedenceRule) { precedenceStates.Add(s.stateNumber); } data.Add((int)(stateType)); if (s.ruleIndex == -1) { data.Add(char.MaxValue); } else { data.Add(s.ruleIndex); } if (s.StateType == StateType.LoopEnd) { data.Add(((LoopEndState)s).loopBackState.stateNumber); } else { if (s is BlockStartState) { data.Add(((BlockStartState)s).endState.stateNumber); } } if (s.StateType != StateType.RuleStop) { // the deserializer can trivially derive these edges, so there's no need to serialize them nedges += s.NumberOfTransitions; } for (int i = 0; i < s.NumberOfTransitions; i++) { Transition t = s.Transition(i); TransitionType edgeType = Transition.serializationTypes.Get(t.GetType()); if (edgeType == TransitionType.Set || edgeType == TransitionType.NotSet) { SetTransition st = (SetTransition)t; if (!setIndices.ContainsKey(st.set)) { sets.AddItem(st.set); setIndices.Put(st.set, sets.Count - 1); } } } } // non-greedy states data.Add(nonGreedyStates.Size()); for (int i_1 = 0; i_1 < nonGreedyStates.Size(); i_1++) { data.Add(nonGreedyStates.Get(i_1)); } // SLL decisions data.Add(sllStates.Size()); for (int i_2 = 0; i_2 < sllStates.Size(); i_2++) { data.Add(sllStates.Get(i_2)); } // precedence states data.Add(precedenceStates.Size()); for (int i_3 = 0; i_3 < precedenceStates.Size(); i_3++) { data.Add(precedenceStates.Get(i_3)); } int nrules = atn.ruleToStartState.Length; data.Add(nrules); for (int r = 0; r < nrules; r++) { ATNState ruleStartState = atn.ruleToStartState[r]; data.Add(ruleStartState.stateNumber); bool leftFactored = ruleNames[ruleStartState.ruleIndex].IndexOf(ATNSimulator.RuleVariantDelimiter) >= 0; data.Add(leftFactored ? 1 : 0); if (atn.grammarType == ATNType.Lexer) { if (atn.ruleToTokenType[r] == TokenConstants.Eof) { data.Add(char.MaxValue); } else { data.Add(atn.ruleToTokenType[r]); } } } int nmodes = atn.modeToStartState.Count; data.Add(nmodes); if (nmodes > 0) { foreach (ATNState modeStartState in atn.modeToStartState) { data.Add(modeStartState.stateNumber); } } int nsets = sets.Count; data.Add(nsets); foreach (IntervalSet set in sets) { bool containsEof = set.Contains(TokenConstants.Eof); if (containsEof && set.GetIntervals()[0].b == TokenConstants.Eof) { data.Add(set.GetIntervals().Count - 1); } else { data.Add(set.GetIntervals().Count); } data.Add(containsEof ? 1 : 0); foreach (Interval I in set.GetIntervals()) { if (I.a == TokenConstants.Eof) { if (I.b == TokenConstants.Eof) { continue; } else { data.Add(0); } } else { data.Add(I.a); } data.Add(I.b); } } data.Add(nedges); foreach (ATNState s_1 in atn.states) { if (s_1 == null) { // might be optimized away continue; } if (s_1.StateType == StateType.RuleStop) { continue; } for (int i = 0; i_3 < s_1.NumberOfTransitions; i_3++) { Transition t = s_1.Transition(i_3); if (atn.states[t.target.stateNumber] == null) { throw new InvalidOperationException("Cannot serialize a transition to a removed state."); } int src = s_1.stateNumber; int trg = t.target.stateNumber; TransitionType edgeType = Transition.serializationTypes.Get(t.GetType()); int arg1 = 0; int arg2 = 0; int arg3 = 0; switch (edgeType) { case TransitionType.Rule: { trg = ((RuleTransition)t).followState.stateNumber; arg1 = ((RuleTransition)t).target.stateNumber; arg2 = ((RuleTransition)t).ruleIndex; arg3 = ((RuleTransition)t).precedence; break; } case TransitionType.Precedence: { PrecedencePredicateTransition ppt = (PrecedencePredicateTransition)t; arg1 = ppt.precedence; break; } case TransitionType.Predicate: { PredicateTransition pt = (PredicateTransition)t; arg1 = pt.ruleIndex; arg2 = pt.predIndex; arg3 = pt.isCtxDependent ? 1 : 0; break; } case TransitionType.Range: { arg1 = ((RangeTransition)t).from; arg2 = ((RangeTransition)t).to; if (arg1 == TokenConstants.Eof) { arg1 = 0; arg3 = 1; } break; } case TransitionType.Atom: { arg1 = ((AtomTransition)t).label; if (arg1 == TokenConstants.Eof) { arg1 = 0; arg3 = 1; } break; } case TransitionType.Action: { ActionTransition at = (ActionTransition)t; arg1 = at.ruleIndex; arg2 = at.actionIndex; if (arg2 == -1) { arg2 = unchecked ((int)(0xFFFF)); } arg3 = at.isCtxDependent ? 1 : 0; break; } case TransitionType.Set: { arg1 = setIndices.Get(((SetTransition)t).set); break; } case TransitionType.NotSet: { arg1 = setIndices.Get(((SetTransition)t).set); break; } case TransitionType.Wildcard: { break; } } data.Add(src); data.Add(trg); data.Add((int)(edgeType)); data.Add(arg1); data.Add(arg2); data.Add(arg3); } } int ndecisions = atn.decisionToState.Count; data.Add(ndecisions); foreach (DecisionState decStartState in atn.decisionToState) { data.Add(decStartState.stateNumber); } // // LEXER ACTIONS // if (atn.grammarType == ATNType.Lexer) { data.Add(atn.lexerActions.Length); foreach (ILexerAction action in atn.lexerActions) { data.Add((int)(action.GetActionType())); switch (action.GetActionType()) { case LexerActionType.Channel: { int channel = ((LexerChannelAction)action).GetChannel(); data.Add(channel != -1 ? channel : unchecked ((int)(0xFFFF))); data.Add(0); break; } case LexerActionType.Custom: { int ruleIndex = ((LexerCustomAction)action).GetRuleIndex(); int actionIndex = ((LexerCustomAction)action).GetActionIndex(); data.Add(ruleIndex != -1 ? ruleIndex : unchecked ((int)(0xFFFF))); data.Add(actionIndex != -1 ? actionIndex : unchecked ((int)(0xFFFF))); break; } case LexerActionType.Mode: { int mode = ((LexerModeAction)action).GetMode(); data.Add(mode != -1 ? mode : unchecked ((int)(0xFFFF))); data.Add(0); break; } case LexerActionType.More: { data.Add(0); data.Add(0); break; } case LexerActionType.PopMode: { data.Add(0); data.Add(0); break; } case LexerActionType.PushMode: { mode = ((LexerPushModeAction)action).GetMode(); data.Add(mode != -1 ? mode : unchecked ((int)(0xFFFF))); data.Add(0); break; } case LexerActionType.Skip: { data.Add(0); data.Add(0); break; } case LexerActionType.Type: { int type = ((LexerTypeAction)action).GetType(); data.Add(type != -1 ? type : unchecked ((int)(0xFFFF))); data.Add(0); break; } default: { string message = string.Format(CultureInfo.CurrentCulture, "The specified lexer action type {0} is not valid.", action.GetActionType()); throw new ArgumentException(message); } } } } // don't adjust the first value since that's the version number for (int i_4 = 1; i_4 < data.Size(); i_4++) { if (data.Get(i_4) < char.MinValue || data.Get(i_4) > char.MaxValue) { throw new NotSupportedException("Serialized ATN data element out of range."); } int value = (data.Get(i_4) + 2) & unchecked ((int)(0xFFFF)); data.Set(i_4, value); } return(data); }
protected void PredicateDFAState(DFAState dfaState, DecisionState decisionState) { // We need to test all predicates, even in DFA states that // uniquely predict alternative. int nalts = decisionState.NumberOfTransitions; // Update DFA so reach becomes accept state with (predicate,alt) // pairs if preds found for conflicting alts BitSet altsToCollectPredsFrom = GetConflictingAltsOrUniqueAlt(dfaState.configSet); SemanticContext[] altToPred = GetPredsForAmbigAlts(altsToCollectPredsFrom, dfaState.configSet, nalts); if (altToPred != null) { dfaState.predicates = GetPredicatePredictions(altsToCollectPredsFrom, altToPred); dfaState.prediction = ATN.INVALID_ALT_NUMBER; // make sure we use preds } else { // There are preds in configs but they might go away // when OR'd together like {p}? || NONE == NONE. If neither // alt has preds, resolve to min alt dfaState.prediction = altsToCollectPredsFrom.NextSetBit(0); } }
/** Override this method so that we can record which alternative * was taken at each decision point. For non-left recursive rules, * it's simple. Set decisionStatesThatSetOuterAltNumInContext * indicates which decision states should set the outer alternative number. * * <p>Left recursive rules are much more complicated to deal with: * there is typically a decision for the primary alternatives and a * decision to choose between the recursive operator alternatives. * For example, the following left recursive rule has two primary and 2 * recursive alternatives.</p> * e : e '*' e | '-' INT | e '+' e | ID ; * <p>ANTLR rewrites that rule to be</p> e[int precedence] : ('-' INT | ID) ( {...}? '*' e[5] | {...}? '+' e[3] )* ; * * <p>So, there are two decisions associated with picking the outermost alt. * This complicates our tracking significantly. The outermost alternative number * is a function of the decision (ATN state) within a left recursive rule and the * predicted alternative coming back from adaptivePredict().</p> * * We use stateToAltsMap as a cache to avoid expensive calls to * getRecursiveOpAlts(). */ protected override int VisitDecisionState(DecisionState p) { int predictedAlt = base.VisitDecisionState(p); if (p.NumberOfTransitions > 1) { // System.out.println("decision "+p.decision+": "+predictedAlt); if (p.decision == this.overrideDecision && this._input.Index == this.overrideDecisionInputIndex) { overrideDecisionRoot = (GrammarInterpreterRuleContext)Context; } } GrammarInterpreterRuleContext ctx = (GrammarInterpreterRuleContext)_ctx; if (decisionStatesThatSetOuterAltNumInContext.Get(p.stateNumber)) { ctx.OuterAlternative = predictedAlt; Rule r = g.GetRule(p.ruleIndex); if (atn.ruleToStartState[r.index].isPrecedenceRule) { int[] alts = stateToAltsMap[p.stateNumber]; LeftRecursiveRule lr = (LeftRecursiveRule)g.GetRule(p.ruleIndex); if (p.StateType == StateType.BlockStart) { if (alts == null) { alts = lr.GetPrimaryAlts(); stateToAltsMap[p.stateNumber] = alts; // cache it } } else if (p.StateType == StateType.StarBlockStart) { if (alts == null) { alts = lr.GetRecursiveOpAlts(); stateToAltsMap[p.stateNumber] = alts; // cache it } } ctx.OuterAlternative = alts[predictedAlt]; } } return predictedAlt; }
public static ATN Deserialize(char[] data, bool optimize) { data = (char[])data.Clone(); // don't adjust the first value since that's the version number for (int i = 1; i < data.Length; i++) { data[i] = (char)(data[i] - 2); } int p = 0; int version = ToInt(data[p++]); if (version != SerializedVersion) { string reason = string.Format(CultureInfo.CurrentCulture, "Could not deserialize ATN with version {0} (expected {1})." , version, SerializedVersion); throw new NotSupportedException(reason); } Guid uuid = ToUUID(data, p); p += 8; if (!uuid.Equals(SerializedUuid)) { string reason = string.Format(CultureInfo.CurrentCulture, "Could not deserialize ATN with UUID {0} (expected {1})." , uuid, SerializedUuid); throw new NotSupportedException(reason); } ATNType grammarType = (ATNType)ToInt(data[p++]); int maxTokenType = ToInt(data[p++]); ATN atn = new ATN(grammarType, maxTokenType); // // STATES // IList <Tuple <LoopEndState, int> > loopBackStateNumbers = new List <Tuple <LoopEndState , int> >(); IList <Tuple <BlockStartState, int> > endStateNumbers = new List <Tuple <BlockStartState , int> >(); int nstates = ToInt(data[p++]); for (int i_1 = 0; i_1 < nstates; i_1++) { StateType stype = (StateType)ToInt(data[p++]); // ignore bad type of states if (stype == StateType.InvalidType) { atn.AddState(null); continue; } int ruleIndex = ToInt(data[p++]); if (ruleIndex == char.MaxValue) { ruleIndex = -1; } ATNState s = StateFactory(stype, ruleIndex); if (stype == StateType.LoopEnd) { // special case int loopBackStateNumber = ToInt(data[p++]); loopBackStateNumbers.Add(Tuple.Create((LoopEndState)s, loopBackStateNumber)); } else { if (s is BlockStartState) { int endStateNumber = ToInt(data[p++]); endStateNumbers.Add(Tuple.Create((BlockStartState)s, endStateNumber)); } } atn.AddState(s); } // delay the assignment of loop back and end states until we know all the state instances have been initialized foreach (Tuple <LoopEndState, int> pair in loopBackStateNumbers) { pair.Item1.loopBackState = atn.states[pair.Item2]; } foreach (Tuple <BlockStartState, int> pair_1 in endStateNumbers) { pair_1.Item1.endState = (BlockEndState)atn.states[pair_1.Item2]; } int numNonGreedyStates = ToInt(data[p++]); for (int i_2 = 0; i_2 < numNonGreedyStates; i_2++) { int stateNumber = ToInt(data[p++]); ((DecisionState)atn.states[stateNumber]).nonGreedy = true; } int numSllDecisions = ToInt(data[p++]); for (int i_3 = 0; i_3 < numSllDecisions; i_3++) { int stateNumber = ToInt(data[p++]); ((DecisionState)atn.states[stateNumber]).sll = true; } int numPrecedenceStates = ToInt(data[p++]); for (int i_4 = 0; i_4 < numPrecedenceStates; i_4++) { int stateNumber = ToInt(data[p++]); ((RuleStartState)atn.states[stateNumber]).isPrecedenceRule = true; } // // RULES // int nrules = ToInt(data[p++]); if (atn.grammarType == ATNType.Lexer) { atn.ruleToTokenType = new int[nrules]; atn.ruleToActionIndex = new int[nrules]; } atn.ruleToStartState = new RuleStartState[nrules]; for (int i_5 = 0; i_5 < nrules; i_5++) { int s = ToInt(data[p++]); RuleStartState startState = (RuleStartState)atn.states[s]; startState.leftFactored = ToInt(data[p++]) != 0; atn.ruleToStartState[i_5] = startState; if (atn.grammarType == ATNType.Lexer) { int tokenType = ToInt(data[p++]); if (tokenType == unchecked ((int)(0xFFFF))) { tokenType = TokenConstants.Eof; } atn.ruleToTokenType[i_5] = tokenType; int actionIndex = ToInt(data[p++]); if (actionIndex == unchecked ((int)(0xFFFF))) { actionIndex = -1; } atn.ruleToActionIndex[i_5] = actionIndex; } } atn.ruleToStopState = new RuleStopState[nrules]; foreach (ATNState state in atn.states) { if (!(state is RuleStopState)) { continue; } RuleStopState stopState = (RuleStopState)state; atn.ruleToStopState[state.ruleIndex] = stopState; atn.ruleToStartState[state.ruleIndex].stopState = stopState; } // // MODES // int nmodes = ToInt(data[p++]); for (int i_6 = 0; i_6 < nmodes; i_6++) { int s = ToInt(data[p++]); atn.modeToStartState.Add((TokensStartState)atn.states[s]); } atn.modeToDFA = new DFA[nmodes]; for (int i_7 = 0; i_7 < nmodes; i_7++) { atn.modeToDFA[i_7] = new DFA(atn.modeToStartState[i_7]); } // // SETS // IList <IntervalSet> sets = new List <IntervalSet>(); int nsets = ToInt(data[p++]); for (int i_8 = 0; i_8 < nsets; i_8++) { int nintervals = ToInt(data[p]); p++; IntervalSet set = new IntervalSet(); sets.Add(set); bool containsEof = ToInt(data[p++]) != 0; if (containsEof) { set.Add(-1); } for (int j = 0; j < nintervals; j++) { set.Add(ToInt(data[p]), ToInt(data[p + 1])); p += 2; } } // // EDGES // int nedges = ToInt(data[p++]); for (int i_9 = 0; i_9 < nedges; i_9++) { int src = ToInt(data[p]); int trg = ToInt(data[p + 1]); TransitionType ttype = (TransitionType)ToInt(data[p + 2]); int arg1 = ToInt(data[p + 3]); int arg2 = ToInt(data[p + 4]); int arg3 = ToInt(data[p + 5]); Transition trans = EdgeFactory(atn, ttype, src, trg, arg1, arg2, arg3, sets); // System.out.println("EDGE "+trans.getClass().getSimpleName()+" "+ // src+"->"+trg+ // " "+Transition.serializationNames[ttype]+ // " "+arg1+","+arg2+","+arg3); ATNState srcState = atn.states[src]; srcState.AddTransition(trans); p += 6; } // edges for rule stop states can be derived, so they aren't serialized foreach (ATNState state_1 in atn.states) { bool returningToLeftFactored = state_1.ruleIndex >= 0 && atn.ruleToStartState[state_1 .ruleIndex].leftFactored; for (int i_10 = 0; i_10 < state_1.NumberOfTransitions; i_10++) { Transition t = state_1.Transition(i_10); if (!(t is RuleTransition)) { continue; } RuleTransition ruleTransition = (RuleTransition)t; bool returningFromLeftFactored = atn.ruleToStartState[ruleTransition.target.ruleIndex ].leftFactored; if (!returningFromLeftFactored && returningToLeftFactored) { continue; } atn.ruleToStopState[ruleTransition.target.ruleIndex].AddTransition(new EpsilonTransition (ruleTransition.followState)); } } foreach (ATNState state_2 in atn.states) { if (state_2 is BlockStartState) { // we need to know the end state to set its start state if (((BlockStartState)state_2).endState == null) { throw new InvalidOperationException(); } // block end states can only be associated to a single block start state if (((BlockStartState)state_2).endState.startState != null) { throw new InvalidOperationException(); } ((BlockStartState)state_2).endState.startState = (BlockStartState)state_2; } if (state_2 is PlusLoopbackState) { PlusLoopbackState loopbackState = (PlusLoopbackState)state_2; for (int i_10 = 0; i_10 < loopbackState.NumberOfTransitions; i_10++) { ATNState target = loopbackState.Transition(i_10).target; if (target is PlusBlockStartState) { ((PlusBlockStartState)target).loopBackState = loopbackState; } } } else { if (state_2 is StarLoopbackState) { StarLoopbackState loopbackState = (StarLoopbackState)state_2; for (int i_10 = 0; i_10 < loopbackState.NumberOfTransitions; i_10++) { ATNState target = loopbackState.Transition(i_10).target; if (target is StarLoopEntryState) { ((StarLoopEntryState)target).loopBackState = loopbackState; } } } } } // // DECISIONS // int ndecisions = ToInt(data[p++]); for (int i_11 = 1; i_11 <= ndecisions; i_11++) { int s = ToInt(data[p++]); DecisionState decState = (DecisionState)atn.states[s]; atn.decisionToState.Add(decState); decState.decision = i_11 - 1; } atn.decisionToDFA = new DFA[ndecisions]; for (int i_12 = 0; i_12 < ndecisions; i_12++) { atn.decisionToDFA[i_12] = new DFA(atn.decisionToState[i_12], i_12); } if (optimize) { while (true) { int optimizationCount = 0; optimizationCount += InlineSetRules(atn); optimizationCount += CombineChainedEpsilons(atn); bool preserveOrder = atn.grammarType == ATNType.Lexer; optimizationCount += OptimizeSets(atn, preserveOrder); if (optimizationCount == 0) { break; } } } IdentifyTailCalls(atn); VerifyATN(atn); return(atn); }
/// <summary> /// Method visitDecisionState() is called when the interpreter reaches /// a decision state (instance of DecisionState). /// </summary> /// <remarks> /// Method visitDecisionState() is called when the interpreter reaches /// a decision state (instance of DecisionState). It gives an opportunity /// for subclasses to track interesting things. /// </remarks> protected internal virtual int VisitDecisionState(DecisionState p) { int edge = 1; int predictedAlt; ErrorHandler.Sync(this); int decision = p.decision; if (decision == overrideDecision && _input.Index == overrideDecisionInputIndex && !overrideDecisionReached) { predictedAlt = overrideDecisionAlt; overrideDecisionReached = true; } else { predictedAlt = Interpreter.AdaptivePredict(_input, decision, _ctx); } return predictedAlt; }
public DFA(DecisionState atnStartState) : this(atnStartState, 0) { }