[antlr-interest] BUG? - Antlr hoists unhoistable predicates

Johannes Luber jaluber at gmx.de
Mon Oct 8 02:25:19 PDT 2007


Austin Hastings wrote:
> I was trying to slim down my grammar a bit by combining some rules into
> a single parameterized rule.
> 
> decl_specifiers[Boolean permitStorageClass, Boolean permitFunctionSpec]
>    : ( accs+=decl_access_qualifier
>      | type+=decl_type_spec
>      | stcl+=decl_storage_class
>      | {$permitFunctionSpec}? func+=decl_function_spec
>          )+
>    /* blah blah blah */
> 
> This would have eliminated several nearly-identical variants.
> 
> Unfortunately, Antlr3 hoisted the predicate out of the rule. This
> doesn't work, obviously, because the $permitFunctionSpec is a parameter
> to the rule (which the predicate was hoisted out of...).
> 
> In cases like this, Antlr needs to catch the impossibility (detecting
> the use of a rule- or token- field seems a good start) and either not
> hoist, or construct a synthetic predicate with the correct values.
> 
> =Austin

This isn't a bug, but a design decision. Instead using a parameter use
either a field of the class or - recommended - use a dynamic scope instead.

Best regards,
Johannes Luber


More information about the antlr-interest mailing list