[antlr-interest] Guarded Predicates

David Wigg wiggjd at sbu.ac.uk
Fri Dec 13 06:42:13 PST 2002


Guarded predicates do not appear to do what I expected from them 
as described in the ANTLR documentation (which seemed sensible 
and intuitively correct).

It says the following;

ANTLR provides a guarded predicate to allow you to specify the 
lookahead context under which a predicate should be evaluated. 
The syntax is:

(lookahead-context-for-predicate)=>{predicate}?

A guarded predicate is useful in situations where the semantic 
predicate should be hoisted into the prediction decision only 
when the lookahead is consistent with some context. For example:

a   :   (ID)=>{isType(LT(1))}? (ID|INT)
     |   ID
     ;

Here, the predicate is only applicable when an ID is found on 
the input stream. It should not be evaluated when an INT is found.


So, in the case illustrated above if an INT was next the guarded 
predicate would not be evaluated and parsing would continue 
within the first alternative to (ID|INT).

In fact as far as I can see the effect of the current 
implementation is that unless the predicate is true parsing will 
not fall through to the rest of the alternative anyway.

It's quite simple to put the above rule in a .g file to compile 
it and to examine the resulting generated code.

I find it rather ominous that I have only been able to find any 
examples of guarded predicates in TinyBasic.g in the java 
examples. Does this mean that other people have found that 
guarded predicates don't work?

Needless to say I have not found anything about this in the 
FAQs. "No results found" was the response to a search for "Guarded".

David.








abstract_declarator
   :  (SCOPE|ID)=> {qualifiedItemIsOneOf(qiPtrMember)}?
      ptr_operator abstract_declarator {declaratorPointerTo();}
   |  LPAREN abstract_declarator RPAREN
      (abstract_declarator_suffix)+
   |  (LSQUARE (constant_expression )? RSQUARE  {declaratorArray();}
      )+
   |  /* empty */
   ;


 

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



More information about the antlr-interest mailing list