/// <summary> /// Since the alternatives within any lexer decision are ordered by /// preference, this method stops pursuing the closure as soon as an accept /// state is reached. /// </summary> /// <remarks> /// Since the alternatives within any lexer decision are ordered by /// preference, this method stops pursuing the closure as soon as an accept /// state is reached. After the first accept state is reached by depth-first /// search from /// <code>config</code> /// , all other (potentially reachable) states for /// this rule would have a lower priority. /// </remarks> /// <returns> /// /// <code>true</code> /// if an accept state is reached, otherwise /// <code>false</code> /// . /// </returns> protected internal virtual bool Closure(ICharStream input, ATNConfig config, ATNConfigSet configs, bool speculative) { if (config.State is RuleStopState) { PredictionContext context = config.Context; if (context.IsEmpty) { configs.AddItem(config); return(true); } else { if (context.HasEmpty) { configs.AddItem(config.Transform(config.State, PredictionContext.EmptyFull)); return(true); } } for (int i = 0; i < context.Size; i++) { int returnStateNumber = context.GetReturnState(i); if (returnStateNumber == PredictionContext.EmptyFullStateKey) { continue; } PredictionContext newContext = context.GetParent(i); // "pop" return state ATNState returnState = atn.states[returnStateNumber]; ATNConfig c = ATNConfig.Create(returnState, config.Alt, newContext); if (Closure(input, c, configs, speculative)) { return(true); } } return(false); } // optimization if (!config.State.OnlyHasEpsilonTransitions) { configs.AddItem(config); } ATNState p = config.State; for (int i_1 = 0; i_1 < p.NumberOfOptimizedTransitions; i_1++) { Transition t = p.GetOptimizedTransition(i_1); ATNConfig c = GetEpsilonTarget(input, config, t, configs, speculative); if (c != null) { if (Closure(input, c, configs, speculative)) { return(true); } } } return(false); }
/// <summary> /// Since the alternatives within any lexer decision are ordered by /// preference, this method stops pursuing the closure as soon as an accept /// state is reached. /// </summary> /// <remarks> /// Since the alternatives within any lexer decision are ordered by /// preference, this method stops pursuing the closure as soon as an accept /// state is reached. After the first accept state is reached by depth-first /// search from /// <code>config</code> /// , all other (potentially reachable) states for /// this rule would have a lower priority. /// </remarks> /// <returns> /// /// <code>true</code> /// if an accept state is reached, otherwise /// <code>false</code> /// . /// </returns> protected internal virtual bool Closure(ICharStream input, ATNConfig config, ATNConfigSet configs, bool currentAltReachedAcceptState, bool speculative, bool treatEofAsEpsilon) { if (config.State is RuleStopState) { PredictionContext context = config.Context; if (context.IsEmpty) { configs.AddItem(config); return(true); } else { if (context.HasEmpty) { configs.AddItem(config.Transform(config.State, PredictionContext.EmptyFull, true)); currentAltReachedAcceptState = true; } } for (int i = 0; i < context.Size; i++) { int returnStateNumber = context.GetReturnState(i); if (returnStateNumber == PredictionContext.EmptyFullStateKey) { continue; } PredictionContext newContext = context.GetParent(i); // "pop" return state ATNState returnState = atn.states[returnStateNumber]; ATNConfig c = config.Transform(returnState, newContext, false); currentAltReachedAcceptState = Closure(input, c, configs, currentAltReachedAcceptState, speculative, treatEofAsEpsilon); } return(currentAltReachedAcceptState); } // optimization if (!config.State.OnlyHasEpsilonTransitions) { if (!currentAltReachedAcceptState || !config.HasPassedThroughNonGreedyDecision()) { configs.AddItem(config); } } ATNState p = config.State; for (int i_1 = 0; i_1 < p.NumberOfOptimizedTransitions; i_1++) { Transition t = p.GetOptimizedTransition(i_1); ATNConfig c = GetEpsilonTarget(input, config, t, configs, speculative, treatEofAsEpsilon); if (c != null) { currentAltReachedAcceptState = Closure(input, c, configs, currentAltReachedAcceptState, speculative, treatEofAsEpsilon); } } return(currentAltReachedAcceptState); }
/// <summary>Computes the SLL prediction termination condition.</summary> /// <remarks> /// Computes the SLL prediction termination condition. /// <p> /// This method computes the SLL prediction termination condition for both of /// the following cases.</p> /// <ul> /// <li>The usual SLL+LL fallback upon SLL conflict</li> /// <li>Pure SLL without LL fallback</li> /// </ul> /// <p><strong>COMBINED SLL+LL PARSING</strong></p> /// <p>When LL-fallback is enabled upon SLL conflict, correct predictions are /// ensured regardless of how the termination condition is computed by this /// method. Due to the substantially higher cost of LL prediction, the /// prediction should only fall back to LL when the additional lookahead /// cannot lead to a unique SLL prediction.</p> /// <p>Assuming combined SLL+LL parsing, an SLL configuration set with only /// conflicting subsets should fall back to full LL, even if the /// configuration sets don't resolve to the same alternative (e.g. /// <code></code> /// /// 1,2}} and /// <code></code> /// /// 3,4}}. If there is at least one non-conflicting /// configuration, SLL could continue with the hopes that more lookahead will /// resolve via one of those non-conflicting configurations.</p> /// <p>Here's the prediction termination rule them: SLL (for SLL+LL parsing) /// stops when it sees only conflicting configuration subsets. In contrast, /// full LL keeps going when there is uncertainty.</p> /// <p><strong>HEURISTIC</strong></p> /// <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 it. /// The single-alt-state thing lets prediction continue upon rules like /// (otherwise, it would admit defeat too soon):</p> /// <p> /// <code>[12|1|[], 6|2|[], 12|2|[]]. s : (ID | ID ID?) ';' ;</code> /// </p> /// <p>When the ATN simulation reaches the state before /// <code>';'</code> /// , it has a /// DFA state that looks like: /// <code>[12|1|[], 6|2|[], 12|2|[]]</code> /// . Naturally /// <code>12|1|[]</code> /// and /// <code>12|2|[]</code> /// conflict, but we cannot stop /// processing this node because alternative to has another way to continue, /// via /// <code>[6|2|[]]</code> /// .</p> /// <p>It also let's us continue for this rule:</p> /// <p> /// <code>[1|1|[], 1|2|[], 8|3|[]] a : A | A | A B ;</code> /// </p> /// <p>After matching input A, we reach the stop state for rule A, state 1. /// State 8 is the state right before B. Clearly alternatives 1 and 2 /// conflict and no amount of further lookahead will separate the two. /// However, alternative 3 will be able to continue and so we do not stop /// working on this state. In the previous example, we're concerned with /// states associated with the conflicting alternatives. Here alt 3 is not /// associated with the conflicting configs, but since we can continue /// looking for input reasonably, don't declare the state done.</p> /// <p><strong>PURE SLL PARSING</strong></p> /// <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> /// <p><strong>PREDICATES IN SLL+LL PARSING</strong></p> /// <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> /// <p>Implementation-wise, /// <see cref="ATNConfigSet">ATNConfigSet</see> /// combines stack contexts but not /// semantic predicate contexts so we might see two configurations like the /// following.</p> /// <p> /// <code></code> /// (s, 1, x, /// ), (s, 1, x', {p})}</p> /// <p>Before testing these configurations against others, we have to merge /// <code>x</code> /// and /// <code>x'</code> /// (without modifying the existing configurations). /// For example, we test /// <code>(x+x')==x''</code> /// when looking for conflicts in /// the following configurations.</p> /// <p> /// <code></code> /// (s, 1, x, /// ), (s, 1, x', {p}), (s, 2, x'', {})}</p> /// <p>If the configuration set has predicates (as indicated by /// <see cref="ATNConfigSet.HasSemanticContext()">ATNConfigSet.HasSemanticContext()</see> /// ), this algorithm makes a copy of /// the configurations to strip out all of the predicates so that a standard /// <see cref="ATNConfigSet">ATNConfigSet</see> /// will merge everything ignoring predicates.</p> /// </remarks> public static bool HasSLLConflictTerminatingPrediction(PredictionMode mode, ATNConfigSet configs) { if (AllConfigsInRuleStopStates(configs)) { return(true); } // pure SLL mode parsing if (mode == PredictionMode.Sll) { // Don't bother with combining configs from different semantic // contexts if we can fail over to full LL; costs more time // since we'll often fail over anyway. if (configs.HasSemanticContext) { // dup configs, tossing out semantic predicates ATNConfigSet dup = new ATNConfigSet(); foreach (ATNConfig c in configs) { ATNConfig config = c.Transform(c.State, SemanticContext.None, false); dup.AddItem(config); } configs = dup; } } // now we have combined contexts for configs with dissimilar preds // pure SLL or combined SLL+LL mode parsing ICollection <BitSet> altsets = GetConflictingAltSubsets(configs); bool heuristic = HasConflictingAltSet(altsets) && !HasStateAssociatedWithOneAlt(configs); return(heuristic); }