[antlr-interest] Predicate hoisting pain

Sam Barnett-Cormack s.barnett-cormack at lancaster.ac.uk
Mon Apr 13 09:11:01 PDT 2009


Jim Idle wrote:
> However, as you can obviously distinguish the cases at some point higher 
> up the rule chain, then if you wish to pursue this, then all you need do 
> is create a scope with your flag in it at a high enough level, init it 
> to the default case, then set/unset it as the rules descend, then use it 
> as the gated predicate in your rule above:
> 
> highuprule
>     scope
>      { boolean os; }
>     @init { $highuprule::os = false; }
> : rule rule rule ... ;
> 
> ...
> 
> ruleX :  X  Y (Z { $highuprule::os = true; }  objectSetSpec)? // Z 
> present means flip the flag
> ;
> 
> objectSetSpec
>    : {$highuprule:os}?=>additionalSetSpec
>    | something else
>    ;

It's more that it would have to be changed on the way down the parse 
tree, and changed back on the way back. So ruleX would be more like (if 
this would work):

ruleX
@init {
   boolean os = $highuprule::os;
}
   : X Y ({$highuprule::os = true;} ruleZ {$highuprule::os = os;})?
   ;

If that makes any sense at all.... it needs to be able to change it for 
the duration of ruleZ (and stuff under ruleZ might change it as well for 
things under themselves) while changing it back to whatever it was on 
entering ruleX after it finishes with ruleZ. All assuming that X Y 
(ruleZ)? wouldn't be ambiguous, of course.

Sam



More information about the antlr-interest mailing list