Search in sources :

Example 1 with ATNConfig

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

the class LL1Analyzer method getDecisionLookahead.

/**
	 * Calculates the SLL(1) expected lookahead set for each outgoing transition
	 * of an {@link ATNState}. The returned array has one element for each
	 * outgoing transition in {@code s}. If the closure from transition
	 * <em>i</em> leads to a semantic predicate before matching a symbol, the
	 * element at index <em>i</em> of the result will be {@code null}.
	 *
	 * @param s the ATN state
	 * @return the expected symbols for each outgoing transition of {@code s}.
	 */
public IntervalSet[] getDecisionLookahead(ATNState s) {
    //		System.out.println("LOOK("+s.stateNumber+")");
    if (s == null) {
        return null;
    }
    IntervalSet[] look = new IntervalSet[s.getNumberOfTransitions()];
    for (int alt = 0; alt < s.getNumberOfTransitions(); alt++) {
        look[alt] = new IntervalSet();
        Set<ATNConfig> lookBusy = new HashSet<ATNConfig>();
        // fail to get lookahead upon pred
        boolean seeThruPreds = false;
        _LOOK(s.transition(alt).target, null, PredictionContext.EMPTY, look[alt], lookBusy, new BitSet(), seeThruPreds, false);
        // or we had a predicate when we !seeThruPreds
        if (look[alt].size() == 0 || look[alt].contains(HIT_PRED)) {
            look[alt] = null;
        }
    }
    return look;
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) BitSet(java.util.BitSet) HashSet(java.util.HashSet)

Example 2 with ATNConfig

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

the class LL1Analyzer method LOOK.

/**
	 * Compute set of tokens that can follow {@code s} in the ATN in the
	 * specified {@code ctx}.
	 *
	 * <p>If {@code ctx} is {@code null} and the end of the rule containing
	 * {@code s} is reached, {@link Token#EPSILON} is added to the result set.
	 * If {@code ctx} is not {@code null} and the end of the outermost rule is
	 * reached, {@link Token#EOF} is added to the result set.</p>
	 *
	 * @param s the ATN state
	 * @param stopState the ATN state to stop at. This can be a
	 * {@link BlockEndState} to detect epsilon paths through a closure.
	 * @param ctx the complete parser context, or {@code null} if the context
	 * should be ignored
	 *
	 * @return The set of tokens that can follow {@code s} in the ATN in the
	 * specified {@code ctx}.
	 */
public IntervalSet LOOK(ATNState s, ATNState stopState, RuleContext ctx) {
    IntervalSet r = new IntervalSet();
    // ignore preds; get all lookahead
    boolean seeThruPreds = true;
    PredictionContext lookContext = ctx != null ? PredictionContext.fromRuleContext(s.atn, ctx) : null;
    _LOOK(s, stopState, lookContext, r, new HashSet<ATNConfig>(), new BitSet(), seeThruPreds, true);
    return r;
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) BitSet(java.util.BitSet)

Example 3 with ATNConfig

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

the class LexerATNSimulator method addDFAState.

/** Add a new DFA state if there isn't one with this set of
		configurations already. This method also detects the first
		configuration containing an ATN rule stop state. Later, when
		traversing the DFA, we will know which rule to accept.
	 */
protected DFAState addDFAState(ATNConfigSet configs) {
    /* the lexer evaluates predicates on-the-fly; by this point configs
		 * should not contain any configurations with unevaluated predicates.
		 */
    assert !configs.hasSemanticContext;
    DFAState proposed = new DFAState(configs);
    ATNConfig firstConfigWithRuleStopState = null;
    for (ATNConfig c : configs) {
        if (c.state instanceof RuleStopState) {
            firstConfigWithRuleStopState = c;
            break;
        }
    }
    if (firstConfigWithRuleStopState != null) {
        proposed.isAcceptState = true;
        proposed.lexerActionExecutor = ((LexerATNConfig) firstConfigWithRuleStopState).getLexerActionExecutor();
        proposed.prediction = atn.ruleToTokenType[firstConfigWithRuleStopState.state.ruleIndex];
    }
    DFA dfa = decisionToDFA[mode];
    synchronized (dfa.states) {
        DFAState existing = dfa.states.get(proposed);
        if (existing != null)
            return existing;
        DFAState newState = proposed;
        newState.stateNumber = dfa.states.size();
        configs.setReadonly(true);
        newState.configs = configs;
        dfa.states.put(newState, newState);
        return newState;
    }
}
Also used : DFAState(org.antlr.v4.runtime.dfa.DFAState) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 4 with ATNConfig

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

the class DOTGenerator method getStateLabel.

