[antlr-interest] Why does ANTLR generate code that will never call an OR'd alternative?

Avid Trober avidtrober at gmail.com
Fri Aug 20 23:01:20 PDT 2010


For this rule,

 

identifier 

                :       isToken | IDENTIFIER;

 

ANTLR generates code that would never calls the isToken rule
(target=CSharp2):

 

    public MYParser.identifier_return identifier()    // throws
RecognitionException [1]

    {   

.

            // .  : ( isToken | IDENTIFIER )

            int alt30 = 2;

            int LA30_0 = input.LA(1);

 

            if ( (LA30_0 == IDENTIFIER) )   // <== token must be IDENTIFIER
to call isToken???

            {

                int LA30_1 = input.LA(2);

 

                if ( ((isToken(input.LT(1)))) )  // <== why must LA30_0 ==
IDENTIFIER to call isToken?

                {

                    alt30 = 1;

                }

                else if ( (true) )

                {

                    alt30 = 2;

                }

.

            else                         //  <== since not IDENTIFIER, why
not call isToken here???

            {

                NoViableAltException nvae_d30s0 =

                    new NoViableAltException("", 30, 0, input);

 

                throw nvae_d30s0;

            }

 

I would think it's something to do with DFA optimization?   Perhaps that's
why IDENTIFIER is checked first.

But, if IDENTIFIER is false, why not call isToken???    Afterall, the rule
is IDENTIFIER  ****OR***** isToken.

 

Thanks,

Trober

 

 



More information about the antlr-interest mailing list