[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Token types with constructor
From: |
Wolfgang Wieser |
Subject: |
Re: Token types with constructor |
Date: |
Thu, 2 Sep 2004 18:56:33 +0200 |
Hello Martin & Hans!
On Tuesday 31 August 2004 22:03, you wrote:
> [...lots of text about token types...]
>
So I don't really see why you seem to make things so difficult...
Especially since it is really easy to quickly call the C++ constructors and
destructors from an (otherwise plain C) [parser] code:
I assume the parser allocates a stack of token elements using:
Tok *stack = (Tok*)malloc(N*sizeof(Tok));
Okay, so when we need a token, then let's use one of the array elements
but don't forget to call the constructor before. This could be done from
within the push (shift) operation:
Tok *t = new(&stack[stack_top_index]) Tok();
(operator new with placement syntax, see Stroustroup!)
This requires that the token type used inside bison (i.e. not the
user-supplied one), has a custom operator new:
struct Tok
{
short yyss;
YYSTYPE yyvs;
void *operator new(unsigned int /*size*/,void *ptr)
{ return(ptr); }
};
And when the token is no longer used, "delete" it again, i.e. call the
destructor:
t->Tok::~Tok();
Okay, and the rest of the memory management is as usual:
free(stack);
In case the stack needs to be enlarged, we simply do:
Tok *newstack = (Tok*)malloc(newsize*sizeof());
for(int i=0; i<oldsize; i++)
{
new(&newstack[i]) Tok(); // constructor
newstack[i] = oldstack[i]; // copy
oldstack[i].Tok::~Tok(); // destructor
}
free(oldstack);
I've been doing things like that several times and it worked well.
Just write two #defines which do the ugly tasks for us. For normal C
parsers, these #defines are no-ops (making sure that the C compiler does
not stumble over operator new, etc) and for C++, we use the implementations
above. What do you think? (It's teaching C code C++ behaviour :)
Regards,
Wolfgang
- Re: Token types with constructor, (continued)
- Re: Token types with constructor, Akim Demaille, 2004/09/03
- Re: Token types with constructor, Hans Aberg, 2004/09/04
- Re: Token types with constructor, Akim Demaille, 2004/09/13
- Re: Token types with constructor, Hans Aberg, 2004/09/13
- Re: Token types with constructor, Akim Demaille, 2004/09/14
- Re: Token types with constructor, Hans Aberg, 2004/09/14
Re: Token types with constructor, Wolfgang Wieser, 2004/09/03
Re: Token types with constructor, Hans Aberg, 2004/09/02
Re: Token types with constructor, Martin Trautmann, 2004/09/02
Re: Token types with constructor,
Wolfgang Wieser <=
Re: Token types with constructor, Hans Aberg, 2004/09/02
Re: Token types with constructor, Akim Demaille, 2004/09/03
Re: Token types with constructor, Martin Trautmann, 2004/09/09