Gary R. Van Sickle wrote:
From: Richard Thrippleton

When I have a rule of the form

"rule returns [std::vector<int> foo]
..."

    

Unfortunately, you can't do that.  Under the hood, in the C runtime, rules
return a malloc()'ed struct, 
No, it doesn't, it returns a struct, declared in the rule that is called.
so your vector<>'s constructor never gets
called and you end up with a bit of a mess.  The only way to make this work
at the moment is to do something like this:

rule returns [std::vector<int> *foo]
@init
{
	foo = new std::vector<int>;
}

There was talk a week or two ago on the list regarding replacing explicit
calls to malloc() with either a redefineable macro (e.g.
ANTLR_MALLOC_OR_NEW_YOU_DECIDE()), or (my preference) something to the
effect of:

#ifdef __cplusplus
// We're being built as C++.
#define ANTLR_NEW() new ...
#else
/* We're being built as C */
#define ANTLR_NEW() malloc ...
#endif

This would allow what you have above to "just work".  I don't know what if
anything became of it however.
  
It would not work, these things are not allocated and you don;t want them to be either.
 
  
the generated code for that rule will start with

"std::vector<int> foo = NULL;"

    

Really, it adds that "= NULL" of its own volition?  That sounds like a bug.
All kinds of C PODs (structs for instance) could be in there, none of which
could be pointers, and that should then fail on C as well as C++.  What
version is this?
  
ANTLR has always initialized things and it does so via a table. I don't have control at the finer level, but anyway, the thing is that you need to use pointers anyway and so initializing to NULL is not an issue. I need to get this into the docs I guess.

There isn't anything that you can legitimately put in to the return that cannot be initialized to NULL basically, so if you get this error, it means you need to look at what you are putting into the return/scope/whatever structure and change it to something that can be initialized to NULL. The semantics will be trivially different for your code, but will follow the gestalt of ANTLR and the way the C runtime rules are implemented. There is always a tradeoff between performance (which is what we want) and flexibility to use any kind of structure/declaration in the target language, but here we are programming a grammar, not a standalone C or C++ program and so there are rules to follow that are more restrictive than a freeform C/C++ program.

Jim

Jim
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to