[antlr-interest] Predicate hoisting pain

Sam Barnett-Cormack s.barnett-cormack at lancaster.ac.uk
Mon Apr 13 05:55:13 PDT 2009


Sam Barnett-Cormack wrote:
> I guess the question really is, for me, why does it get hoisted in one 
> case and not another? I'm assuming it's because of the choice in the 
> objectSetSpec rule, and I can't see any way to refactor that to lose the 
> choice. Of course, there's probably a kludgy solution of setting a 
> variable os to true and passing it...

And that worked... changed it to:

objectSetSpec
@init{
   boolean os = true;
}
   : rootElementSetSpec[os] (COMMA EXTMARK additionalElementSetSpec[os]?)?
   | EXTMARK (COMMA additionalElementSetSpec[os])?
   ;

Horrible, but in some ways less horrible than the more "elegant" solutions.

>> -----Original Message----- From: antlr-interest-bounces at antlr.org 
>> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Sam 
>> Barnett-Cormack Sent: Monday, April 13, 2009 6:33 AM To: ANTLR
>> Interest Mailing List Subject: [antlr-interest] Predicate hoisting
>> pain
>>
>> 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
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest 
>> Unsubscribe: 
>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address



More information about the antlr-interest mailing list