[antlr-interest] token name from token type

Ron Hunter-Duvar ron.hunter-duvar at oracle.com
Tue Mar 30 09:14:46 PDT 2010


I ran into the same problem. I may be wrong, but it seemed like 
functionality that was intended to be implemented in Antlr but was never 
finished, at least in the Java run-time. I just hacked together a method 
that uses introspection to pick out all the token type declarations 
("public static final int XXX = nnn") at startup and put them in a hash 
table. There might be a better way to do it, but this was easy enough 
and got the job done.

Ron


Ketan Maheshwari wrote:
> Hello (Again)
>
> I am playing with the CommonTree object. It has a method called getType()
> that gives an int for token. How do I get the actual token name from that
> int?
>
> I saw in the archives that this question is asked but could not really find
> the method String getTokenName(int) as answered in that post. Instead, I
> find the method String[] getTokenNames() which is not suitable to my need.
>
> Also here in the javadoc for Parser class, a method called String
> getTokenName(int ) appears but it does not seem to be available in my parser
> class which is an infact extended from parser class.
>
> Thanks for your answers.
>
> Ketan
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>   

-- 
Ron Hunter-Duvar | Software Developer V | 403-272-6580
Oracle Service Engineering
Gulf Canada Square 401 - 9th Avenue S.W., Calgary, AB, Canada T2P 3C5

All opinions expressed here are mine, and do not necessarily represent
those of my employer.



More information about the antlr-interest mailing list