java类org.antlr.v4.runtime.dfa.DFAState的实例源码

LexerATNSimulator.java 文件源码 项目:Scratch-ApuC 阅读 27 收藏 0 点赞 0 评论 0
protected int execATN(@NotNull CharStream input, @NotNull DFAState ds0) {
    //System.out.println("enter exec index "+input.index()+" from "+ds0.configs);
    if ( debug ) {
        System.out.format(Locale.getDefault(), "start state closure=%s\n", ds0.configs);
    }

    int t = input.LA(1);
    @NotNull
    DFAState s = ds0; // s is current/from DFA state

    while ( true ) { // while more work
        if ( debug ) {
            System.out.format(Locale.getDefault(), "execATN loop starting closure: %s\n", s.configs);
        }

        // As we move src->trg, src->trg, we keep track of the previous trg to
        // avoid looking up the DFA state again, which is expensive.
        // If the previous target was already part of the DFA, we might
        // be able to avoid doing a reach operation upon t. If s!=null,
        // it means that semantic predicates didn't prevent us from
        // creating a DFA state. Once we know s!=null, we check to see if
        // the DFA state has an edge already for t. If so, we can just reuse
        // it's configuration set; there's no point in re-computing it.
        // This is kind of like doing DFA simulation within the ATN
        // simulation because DFA simulation is really just a way to avoid
        // computing reach/closure sets. Technically, once we know that
        // we have a previously added DFA state, we could jump over to
        // the DFA simulator. But, that would mean popping back and forth
        // a lot and making things more complicated algorithmically.
        // This optimization makes a lot of sense for loops within DFA.
        // A character will take us back to an existing DFA state
        // that already has lots of edges out of it. e.g., .* in comments.
        DFAState target = getExistingTargetState(s, t);
        if (target == null) {
            target = computeTargetState(input, s, t);
        }

        if (target == ERROR) {
            break;
        }

        if (target.isAcceptState) {
            captureSimState(prevAccept, input, target);
            if (t == IntStream.EOF) {
                break;
            }
        }

        if (t != IntStream.EOF) {
            consume(input);
            t = input.LA(1);
        }

        s = target; // flip; current DFA target becomes new src/from state
    }

    return failOrAccept(prevAccept, input, s.configs, t);
}
StatisticsLexerATNSimulator.java 文件源码 项目:antlrworks2 阅读 16 收藏 0 点赞 0 评论 0
@Override
protected DFAState getExistingTargetState(DFAState s, int t) {
    totalTransitions++;
    return super.getExistingTargetState(s, t);
}
StatisticsLexerATNSimulator.java 文件源码 项目:antlrworks2 阅读 14 收藏 0 点赞 0 评论 0
@Override
protected DFAState computeTargetState(CharStream input, DFAState s, int t) {
    computedTransitions++;
    return super.computeTargetState(input, s, t);
}
StatisticsParserATNSimulator.java 文件源码 项目:antlrworks2 阅读 21 收藏 0 点赞 0 评论 0
@Override
protected DFAState getExistingTargetState(DFAState previousD, int t) {
    totalTransitions[decision]++;
    return super.getExistingTargetState(previousD, t);
}
TracingLexerATNSimulator.java 文件源码 项目:antlrworks2 阅读 16 收藏 0 点赞 0 评论 0
@Override
protected DFAState computeTargetState(CharStream input, DFAState s, int t) {
    _listener.transition(true);
    return super.computeTargetState(input, s, t);
}
TracingLexerATNSimulator.java 文件源码 项目:antlrworks2 阅读 15 收藏 0 点赞 0 评论 0
@Override
protected void captureSimState(SimState settings, CharStream input, DFAState dfaState) {
    _listener.acceptState(dfaState.getPrediction());
    super.captureSimState(settings, input, dfaState);
}
ParserATNSimulator.java 文件源码 项目:Scratch-ApuC 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Get an existing target state for an edge in the DFA. If the target state
 * for the edge has not yet been computed or is otherwise not available,
 * this method returns {@code null}.
 *
 * @param previousD The current DFA state
 * @param t The next input symbol
 * @return The existing target DFA state for the given input symbol
 * {@code t}, or {@code null} if the target state for this edge is not
 * already cached
 */
@Nullable
protected DFAState getExistingTargetState(@NotNull DFAState previousD, int t) {
    DFAState[] edges = previousD.edges;
    if (edges == null || t + 1 < 0 || t + 1 >= edges.length) {
        return null;
    }

    return edges[t + 1];
}


问题


面经


文章

微信
公众号

扫码关注公众号