[antlr-interest] change token type?

Kendall Shaw kshaw at kendallshaw.com
Fri Jul 31 16:31:09 PDT 2009


Here's my non-working toy grammar:

grammar test;

tokens {
    CAT = 'cat' ;
    LP = '(' ;
    RP = ')' ;
}

them
    : (cat)=> cat
    | NAME
    ;

cat : CAT LP RP ;

NAME : 'a'..'z'+ ;

The lexer insists that "cat" is a CAT instead of a NAME. In the real
grammar, all keywords can also be identifiers. How can this be done?
None of the examples I've seen so far really fit this case.

I tried:

cat : c:CAT {if(!input.LT(1).getText().equals("[")) c.setType(NAME);} LP RP ;

but that doesn't seem to work.




More information about the antlr-interest mailing list