[antlr-interest] problem with keywords-as-identifiers

Andy Tripp antlr at jazillian.com
Wed Sep 5 14:03:02 PDT 2007


I'm having a problem...in the language I'm parsing, keywords are allowed 
as identifiers,
so for each keyword, I have a of rule like this:

keyPROPERTY:
    {validateIdentifierKey("PROPERTY")}? value=Identifier
    -> PROPERTY[$value]
    ;

And then, of course, other rules that refer to these rules like this:

propertySetDeclaration:
    modifiers? keyPROPERTY 'SET' Identifier (LPAREN formalParamList 
RPAREN)? eos
        statements keyEND keyPROPERTY
    -> ^(PROPERTY_SET_DECLARATION modifiers? Identifier formalParamList?
        statements)
    ;

The problem is that there is no "PROPERTY" keyword in the input, yet the 
generated
keyPROPERTY() method is not setting the "failed" variable to true. The 
generated parser
code looks like this:

    public final keyPROPERTY_return keyPROPERTY() throws 
RecognitionException {
       try {
            ...
            if ( !(validateIdentifierKey("PROPERTY")) ) {
                if (backtracking>0) {failed=true; return retval;}
                throw new FailedPredicateException(input, "keyPROPERTY", 
"valida
teIdentifierKey(\"PROPERTY\")");
            }
            ...
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        ...
        return retval;
}

By adding some tracing, I see that my validateIdentifierKey() method is, 
in fact, returning
false. And backtracking is 0, so a FailedPredicateException is thrown 
and then caught
down at the bottom of the method. The "failed" variable is never set to 
false, but it's checked
in the calling method just after the call to keyPROPERTY().

So I'm wondering if the logic in the generated code is right. Isn't 
there a case here where
the keyPROPERTY method is never setting the "failed" variable?

Thanks in advance,
Andy





More information about the antlr-interest mailing list