Search in sources :

Example 6 with PredictionContextCache

use of org.antlr.v4.runtime.atn.PredictionContextCache in project antlr4 by antlr.

the class TestGraphNodes method setUp.

@Before
public void setUp() {
    PredictionContext.globalNodeCount = 1;
    contextCache = new PredictionContextCache();
}
Also used : PredictionContextCache(org.antlr.v4.runtime.atn.PredictionContextCache) Before(org.junit.Before)

Example 7 with PredictionContextCache

use of org.antlr.v4.runtime.atn.PredictionContextCache in project antlr4 by antlr.

the class TestATNParserPrediction method checkPredictedAlt.

/**
 * first check that the ATN predicts right alt.
 *  Then check adaptive prediction.
 */
public void checkPredictedAlt(LexerGrammar lg, Grammar g, int decision, String inputString, int expectedAlt) {
    Tool.internalOption_ShowATNConfigsInDFA = true;
    ATN lexatn = createATN(lg, true);
    LexerATNSimulator lexInterp = new LexerATNSimulator(lexatn, new DFA[] { new DFA(lexatn.modeToStartState.get(Lexer.DEFAULT_MODE)) }, new PredictionContextCache());
    IntegerList types = getTokenTypesViaATN(inputString, lexInterp);
    // System.out.println(types);
    semanticProcess(lg);
    g.importVocab(lg);
    semanticProcess(g);
    ParserATNFactory f = new ParserATNFactory(g);
    ATN atn = f.createATN();
    DOTGenerator dot = new DOTGenerator(g);
    Rule r = g.getRule("a");
    // if ( r!=null) System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    r = g.getRule("b");
    // if ( r!=null) System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    r = g.getRule("e");
    // if ( r!=null) System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    r = g.getRule("ifstat");
    // if ( r!=null) System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    r = g.getRule("block");
    // if ( r!=null) System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    // Check ATN prediction
    // ParserATNSimulator interp = new ParserATNSimulator(atn);
    TokenStream input = new MockIntTokenStream(types);
    ParserInterpreterForTesting interp = new ParserInterpreterForTesting(g, input);
    int alt = interp.adaptivePredict(input, decision, ParserRuleContext.EMPTY);
    assertEquals(expectedAlt, alt);
    // Check adaptive prediction
    input.seek(0);
    alt = interp.adaptivePredict(input, decision, null);
    assertEquals(expectedAlt, alt);
    // run 2x; first time creates DFA in atn
    input.seek(0);
    alt = interp.adaptivePredict(input, decision, null);
    assertEquals(expectedAlt, alt);
}
Also used : ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) DOTGenerator(org.antlr.v4.tool.DOTGenerator) MockIntTokenStream(org.antlr.v4.test.runtime.MockIntTokenStream) TokenStream(org.antlr.v4.runtime.TokenStream) LexerATNSimulator(org.antlr.v4.runtime.atn.LexerATNSimulator) IntegerList(org.antlr.v4.runtime.misc.IntegerList) MockIntTokenStream(org.antlr.v4.test.runtime.MockIntTokenStream) RuntimeTestUtils.getTokenTypesViaATN(org.antlr.v4.test.runtime.RuntimeTestUtils.getTokenTypesViaATN) ATN(org.antlr.v4.runtime.atn.ATN) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) PredictionContextCache(org.antlr.v4.runtime.atn.PredictionContextCache) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 8 with PredictionContextCache

use of org.antlr.v4.runtime.atn.PredictionContextCache in project antlr4 by tunnelvisionlabs.

the class ParserATNSimulator method addDFAState.

/**
 * See comment on LexerInterpreter.addDFAState.
 */
@NotNull
protected DFAState addDFAState(@NotNull DFA dfa, @NotNull ATNConfigSet configs, PredictionContextCache contextCache) {
    final boolean enableDfa = enable_global_context_dfa || !configs.isOutermostConfigSet();
    if (enableDfa) {
        if (!configs.isReadOnly()) {
            configs.optimizeConfigs(this);
        }
        DFAState proposed = createDFAState(dfa, configs);
        DFAState existing = dfa.states.get(proposed);
        if (existing != null)
            return existing;
    }
    if (!configs.isReadOnly()) {
        if (configs.getConflictInfo() == null) {
            configs.setConflictInfo(isConflicted(configs, contextCache));
        }
    }
    DFAState newState = createDFAState(dfa, configs.clone(true));
    DecisionState decisionState = atn.getDecisionState(dfa.decision);
    int predictedAlt = getUniqueAlt(configs);
    if (predictedAlt != ATN.INVALID_ALT_NUMBER) {
        newState.setAcceptState(new AcceptStateInfo(predictedAlt));
    } else if (configs.getConflictingAlts() != null) {
        newState.setAcceptState(new AcceptStateInfo(newState.configs.getConflictingAlts().nextSetBit(0)));
    }
    if (newState.isAcceptState() && configs.hasSemanticContext()) {
        predicateDFAState(newState, configs, decisionState.getNumberOfTransitions());
    }
    if (!enableDfa) {
        return newState;
    }
    DFAState added = dfa.addState(newState);
    if (debug && added == newState)
        System.out.println("adding new DFA state: " + newState);
    return added;
}
Also used : DFAState(org.antlr.v4.runtime.dfa.DFAState) AcceptStateInfo(org.antlr.v4.runtime.dfa.AcceptStateInfo) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 9 with PredictionContextCache