protected String getStateLabel(DFAState s) {
    if (s == null)
        return "null";
    StringBuilder buf = new StringBuilder(250);
    buf.append('s');
    buf.append(s.stateNumber);
    if (s.isAcceptState) {
        buf.append("=>").append(s.prediction);
    }
    if (s.requiresFullContext) {
        buf.append("^");
    }
    if (grammar != null) {
        Set<Integer> alts = s.getAltSet();
        if (alts != null) {
            buf.append("\\n");
            // separate alts
            IntegerList altList = new IntegerList();
            altList.addAll(alts);
            altList.sort();
            Set<ATNConfig> configurations = s.configs;
            for (int altIndex = 0; altIndex < altList.size(); altIndex++) {
                int alt = altList.get(altIndex);
                if (altIndex > 0) {
                    buf.append("\\n");
                }
                buf.append("alt");
                buf.append(alt);
                buf.append(':');
                // get a list of configs for just this alt
                // it will help us print better later
                List<ATNConfig> configsInAlt = new ArrayList<ATNConfig>();
                for (ATNConfig c : configurations) {
                    if (c.alt != alt)
                        continue;
                    configsInAlt.add(c);
                }
                int n = 0;
                for (int cIndex = 0; cIndex < configsInAlt.size(); cIndex++) {
                    ATNConfig c = configsInAlt.get(cIndex);
                    n++;
                    buf.append(c.toString(null, false));
                    if ((cIndex + 1) < configsInAlt.size()) {
                        buf.append(", ");
                    }
                    if (n % 5 == 0 && (configsInAlt.size() - cIndex) > 3) {
                        buf.append("\\n");
                    }
                }
            }
        }
    }
    String stateLabel = buf.toString();
    return stateLabel;
}
Also used : ATNConfig(org.antlr.v4.runtime.atn.ATNConfig) IntegerList(org.antlr.v4.runtime.misc.IntegerList) ArrayList(java.util.ArrayList)

Example 5 with ATNConfig

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

the class ParserATNSimulator method removeAllConfigsNotInRuleStopState.

/**
	 * Return a configuration set containing only the configurations from
	 * {@code configs} which are in a {@link RuleStopState}. If all
	 * configurations in {@code configs} are already in a rule stop state, this
	 * method simply returns {@code configs}.
	 *
	 * <p>When {@code lookToEndOfRule} is true, this method uses
	 * {@link ATN#nextTokens} for each configuration in {@code configs} which is
	 * not already in a rule stop state to see if a rule stop state is reachable
	 * from the configuration via epsilon-only transitions.</p>
	 *
	 * @param configs the configuration set to update
	 * @param lookToEndOfRule when true, this method checks for rule stop states
	 * reachable by epsilon-only transitions from each configuration in
	 * {@code configs}.
	 *
	 * @return {@code configs} if all configurations in {@code configs} are in a
	 * rule stop state, otherwise return a new configuration set containing only
	 * the configurations from {@code configs} which are in a rule stop state
	 */
protected ATNConfigSet removeAllConfigsNotInRuleStopState(ATNConfigSet configs, boolean lookToEndOfRule) {
    if (PredictionMode.allConfigsInRuleStopStates(configs)) {
        return configs;
    }
    ATNConfigSet result = new ATNConfigSet(configs.fullCtx);
    for (ATNConfig config : configs) {
        if (config.state instanceof RuleStopState) {
            result.add(config, mergeCache);
            continue;
        }
        if (lookToEndOfRule && config.state.onlyHasEpsilonTransitions()) {
            IntervalSet nextTokens = atn.nextTokens(config.state);
            if (nextTokens.contains(Token.EPSILON)) {
                ATNState endOfRuleState = atn.ruleToStopState[config.state.ruleIndex];
                result.add(new ATNConfig(config, endOfRuleState), mergeCache);
            }
        }
    }
    return result;
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet)

Aggregations

IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)3 ArrayList (java.util.ArrayList)2 BitSet (java.util.BitSet)2 HashSet (java.util.HashSet)2 ATNConfig (org.antlr.v4.runtime.atn.ATNConfig)2 DFA (org.antlr.v4.runtime.dfa.DFA)2 DFAState (org.antlr.v4.runtime.dfa.DFAState)2 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 CharStream (org.antlr.v4.runtime.CharStream)1 Lexer (org.antlr.v4.runtime.Lexer)1 Parser (org.antlr.v4.runtime.Parser)1 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)1 ParserATNSimulator (org.antlr.v4.runtime.atn.ParserATNSimulator)1 IntegerList (org.antlr.v4.runtime.misc.IntegerList)1