[antlr-interest] Problems with semantic predicates

Gavin Lambert antlr at mirality.co.nz
Thu Apr 17 13:31:04 PDT 2008


At 04:02 18/04/2008, Haralambi Haralambiev wrote:
>r2[boolean flag]
>:{flag}? Token1 -> ^(NODE Token1)
>|Token1
>;
[...]
>In the example above - the method synpred2_fragment() contains a 
>statement if ( !(flag) ) but the variable flag cannot be resolved.

As Johannes has already said, the problem is that the predicate is 
getting hoisted.  However, I think there's another way to get it 
to work -- rewrite the rule so that it doesn't *need* to hoist the 
predicate:

r2[boolean flag]
   :  Token1
      ( {$flag}? => -> ^(NODE Token1)
      | -> Token1
      )
   ;

(I haven't tested this, but it seems plausible.)



More information about the antlr-interest mailing list