Search in sources :

Example 1 with PredictionMode

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

the class ParserATNSimulator method execDFA.

protected int execDFA(@NotNull DFA dfa, @NotNull TokenStream input, int startIndex, @NotNull SimulatorState state) {
    ParserRuleContext outerContext = state.outerContext;
    if (dfa_debug)
        System.out.println("DFA decision " + dfa.decision + " exec LA(1)==" + getLookaheadName(input) + ", outerContext=" + outerContext.toString(parser));
    if (dfa_debug)
        System.out.print(dfa.toString(parser.getVocabulary(), parser.getRuleNames()));
    DFAState s = state.s0;
    int t = input.LA(1);
    ParserRuleContext remainingOuterContext = state.remainingOuterContext;
    while (true) {
        if (dfa_debug)
            System.out.println("DFA state " + s.stateNumber + " LA(1)==" + getLookaheadName(input));
        if (state.useContext) {
            while (s.isContextSymbol(t)) {
                DFAState next = null;
                if (remainingOuterContext != null) {
                    remainingOuterContext = skipTailCalls(remainingOuterContext);
                    next = s.getContextTarget(getReturnState(remainingOuterContext));
                }
                if (next == null) {
                    // fail over to ATN
                    SimulatorState initialState = new SimulatorState(state.outerContext, s, state.useContext, remainingOuterContext);
                    return execATN(dfa, input, startIndex, initialState);
                }
                assert remainingOuterContext != null;
                remainingOuterContext = remainingOuterContext.getParent();
                s = next;
            }
        }
        if (isAcceptState(s, state.useContext)) {
            if (s.predicates != null) {
                if (dfa_debug)
                    System.out.println("accept " + s);
            } else {
                if (dfa_debug)
                    System.out.println("accept; predict " + s.getPrediction() + " in state " + s.stateNumber);
            }
            // TODO: v3 dfa don't do this.
            break;
        }
        // t is not updated if one of these states is reached
        assert !isAcceptState(s, state.useContext);
        // if no edge, pop over to ATN interpreter, update DFA and return
        DFAState target = getExistingTargetState(s, t);
        if (target == null) {
            if (dfa_debug && t >= 0)
                System.out.println("no edge for " + parser.getVocabulary().getDisplayName(t));
            int alt;
            if (dfa_debug) {
                Interval interval = Interval.of(startIndex, parser.getInputStream().index());
                System.out.println("ATN exec upon " + parser.getInputStream().getText(interval) + " at DFA state " + s.stateNumber);
            }
            SimulatorState initialState = new SimulatorState(outerContext, s, state.useContext, remainingOuterContext);
            alt = execATN(dfa, input, startIndex, initialState);
            if (dfa_debug) {
                System.out.println("back from DFA update, alt=" + alt + ", dfa=\n" + dfa.toString(parser.getVocabulary(), parser.getRuleNames()));
            // dump(dfa);
            }
            // action already executed
            if (dfa_debug)
                System.out.println("DFA decision " + dfa.decision + " predicts " + alt);
            // we've updated DFA, exec'd action, and have our deepest answer
            return alt;
        } else if (target == ERROR) {
            SimulatorState errorState = new SimulatorState(outerContext, s, state.useContext, remainingOuterContext);
            return handleNoViableAlt(input, startIndex, errorState);
        }
        s = target;
        if (!isAcceptState(s, state.useContext) && t != IntStream.EOF) {
            input.consume();
            t = input.LA(1);
        }
    }
    if (!state.useContext && s.configs.getConflictInfo() != null) {
        if (dfa.atnStartState instanceof DecisionState) {
            if (!userWantsCtxSensitive || (!s.configs.getDipsIntoOuterContext() && s.configs.isExactConflict()) || (treat_sllk1_conflict_as_ambiguity && input.index() == startIndex)) {
            // we don't report the ambiguity again
            // if ( !acceptState.configset.hasSemanticContext() ) {
            // reportAmbiguity(dfa, acceptState, startIndex, input.index(), acceptState.configset.getConflictingAlts(), acceptState.configset);
            // }
            } else {
                assert !state.useContext;
                // Before attempting full context prediction, check to see if there are
                // disambiguating or validating predicates to evaluate which allow an
                // immediate decision
                BitSet conflictingAlts = null;
                DFAState.PredPrediction[] predicates = s.predicates;
                if (predicates != null) {
                    int conflictIndex = input.index();
                    if (conflictIndex != startIndex) {
                        input.seek(startIndex);
                    }
                    conflictingAlts = evalSemanticContext(predicates, outerContext, true);
                    if (conflictingAlts.cardinality() == 1) {
                        return conflictingAlts.nextSetBit(0);
                    }
                    if (conflictIndex != startIndex) {
                        // restore the index so reporting the fallback to full
                        // context occurs with the index at the correct spot
                        input.seek(conflictIndex);
                    }
                }
                if (reportAmbiguities) {
                    SimulatorState conflictState = new SimulatorState(outerContext, s, state.useContext, remainingOuterContext);
                    reportAttemptingFullContext(dfa, conflictingAlts, conflictState, startIndex, input.index());
                }
                input.seek(startIndex);
                return adaptivePredict(input, dfa.decision, outerContext, true);
            }
        }
    }
    // Before jumping to prediction, check to see if there are
    // disambiguating or validating predicates to evaluate
    DFAState.PredPrediction[] predicates = s.predicates;
    if (predicates != null) {
        int stopIndex = input.index();
        if (startIndex != stopIndex) {
            input.seek(startIndex);
        }
        BitSet alts = evalSemanticContext(predicates, outerContext, reportAmbiguities && predictionMode == PredictionMode.LL_EXACT_AMBIG_DETECTION);
        switch(alts.cardinality()) {
            case 0:
                throw noViableAlt(input, outerContext, s.configs, startIndex);
            case 1:
                return alts.nextSetBit(0);
            default:
                // set of ambig alts is reported.
                if (startIndex != stopIndex) {
                    input.seek(stopIndex);
                }
                reportAmbiguity(dfa, s, startIndex, stopIndex, s.configs.isExactConflict(), alts, s.configs);
                return alts.nextSetBit(0);
        }
    }
    if (dfa_debug)
        System.out.println("DFA decision " + dfa.decision + " predicts " + s.getPrediction());
    return s.getPrediction();
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DFAState(org.antlr.v4.runtime.dfa.DFAState) BitSet(java.util.BitSet) Interval(org.antlr.v4.runtime.misc.Interval)

Example 2 with PredictionMode

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

the class ParserATNSimulator method adaptivePredict.

public int adaptivePredict(@NotNull TokenStream input, int decision, @Nullable ParserRuleContext outerContext, boolean useContext) {
    DFA dfa = atn.decisionToDFA[decision];
    assert dfa != null;
    if (optimize_ll1 && !dfa.isPrecedenceDfa() && !dfa.isEmpty()) {
        int ll_1 = input.LA(1);
        if (ll_1 >= 0 && ll_1 <= Short.MAX_VALUE) {
            int key = (decision << 16) + ll_1;
            Integer alt = atn.LL1Table.get(key);
            if (alt != null) {
                return alt;
            }
        }
    }
    this.dfa = dfa;
    if (force_global_context) {
        useContext = true;
    } else if (!always_try_local_context) {
        useContext |= dfa.isContextSensitive();
    }
    userWantsCtxSensitive = useContext || (predictionMode != PredictionMode.SLL && outerContext != null && !atn.decisionToState.get(decision).sll);
    if (outerContext == null) {
        outerContext = ParserRuleContext.emptyContext();
    }
    SimulatorState state = null;
    if (!dfa.isEmpty()) {
        state = getStartState(dfa, input, outerContext, useContext);
    }
    if (state == null) {
        if (outerContext == null)
            outerContext = ParserRuleContext.emptyContext();
        if (debug)
            System.out.println("ATN decision " + dfa.decision + " exec LA(1)==" + getLookaheadName(input) + ", outerContext=" + outerContext.toString(parser));
        state = computeStartState(dfa, outerContext, useContext);
    }
    int m = input.mark();
    int index = input.index();
    try {
        int alt = execDFA(dfa, input, index, state);
        if (debug)
            System.out.println("DFA after predictATN: " + dfa.toString(parser.getVocabulary(), parser.getRuleNames()));
        return alt;
    } finally {
        this.dfa = null;
        input.seek(index);
        input.release(m);
    }
}
Also used : DFA(org.antlr.v4.runtime.dfa.DFA)

Example 3 with PredictionMode

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

the class Parser method setProfile.

/**
 * @since 4.3
 */
public void setProfile(boolean profile) {
    ParserATNSimulator interp = getInterpreter();
    PredictionMode saveMode = interp.getPredictionMode();
    if (profile) {
        if (!(interp instanceof ProfilingATNSimulator)) {
            setInterpreter(new ProfilingATNSimulator(this));
        }
    } else if (interp instanceof ProfilingATNSimulator) {
        ParserATNSimulator sim = new ParserATNSimulator(this, getATN(), interp.decisionToDFA, interp.getSharedContextCache());
        setInterpreter(sim);
    }
    getInterpreter().setPredictionMode(saveMode);
}
Also used : ProfilingATNSimulator(org.antlr.v4.runtime.atn.ProfilingATNSimulator) ParserATNSimulator(org.antlr.v4.runtime.atn.ParserATNSimulator) PredictionMode(org.antlr.v4.runtime.atn.PredictionMode)

Aggregations

BitSet (java.util.BitSet)1 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1 ParserATNSimulator (org.antlr.v4.runtime.atn.ParserATNSimulator)1 PredictionMode (org.antlr.v4.runtime.atn.PredictionMode)1 ProfilingATNSimulator (org.antlr.v4.runtime.atn.ProfilingATNSimulator)1 DFA (org.antlr.v4.runtime.dfa.DFA)1 DFAState (org.antlr.v4.runtime.dfa.DFAState)1 Interval (org.antlr.v4.runtime.misc.Interval)1