[antlr-interest] breaking out of a closure loop

Terence Parr parrt at jguru.com
Wed Dec 12 00:29:56 PST 2001

On Tuesday, December 11, 2001, at 02:43  PM, mzukowski at bco.com wrote:

> 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
> )+


( {pred}? ... )*

an idiom or something that always includes the pred in the loop 
termination?  Don't have time to check at the moment ;)  You're right if 
not.  You would need something following the loop, generally, or an alt 
of the loop to be ambiguous with the lookahead of the alt with the pred.

> 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

Not sure what the analysis would do with that. Hmm....not sure it would 
be legal without destroying the validity of the grammar analysis.

> 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.

My original thought was: just specify semantic validity--antlr will 
decide whether it needs to and can use the pred in a decision.  Kinda 
like just adding assertions, right?  OTOH, people only put them in when 
they want it to help disambiguate.  Perhaps only disambiguating preds 
are useful.



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

More information about the antlr-interest mailing list