[antlr-interest] C target - initialization of return/scope structures

Cristian Târşoagă cristian.tarsoaga at gmail.com
Wed May 19 14:08:06 PDT 2010


Hi All,

My name is Chris, I started to use antlr and I like it a lot!
I use C++ and I have successfully used it to generate some sourcecode.

I need to use C++: I want std::string, std::vector and more things like
this.
But since I use the C target, it didn't took too much time to get into some
quirks though.

One of the problems I had/have is this: structures used for return values
and those used for scope values are NOT initialized.

Since I tried to use a std:string as a scoped value, I quickly got a nice
crash since my string was created using malloc.

These are (well) known problems, I know that. I found some posts from other
guys having the same problems.
I also found some recommendations on how to avoid initialization problems.
E.g:
http://www.mail-archive.com/il-antlr-interest@googlegroups.com/msg02614.html

The hint there was to use pointers, and:

1. define ANTLR3_MALLOC / ANTLR3_FREE to override antlr's allocators

or

2. manually allocate/deallocate those pointers, probably inside @init and
@after


I'd like to have a clean solution to this, but I can't see how any of these
two options can properly work.


Option 1: I can't override antlr allocator like suggested
#define ANTLR3_MALLOC(request) new request()
because ANTLR_MALLOC is actually called with an argument which is actually
the SIZE of the type that will be allocated and not the TYPE itself.
I think a simple change inside antlr can fix this, but until then.... I
tried the other way...


Option 2: I can't use @init and @after because this will create memory
leaks.
Imagine that I have a scoped value x. I would do @init {x = new X();} and
@after{delete x;}
When rule is fully matched, this works perfectly.
But when the parser fails, the code the pops the scoped value from the stack
is called (and my piece of code inside @after is skipped) so I will get a
memory leak!!
I noticed that the scoped values also have a free function pointer inside
(member), that can take care of deallocation in that situations, but I
couldn't find a way to set it. (?)


So:
- my suggestion: change the ANTLR_MALLOC macro (change the name to
ANTLR_ALLOC and change the impl to take as arg the type itself, so that a
c++ impl could override it with 'new')
- my suggestion: generate a properly initialized structure (I know, it's C
code, but still...once you have such a smart StringTemplate lib, this
shouldn't be a problem)
- my question: what would be a clean way to allocate/deallocate pointers
(without leaks)?


THANKS a lot for ANTLR and for your help!

   Chris


PS: I have some other problems too with the C target: I wasn't able to use
composite grammars with C++. I will get back on this later :-)


More information about the antlr-interest mailing list