[antlr-interest] identifiers that are not allowed to be like keywords

Martin Probst mail at martin-probst.com
Fri Jun 9 07:19:21 PDT 2006


Hi,

> I am still working on the Java grammar for antlr v3 and I do not know
> how to make the following "happen" (copied from the Java language
> specification and totally independent from the parser/lexer question
> because of the capatilized first character):
>
> Identifier:
> IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral
>
> IdentifierChars:
> JavaLetter
> IdentifierChars JavaLetterOrDigit

Don't worry, ANTLR does this for you automatically (in fact, it's  
really difficult to get a different behaviour). Sequences of  
characters will get the ID token type (or something similar). Then  
they will be tested against the so called Literals table, which  
contains stuff like 'true'. If it matches, the token type is changed  
from ID to something different, e.g. LITERAL_true. A rule like:

identifier: ID;

will not match that token then, and everything is fine for you. You  
can read about that in the manual by looking for the option  
"testLiterals".

Martin


More information about the antlr-interest mailing list