[antlr-interest] breaking out of a closure loop

mzukowski at bco.com mzukowski at bco.com
Tue Dec 11 14:43:13 PST 2001


Ter, you need a disambiguating semantic predicate, yes?  Otherwise an
exception is thrown on the assumption that it is a validating predicate.  So
you have to have another alternative there.  I think the idiom I used was:

(
	{keepGoing}? otherRule
	| SOME_IMAGINARY_NEVER_GENERATED_TOKEN
)+

SOME_IMAGINARY_NEVER_GENERATED_TOKEN gives the loop a non-guarded
alternative which will never be matched (because the lexer never generates
it) but gives the semantic predicate an alternative so the predicate is
disambiguating instead of validating.  When keepGoing is false then no
viable alternative will be found and the loop will break appropriately.

For antlr 3 would it be possible to incorporate a $break() as an action?  Or
some other appropriate distinction between a validating and disambiguating
predicate?  People always get confused between those two anyways.  And even
in a loop with only one alternative in the loop it is appropriate to have a
disambiguating predicate to break the loop.

Actually the entire role of a validating predicate merits some thinking.  I
don't think I have personally used them.  However, they could be part of a
saner, broader error handling mechanism.

Monty


> -----Original Message-----
> From: Terence Parr [mailto:parrt at jguru.com]
> Sent: Tuesday, December 11, 2001 1:38 PM
> To: antlr-interest at yahoogroups.com
> Subject: Re: [antlr-interest] breaking out of a closure loop
> 
> 
> 
> On Tuesday, December 11, 2001, at 11:36  AM, praveenray wrote:
> 
> > Is it possible to break out of a ()* or ()+ loop explicitly? I mean,
> > Can I use 'break' statement inside the loop like this:
> >
> > class myParser extends Parser;
> > somerule
> > { bool bQuit=false; }
> >       :  SOME_TOKEN
> >             (
> >               bQuit = otherRule
> >               { if (bQuit) break; }
> >             )+
> >            END_TOKEN
> >         ;
> > otherRule returns [bool bRet]
> > { bRet = false; }
> >                              : A B
> >                                ( C | D {bRet=true;}
> >                                );
> >
> >  Will it have any side effects? If yes, what is a safe way to break
> > out of a loop after I have seen some special character?
> 
> "break" works but is not a good idea.  The grammar analysis 
> has no idea 
> what you do in actions.  Better to use a semantic predicate 
> {!isSpecialChar(...)}? to tell it when to terminate.
> 
> Ter
> --
> Chief Scientist & Co-founder, http://www.jguru.com
> Creator, ANTLR Parser Generator: http://www.antlr.org
> 
> 
>  
> 
> Your use of Yahoo! Groups is subject to 
> http://docs.yahoo.com/info/terms/ 
> 
> 
> 

 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list