[antlr-interest] How to get tokens to come out as (char *) types

Jim Idle jimi at temporal-wave.com
Thu Oct 14 11:58:07 PDT 2010

Don't do this. You will create lots of ANTLR strings that you don't need.

Instead, when you want to use the text of the token, use the pointers in the
token (start and end not start and length) and the knowledge of the input
encoding and create the Cstring directly. The $text is just a convenience
method in the C target - you should use your own  methods when doing
something non-trivial.

Also, remember to only call external Helper methods from your parsers/tree
walkers. Do not embedded any code other than the calling code and pass the
whole tree or token pointer. This means your calls won't care what gets done
by the helper API and the helper API will not care how the parsers decided
to call it. Anything else is an unmaintainable mess.


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Sunil Sawkar
> Sent: Thursday, October 14, 2010 11:15 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] How to get tokens to come out as (char *) types
> Hi,
> I am using the following options for my grammar:
> options {
> 	language=C;
> 	output = AST;
> 	backtrack = true;
> }
> I would like to use  TokenLabelType  option to get all my tokens to come
> as (char *) type to be easily manipulated in "string" class. For now I am
> using this cast, for example:   (char *)$ID.text->chars.
> I don't understand the usage TokenLabelType=MyToken; suggested in the
> Definitive ANTLR reference.
> Can anyone suggest a solution to avoid casts in hundreds of places ?
> Appreciate the help..
> -Sunil S
> (New to ANTLR)
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address

More information about the antlr-interest mailing list