[antlr-interest] A Question on FAQ

Gavin Lambert antlr at mirality.co.nz
Thu Dec 25 15:49:45 PST 2008


At 10:23 26/12/2008, Gokulakannan Somasundaram wrote:
>I was going through the FAQ "How can I allow keywords as 
>identifiers?". The example is like this
[...]
>My doubt is, why can't we write KeyIF and KeyCall as
>
>KEYIF : 'if';
>
>KEYCALL : 'call';
>
>instead of writing like the above one. Or am i just repeating 
>what is already told in the second alternative in the same FAQ?

Pretty much.

The two alternatives mentioned in the FAQ are:

1. Avoid matching the keywords directly in the lexer, instead 
recognising them only as IDs.  Then in the parser (where you have 
context) check the actual text of the ID tokens you find to 
determine whether they're keywords or not.

2. Match the keywords directly in the lexer (so you have IF and 
CALL tokens, and ID tokens are only generated for words that don't 
match a keyword).  Then in the parser you never refer to ID 
directly -- wherever an id may appear you instead refer to a 
parser rule like this:
   id : ID | IF | CALL | ... other keyword tokens ... ;

I tend to prefer this second approach, since I try to minimise the 
number of predicates (especially semantic predicates) used in my 
grammars.  But it does mean that whenever you add a keyword to the 
language you have to add it in two places (which is not the case 
with the first approach).  So which one you go for is mostly just 
a matter of taste.



More information about the antlr-interest mailing list