GrammarParserInterpreter.java 文件源码

java
阅读 30 收藏 0 点赞 0 评论 0

项目:codebuff 作者:
/** Override this method so that we can record which alternative
     *  was taken at each decision point. For non-left recursive rules,
     *  it's simple. Set decisionStatesThatSetOuterAltNumInContext
     *  indicates which decision states should set the outer alternative number.
     *
     *  <p>Left recursive rules are much more complicated to deal with:
     *  there is typically a decision for the primary alternatives and a
     *  decision to choose between the recursive operator alternatives.
     *  For example, the following left recursive rule has two primary and 2
     *  recursive alternatives.</p>
     *
         e : e '*' e
           | '-' INT
           | e '+' e
           | ID
           ;

     *  <p>ANTLR rewrites that rule to be</p>

         e[int precedence]
             : ('-' INT | ID)
             ( {...}? '*' e[5]
             | {...}? '+' e[3]
             )*
            ;

     *
     *  <p>So, there are two decisions associated with picking the outermost alt.
     *  This complicates our tracking significantly. The outermost alternative number
     *  is a function of the decision (ATN state) within a left recursive rule and the
     *  predicted alternative coming back from adaptivePredict().
     *
     *  We use stateToAltsMap as a cache to avoid expensive calls to
     *  getRecursiveOpAlts().
     */
    @Override
    protected int visitDecisionState(DecisionState p) {
        int predictedAlt = super.visitDecisionState(p);
        if( p.getNumberOfTransitions() > 1) {
//          System.out.println("decision "+p.decision+": "+predictedAlt);
            if( p.decision == this.overrideDecision &&
                this._input.index() == this.overrideDecisionInputIndex )
            {
                overrideDecisionRoot = (GrammarInterpreterRuleContext)getContext();
            }
        }

        GrammarInterpreterRuleContext ctx = (GrammarInterpreterRuleContext)_ctx;
        if ( decisionStatesThatSetOuterAltNumInContext.get(p.stateNumber) ) {
            ctx.outerAltNum = predictedAlt;
            Rule r = g.getRule(p.ruleIndex);
            if ( atn.ruleToStartState[r.index].isLeftRecursiveRule ) {
                int[] alts = stateToAltsMap[p.stateNumber];
                LeftRecursiveRule lr = (LeftRecursiveRule) g.getRule(p.ruleIndex);
                if (p.getStateType() == ATNState.BLOCK_START) {
                    if ( alts==null ) {
                        alts = lr.getPrimaryAlts();
                        stateToAltsMap[p.stateNumber] = alts; // cache it
                    }
                }
                else if ( p.getStateType() == ATNState.STAR_BLOCK_START ) {
                    if ( alts==null ) {
                        alts = lr.getRecursiveOpAlts();
                        stateToAltsMap[p.stateNumber] = alts; // cache it
                    }
                }
                ctx.outerAltNum = alts[predictedAlt];
            }
        }

        return predictedAlt;
    }
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号