[antlr-interest] Literals, Predicates and Actions

Gavin Lambert antlr at mirality.co.nz
Fri Mar 6 14:36:07 PST 2009


At 10:27 7/03/2009, Julian Mensch wrote:
 >> I tend not to use the ident method, I just use
 >> an identifier rule that allows the keywords.
 >
 >I don't understand what you're talking about here. You
 >mean you just use a single IDENT token and check for
 >keywords with syntactic predicates that test token
 >attributes?

No, I'm reasonably sure that he means the exact opposite :)

<http://www.antlr.org/wiki/pages/viewpage.action?pageId=1741>

As shown here, there are two common strategies to solve the 
keywords-as-identifiers problem.

The first is to define everything as an IDENTIFIER at the lexer 
level, and figure out which ones are actually keywords at the 
parser level (using semantic predicates).

The second is to treat everything as individual keywords/tokens at 
the lexer level; but in the parser, allow those keywords to be 
used in a context where an identifier is expected.  (This usually 
doesn't require any predicates at all, though it depends on how 
ambiguous your language is.)

I favour the second approach; I'm fairly sure that Jim was saying 
the same thing.



More information about the antlr-interest mailing list