[antlr-interest] [antlr3c] Need help working with ANTLR3_STRING...

Jim Idle jimi at temporal-wave.com
Mon Aug 3 09:49:26 PDT 2009


Adamic Tomislav wrote:
> Hi
>
> I'm a bit puzzled with ANTLR API for ANTLR3_STRING... I need few
> operations on strings and I could do it either by converting
> ANTLR3_STRING into std::string (or std::wstring) or do it directly.
> Well, I can't figure out either. So, let's say I want to create a copy
> of some already existing ANTLR3_STRING. Is this the proper way of
> doing it, so it doesn't leak memory:
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>   
These are just convenience methods for C programmers. If you are happy 
with the C++ libraries available to you, then just use the pointer and 
length fields (actually I think it is start and stop) fields to work 
with the string directly. The ANTLR3_STRING stuff will auto track memory 
usage and so on, but if you perform lots of string references then it 
can be a memory hog because it does not release anything until the 
parser is freed.

Get a token reference:  mytok=TOKEN

Then create a factory method or a function or whatever that takes a 
token pointer and creates your C++ string. Do not use the $text 
references as these will create ANTLR3_STRING that you don't need.

Jim


More information about the antlr-interest mailing list