[antlr-interest] Left factor? Syntactic predicates? Or another solution?

Gavin Lambert antlr at mirality.co.nz
Thu Oct 1 11:44:28 PDT 2009


At 01:24 2/10/2009, Naveen Chawla wrote:
>Thanks so much! Yes, I tried complement: object object? 
>coincidentally just before I read your message and it worked. 
>Good news is that in the real grammar they ARE actually 
>equivalent. But is there no way of making 'indirectObject' and 
>'object' appear as they are in the end structure if there are 2 
>'object's in a row (they have slightly different meanings even 
>though syntactically the same)? Would a syntactic predicate solve 
>this? For example, complement: (indirectObject 
>object)=>indirectObject object | object; Is this the right one? 
>(it doesn't seem to work on ANTLRWorks) Many many thanks, N

Which rules it passes through shouldn't have any bearing on the 
semantic meaning; the rules should just be concerned with matching 
logical units of tokens.

You can assign different meanings to the objects via altering the 
output AST, eg:

complement
   : (o1=object -> ^(DIRECT $o1))
     (o2=object -> ^(INDIRECT_DIRECT $o1 $o2))?
   ;
(There are lots of other possible tree layouts, of course.)

If you're carrying out some action directly rather than outputting 
an AST, you can still use a similar technique (putting some of the 
code inside the optional block) to get different behaviour if the 
second object is actually present.



More information about the antlr-interest mailing list