[antlr-interest] Changing Token Names

Jim Idle jimi at temporal-wave.com
Wed Apr 1 08:55:20 PDT 2009


Paul Bouché (NSN) wrote:
> Hi,
>
> I would like to change the token names for error display.
>
> I used have this:
> value : simpleValue | '{' property '}';
> where a token type for the curly braces was created by ANTLR (T_XXX), 
> but if it was missing then in the error message a '{' was generated 
> because for that token in the Java target tokenNames contained a '{'.
> Now I needed to create tokens for the brackets, but I would still like 
> the tokenName display as '{'. I solve this now by a static initializer 
> which alters the tokenNames array - this is kind of a hack - is there 
> another way? In ANTLR v2 there was the paraphrase option?
>   
You have to not use the literals in the parser and create lexer tokens, 
then the token name will be fixed. Then you can use the array that ANTLR 
generates for token names that make sense, and override with a switch or 
a Map lookup for those tokens that don't. The problem with literals is 
that if you add one somewhere in the parser, then all the Tnnn names 
will change and break your messages.

There is no paraphrase option in v3, so you are otherwise doing the 
right thing.

Jim


More information about the antlr-interest mailing list