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. Jim > -----Original Message----- > From: [email protected] [mailto:antlr-interest- > [email protected]] On Behalf Of Sunil Sawkar > Sent: Thursday, October 14, 2010 11:15 AM > To: [email protected] > 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 out > 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 List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address -- You received this message because you are subscribed to the Google Groups "il-antlr-interest" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en.