use of org.antlr.v4.runtime.atn.PredictionContextCache in project antlr4 by tunnelvisionlabs.

the class ParserATNSimulator method computeReachSet.

protected SimulatorState computeReachSet(DFA dfa, SimulatorState previous, int t, PredictionContextCache contextCache) {
    final boolean useContext = previous.useContext;
    ParserRuleContext remainingGlobalContext = previous.remainingOuterContext;
    DFAState s = previous.s0;
    if (useContext) {
        while (s.isContextSymbol(t)) {
            DFAState next = null;
            if (remainingGlobalContext != null) {
                remainingGlobalContext = skipTailCalls(remainingGlobalContext);
                next = s.getContextTarget(getReturnState(remainingGlobalContext));
            }
            if (next == null) {
                break;
            }
            assert remainingGlobalContext != null;
            remainingGlobalContext = remainingGlobalContext.getParent();
            s = next;
        }
    }
    assert !isAcceptState(s, useContext);
    if (isAcceptState(s, useContext)) {
        return new SimulatorState(previous.outerContext, s, useContext, remainingGlobalContext);
    }
    final DFAState s0 = s;
    DFAState target = getExistingTargetState(s0, t);
    if (target == null) {
        Tuple2<DFAState, ParserRuleContext> result = computeTargetState(dfa, s0, remainingGlobalContext, t, useContext, contextCache);
        target = result.getItem1();
        remainingGlobalContext = result.getItem2();
    }
    if (target == ERROR) {
        return null;
    }
    assert !useContext || !target.configs.getDipsIntoOuterContext();
    return new SimulatorState(previous.outerContext, target, useContext, remainingGlobalContext);
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DFAState(org.antlr.v4.runtime.dfa.DFAState)

Example 10 with PredictionContextCache

use of org.antlr.v4.runtime.atn.PredictionContextCache in project antlr4 by tunnelvisionlabs.

the class ParserATNSimulator method computeTargetState.

/**
 * Compute a target state for an edge in the DFA, and attempt to add the
 * computed state and corresponding edge to the DFA.
 *
 * @param dfa
 * @param s The current DFA state
 * @param remainingGlobalContext
 * @param t The next input symbol
 * @param useContext
 * @param contextCache
 *
 * @return The computed target DFA state for the given input symbol
 * {@code t}. If {@code t} does not lead to a valid DFA state, this method
 * returns {@link #ERROR}.
 */
@NotNull
protected Tuple2<DFAState, ParserRuleContext> computeTargetState(@NotNull DFA dfa, @NotNull DFAState s, ParserRuleContext remainingGlobalContext, int t, boolean useContext, PredictionContextCache contextCache) {
    List<ATNConfig> closureConfigs = new ArrayList<ATNConfig>(s.configs);
    IntegerList contextElements = null;
    ATNConfigSet reach = new ATNConfigSet();
    boolean stepIntoGlobal;
    do {
        boolean hasMoreContext = !useContext || remainingGlobalContext != null;
        if (!hasMoreContext) {
            reach.setOutermostConfigSet(true);
        }
        ATNConfigSet reachIntermediate = new ATNConfigSet();
        /* Configurations already in a rule stop state indicate reaching the end
			 * of the decision rule (local context) or end of the start rule (full
			 * context). Once reached, these configurations are never updated by a
			 * closure operation, so they are handled separately for the performance
			 * advantage of having a smaller intermediate set when calling closure.
			 *
			 * For full-context reach operations, separate handling is required to
			 * ensure that the alternative matching the longest overall sequence is
			 * chosen when multiple such configurations can match the input.
			 */
        List<ATNConfig> skippedStopStates = null;
        for (ATNConfig c : closureConfigs) {
            if (debug)
                System.out.println("testing " + getTokenName(t) + " at " + c.toString());
            if (c.getState() instanceof RuleStopState) {
                assert c.getContext().isEmpty();
                if (useContext && !c.getReachesIntoOuterContext() || t == IntStream.EOF) {
                    if (skippedStopStates == null) {
                        skippedStopStates = new ArrayList<ATNConfig>();
                    }
                    skippedStopStates.add(c);
                }
                continue;
            }
            int n = c.getState().getNumberOfOptimizedTransitions();
            for (int ti = 0; ti < n; ti++) {
                // for each optimized transition
                Transition trans = c.getState().getOptimizedTransition(ti);
                ATNState target = getReachableTarget(c, trans, t);
                if (target != null) {
                    reachIntermediate.add(c.transform(target, false), contextCache);
                }
            }
        }
        /* This block optimizes the reach operation for intermediate sets which
			 * trivially indicate a termination state for the overall
			 * adaptivePredict operation.
			 *
			 * The conditions assume that intermediate
			 * contains all configurations relevant to the reach set, but this
			 * condition is not true when one or more configurations have been
			 * withheld in skippedStopStates, or when the current symbol is EOF.
			 */
        if (optimize_unique_closure && skippedStopStates == null && t != Token.EOF && reachIntermediate.getUniqueAlt() != ATN.INVALID_ALT_NUMBER) {
            reachIntermediate.setOutermostConfigSet(reach.isOutermostConfigSet());
            reach = reachIntermediate;
            break;
        }
        /* If the reach set could not be trivially determined, perform a closure
			 * operation on the intermediate set to compute its initial value.
			 */
        final boolean collectPredicates = false;
        boolean treatEofAsEpsilon = t == Token.EOF;
        closure(reachIntermediate, reach, collectPredicates, hasMoreContext, contextCache, treatEofAsEpsilon);
        stepIntoGlobal = reach.getDipsIntoOuterContext();
        if (t == IntStream.EOF) {
            /* After consuming EOF no additional input is possible, so we are
				 * only interested in configurations which reached the end of the
				 * decision rule (local context) or end of the start rule (full
				 * context). Update reach to contain only these configurations. This
				 * handles both explicit EOF transitions in the grammar and implicit
				 * EOF transitions following the end of the decision or start rule.
				 *
				 * This is handled before the configurations in skippedStopStates,
				 * because any configurations potentially added from that list are
				 * already guaranteed to meet this condition whether or not it's
				 * required.
				 */
            reach = removeAllConfigsNotInRuleStopState(reach, contextCache);
        }
        /* If skippedStopStates is not null, then it contains at least one
			 * configuration. For full-context reach operations, these
			 * configurations reached the end of the start rule, in which case we
			 * only add them back to reach if no configuration during the current
			 * closure operation reached such a state. This ensures adaptivePredict
			 * chooses an alternative matching the longest overall sequence when
			 * multiple alternatives are viable.
			 */
        if (skippedStopStates != null && (!useContext || !PredictionMode.hasConfigInRuleStopState(reach))) {
            assert !skippedStopStates.isEmpty();
            for (ATNConfig c : skippedStopStates) {
                reach.add(c, contextCache);
            }
        }
        if (useContext && stepIntoGlobal) {
            reach.clear();
            remainingGlobalContext = skipTailCalls(remainingGlobalContext);
            int nextContextElement = getReturnState(remainingGlobalContext);
            if (contextElements == null) {
                contextElements = new IntegerList();
            }
            if (remainingGlobalContext.isEmpty()) {
                remainingGlobalContext = null;
            } else {
                remainingGlobalContext = remainingGlobalContext.getParent();
            }
            contextElements.add(nextContextElement);
            if (nextContextElement != PredictionContext.EMPTY_FULL_STATE_KEY) {
                for (int i = 0; i < closureConfigs.size(); i++) {
                    closureConfigs.set(i, closureConfigs.get(i).appendContext(nextContextElement, contextCache));
                }
            }
        }
    } while (useContext && stepIntoGlobal);
    if (reach.isEmpty()) {
        addDFAEdge(s, t, ERROR);
        return Tuple.create(ERROR, remainingGlobalContext);
    }
    DFAState result = addDFAEdge(dfa, s, t, contextElements, reach, contextCache);
    return Tuple.create(result, remainingGlobalContext);
}
Also used : DFAState(org.antlr.v4.runtime.dfa.DFAState) ArrayList(java.util.ArrayList) IntegerList(org.antlr.v4.runtime.misc.IntegerList) NotNull(org.antlr.v4.runtime.misc.NotNull)

Aggregations

PredictionContextCache (org.antlr.v4.runtime.atn.PredictionContextCache)6 DFAState (org.antlr.v4.runtime.dfa.DFAState)6 DFA (org.antlr.v4.runtime.dfa.DFA)5 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)4 NotNull (org.antlr.v4.runtime.misc.NotNull)4 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)3 ParserATNSimulator (org.antlr.v4.runtime.atn.ParserATNSimulator)3 IntegerList (org.antlr.v4.runtime.misc.IntegerList)3 TokenStream (org.antlr.v4.runtime.TokenStream)2 ATN (org.antlr.v4.runtime.atn.ATN)2 MockIntTokenStream (org.antlr.v4.test.runtime.MockIntTokenStream)2 RuntimeTestUtils.getTokenTypesViaATN (org.antlr.v4.test.runtime.RuntimeTestUtils.getTokenTypesViaATN)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)1 CharStream (org.antlr.v4.runtime.CharStream)1 CommonTokenFactory (org.antlr.v4.runtime.CommonTokenFactory)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1