[antlr-interest] Predicate hoisting pain

Sam Barnett-Cormack s.barnett-cormack at lancaster.ac.uk
Mon Apr 13 06:38:26 PDT 2009


Sam Barnett-Cormack wrote:
> Sam Harwell wrote:
>> This will fail unexpectedly with certain types of grammars, as
>> demonstrated by some grammars I've sent to the list in the past. I'll
>> find them later today and send a link.
> 
> I can't see why it'd fail horribly in this case... the problem would 
> come if os were being used in a higher rule for something entirely 
> different and the predicate got hoisted.
> 
> In this case, the predicates getting hoisted in just wasteful. It's 
> checking loads of stuff unnecessarily, because it's going to call the 
> rule the predicate is in in either case! I think the logic that needs 
> fixing is examining the branches of the choice the predicate got hoisted 
> into, and if it's hoisted into every branch drop it again. That would 
> solve my issue at least, and I can't imagine a situation where it'd be 
> dropped that it would cause a problem.
> 
> The other option is to detect predicates that use a parameter, and do 
> some substitution in the hoist so that the parameter is substituted (as 
> it's the calling rule that sets it). This might leave it with a 
> parameter (of the "outer" rule) in use. However, if you do it repeatedly 
> when hoisting through multiple layers, it'd work out in the end. As it 
> is, if you have "stacked" rules like this passing a parameter, you may 
> get inconsistent results depending on naming,

Come to think of it, the really sensible thing (from a quick think) 
would be to do both of those things. That'd make hoisting "safe" (or at 
least safer) and avoid wasteful things like has just happened.

Sam



More information about the antlr-interest mailing list