[antlr-interest] Predicate hoisting pain

Sam Barnett-Cormack s.barnett-cormack at lancaster.ac.uk
Mon Apr 13 04:32:46 PDT 2009


Hi all,

So, in my grammar I have need to re-use rules so they ultimately refer 
to a different rule (so I don't have to duplicate 
intersection/union/exception rules). I use a parameter and gated 
predicates, like so:

elements[boolean os]
   : {!$os}?=>subtypeElements
   | {$os}?=>objectSetElements
   | LPAREN! elementSetSpec[$os] RPAREN!
   ;

This is ultimately referred to from two places. The first, which 
generates code that's just fine, is:

elementSetSpecs
   : rootElementSetSpec[false] (COMMA EXTMARK (COMMA 
additionalElementSetSpec[false])?)?
   -> ^(ELEMENTSET rootElementSetSpec EXTMARK? additionalElementSetSpec?)
   ;

However, in the *slightly* more complex case:

objectSetSpec
   : rootElementSetSpec[true] (COMMA EXTMARK 
additionalElementSetSpec[true]?)?
   | EXTMARK (COMMA additionalElementSetSpec[true])?
   ;

The predicates get hoisted in the generated code, and then there's 
compile errors with undefined variable 'os'.

I'm not sure why it happens in one case and not the other, and I'm even 
less clear on how to fix it. Can anyone help?

Sam


More information about the antlr-interest mailing list