Search in sources :

Example 66 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.

the class ParserATNSimulator method reportAttemptingFullContext.

protected void reportAttemptingFullContext(@NotNull DFA dfa, @Nullable BitSet conflictingAlts, @NotNull SimulatorState conflictState, int startIndex, int stopIndex) {
    if (debug || retry_debug) {
        Interval interval = Interval.of(startIndex, stopIndex);
        System.out.println("reportAttemptingFullContext decision=" + dfa.decision + ":" + conflictState.s0.configs + ", input=" + parser.getInputStream().getText(interval));
    }
    if (parser != null)
        parser.getErrorListenerDispatch().reportAttemptingFullContext(parser, dfa, startIndex, stopIndex, conflictingAlts, conflictState);
}
Also used : Interval(org.antlr.v4.runtime.misc.Interval)

Example 67 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.

the class ParserATNSimulator method reportContextSensitivity.

protected void reportContextSensitivity(@NotNull DFA dfa, int prediction, @NotNull SimulatorState acceptState, int startIndex, int stopIndex) {
    if (debug || retry_debug) {
        Interval interval = Interval.of(startIndex, stopIndex);
        System.out.println("reportContextSensitivity decision=" + dfa.decision + ":" + acceptState.s0.configs + ", input=" + parser.getInputStream().getText(interval));
    }
    if (parser != null)
        parser.getErrorListenerDispatch().reportContextSensitivity(parser, dfa, startIndex, stopIndex, prediction, acceptState);
}
Also used : Interval(org.antlr.v4.runtime.misc.Interval)

Example 68 with DFA

use of org.antlr.v4.runtime.dfa.DFA 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 69 with DFA

use of org.antlr.v4.runtime.dfa.DFA 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)

Example 70 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.

the class ParserATNSimulator method reportAmbiguity.

/**
 * If context sensitive parsing, we know it's ambiguity not conflict
 */
protected void reportAmbiguity(@NotNull DFA dfa, // the DFA state from execATN() that had SLL conflicts
DFAState D, int startIndex, int stopIndex, boolean exact, @NotNull BitSet ambigAlts, // configs that LL not SLL considered conflicting
@NotNull ATNConfigSet configs) {
    if (debug || retry_debug) {
        Interval interval = Interval.of(startIndex, stopIndex);
        System.out.println("reportAmbiguity " + ambigAlts + ":" + configs + ", input=" + parser.getInputStream().getText(interval));
    }
    if (parser != null)
        parser.getErrorListenerDispatch().reportAmbiguity(parser, dfa, startIndex, stopIndex, exact, ambigAlts, configs);
}
Also used : Interval(org.antlr.v4.runtime.misc.Interval)

Aggregations

DFA (org.antlr.v4.runtime.dfa.DFA)37 DFAState (org.antlr.v4.runtime.dfa.DFAState)28 Test (org.junit.Test)19 Grammar (org.antlr.v4.tool.Grammar)18 LexerGrammar (org.antlr.v4.tool.LexerGrammar)18 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)16 ArrayList (java.util.ArrayList)14 STGroupString (org.stringtemplate.v4.STGroupString)14 CharStream (org.antlr.v4.runtime.CharStream)13 IOException (java.io.IOException)10 IntegerList (org.antlr.v4.runtime.misc.IntegerList)10 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)9 InputStream (java.io.InputStream)8 BitSet (java.util.BitSet)8 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)7 ATN (org.antlr.v4.runtime.atn.ATN)7 Interval (org.antlr.v4.runtime.misc.Interval)7 NotNull (org.antlr.v4.runtime.misc.NotNull)7 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)6 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)5