[antlr-interest] ANTLR 3.0b5 problem with predicates

Terence Parr parrt at cs.usfca.edu
Thu Nov 23 08:35:19 PST 2006


Hi.  That predicate says that no matter what, the 2nd lookahead  
symbol cannot be '/'.  This breaks when you exit.  You want =='/'  
not !='/' right?  Further, this is an old style v2 idiom.  Use  '/ 
*' (options {greedy=false;} : .)* '*/' and you're good!
ter
On Nov 23, 2006, at 4:20 AM, Shmuel Siegel wrote:

> 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