[antlr-interest] Semantic predicates

Andreas Meyer andreas.meyer at smartshift.de
Fri Oct 16 14:08:37 PDT 2009


Robert Wentworth schrieb:
> There appears to be something fundamental that I am not "getting"  
> about the way semantic predicates work. In the grammar below, the  
> input "cat" is recognized using the rule "cat" but in not recognized  
> by either the rule "expr" or the rule "pexpr". Could someone explain  
> to me why this is so and what the fix is?
>   

I think the generated code is wrong. Looking at the decision-code for 
expr, it seems that the only input expr can accept is plain "dog":

        try {
            if ( state.backtracking>0 && alreadyParsedRule(input, 1) ) { 
return retval; }
            // C:\\projects\\test\\ATest\\ATest.g:24:6: ( cat | cat WHEN 
dog | dog )
            int alt1=3;
            int LA1_0 = input.LA(1);

            if ( (LA1_0==WORD) && 
((input.LT(1).getText().equals("dog")))) { ... }
            else { throw ... }
 
and indeed, using ANTLRWorks, dog is accepted.


More information about the antlr-interest mailing list