[antlr-interest] Semantic predicates

David-Sarah Hopwood david-sarah at jacaranda.org
Fri Oct 16 16:12:01 PDT 2009


Andreas Meyer wrote:
> Andreas Meyer schrieb:
>> Robert Wentworth schrieb:
>>   
>>> There appears to be something fundamental that I am not "getting"  
>>> [...] 
>>>     
>> I think the generated code is wrong. [...]
>>   
> btw, when changing the gated semantic predicates:
>>> cat     : {input.LT(1).getText().equals("cat")}?=> WORD ;
>>>
>>> dog     : {input.LT(1).getText().equals("dog")}?=> WORD ;
>>   
> into plain semantic predicates
>>> cat     : {input.LT(1).getText().equals("cat")}? WORD ;
>>>
>>> dog     : {input.LT(1).getText().equals("dog")}? WORD ;
>>   
> the generated code looks good and expr can accept cat, cat when dog and 
> dog. Now, as the other poster already mentioned, gated semantic 
> predicates are there to distinguish between alternatives (that are not 
> syntactically ambiguous). Maybe that's where ANTLR (or the user?!) get's 
> confused, because cat only has one alternative (which get's effectively 
> disabled if the predicate fails), but the context in which it is used 
> has more alternatives, which effectively boil down to the same gated 
> predicates.

I think we have to interpret this as an ANTLR bug, not the grammar writer
being confused. Conceptually, a gated semantic predicate with a single
alternative has a clear meaning: if the predicate fails then it should be
equivalent to a rule that never matches. It would be very strange for it
to mean anything else.

-- 
David-Sarah Hopwood  ⚥  http://davidsarah.livejournal.com



More information about the antlr-interest mailing list