[antlr-interest] ANTLR 3.0b5 problem with predicates

Shmuel Siegel ssiegel at finjan.com
Thu Nov 23 04:20:33 PST 2006


I am having problems with this simple rule. It throws a failed predicate
exception at the end of any multi-line comment.

 

MLCOMMENT

      :     '/*'

            (

                  { input.LA(2)!='/' }? '*'

            |     ~('*')

            )*

            '*/'

            {$channel=99;}

      ;

 

It produces the (reformatted) following code. Please note that the
produced code will execute case 1 for asterisk whether or not the
asterisk is followed by a slash. But case 1 throws an exception if the
asterisk is followed by a slash. The code was produced with AntlrWorks
1.0b6

 

loop12: do

{

            int alt12 = 3;

            int LA12_0 = input.LA(1);

            if ((LA12_0 == '*'))

            {

                  int LA12_1 = input.LA(2);

                  if ((LA12_1 == '/'))

                  {

                        int LA12_3 = input.LA(3);

                        if (((LA12_3 >= '\u0000' && LA12_3 <=
'\uFFFE')))

                        {

                              alt12 = 1;

                        }

                  }

                  else if (((LA12_1 >= '\u0000' && LA12_1 <= '.') 

|| (LA12_1 >= '0' && LA12_1 <= '\uFFFE')))

                  {

                        alt12 = 1;

                  }

            }

            else if (((LA12_0 >= '\u0000' && LA12_0 <= ')') 

|| (LA12_0 >= '+' && LA12_0 <= '\uFFFE')))

            {

                  alt12 = 2;

            }

 

            switch (alt12)

            {

            case 1:

            // {...}? '*'

            {

                  if (!(input.LA(2) != '/'))

                  {

                        if (backtracking > 0)

                        {

                              failed = true;

                              return;

                        }

                        throw new FailedPredicateException(input,

                              "MLCOMMENT", " input.LA(2)!='/' ");

                  }

                  match('*');

                  if (failed)

                        return;

            }

                  break;

            case 2:

            // ~ '*'

            {

                  if ((input.LA(1) >= '\u0000' && input.LA(1) <= ')')

                              || (input.LA(1) >= '+' && input.LA(1) <=
'\uFFFE'))

                  {

                        input.consume();

                        failed = false;

                  }

                  else

                  {

                        if (backtracking > 0)

                        {

                              failed = true;

                              return;

                        }

                        MismatchedSetException mse = 

new MismatchedSetException(null, input);

                        recover(mse);

                        throw mse;

                  }

            }

                  break;

            default:

                  break loop12;

            }

} while (true);



More information about the antlr-interest mailing list