[antlr-interest] Predicate hoisting/lowering confusion

Wincent Colaiuta win at wincent.com
Tue Jun 19 02:53:31 PDT 2007


El 19/6/2007, a las 2:12, Terence Parr escribió:

> On Jun 19, 2007, at 1:29 AM, Wincent Colaiuta wrote:
>> parser code I can see how it gets "hoisted" up into the DFA of a  
>> calling rule. The thing which puzzles me is that it is also being  
>> "lowered" down into a rule further down the chain, and most  
>> confusingly of all, the sense of the predicate is inverted in the  
>> lower rule:
>
> I believe it's because FOO+ requires that it figure out what  
> follows it to determine if it should exit.  That means FOLLOW(pre),  
> which is FOLLOW(element), which is FIRST(element) which is FIRST 
> (pre).  It sucks the pred back in.

Ok, thanks for the explanation, Ter.

So ANTLR is doing the right thing, but the grammar itself has a  
defect (a rule which can never match for any input). I wonder if it  
would be possible to issue a warning for this case? I suspect not, as  
ANTLR can't know what might be in the predicate and can't know that  
it will never pass... Ah well... Just something to be aware of when  
using predicates!

About the only way I can think of to be aware of this risk is to  
check the grammar without the predicates. That yields the usual  
"Decision can match input such as "FOO" using multiple alternatives:  
1, 2" warning, which although ANTLR resolves it automatically in the  
way that the grammar author probably intended (ie. it does a greedy  
match of FOO+) it should still alert you to the fact that ANTLR is  
trying to "see past" the end of the rule to know when it should exit,  
which in turn means that your predicate could end up sucked back in  
to a place where you didn't expect it...

Cheers,
Wincent



More information about the antlr-interest mailing list