[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: stl constructs inside the union block
From: |
lfinsto1 |
Subject: |
Re: stl constructs inside the union block |
Date: |
Fri, 5 Oct 2007 11:10:04 +0200 (CEST) |
User-agent: |
SquirrelMail/1.4.9a |
The `%union' declaration for GNU 3DLDF looks like this:
%union
{
char string_value[64];
double real_value;
signed int int_value;
unsigned long long ulong_long_value;
void* pointer_value;
};
I use the members of the union other than `pointer_value' for the values
of symbols where it's convenient to let Bison handle memory management for
me. Where I use `pointer_value', it's often for temporaries. In this
case, the object it points to is either used for the value of a rule, or
it's deleted before the end of the action. If it's not, it's a memory
leak and I fix it if I find it. However, it may also appear on the
right-hand side of an assignment, in which case it "disappears" from the
rules but is "attached" to an object whose scope extends beyond that of
`yyparse'.
This makes it unnecessary to implement a special garbage-collecting
routine, since memory is immediately recovered when temporaries are
discarded. One does need to remember to call `delete'. I do occasionally
find places where I've forgotten to do this, usually in older code from
before I really had gotten the hang of using Bison.
In addition, if your parser may grow to be very large, I recommend putting
code into functions to reduce the size and compilation time, which can
become rather long. I eventually had to turn off optimization, because it
was taking up to 10 minutes on a fast machine. It's a lot easier to put
code into functions as one goes along, rather than trying to do it later,
as I know to my cost. Turning off compilation is a bit tricky if you're
also using `Automake', but I think this tangent has gone far enough. If
you have this problem, let me know.
Laurence Finston