help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Token types with constructor


From: Martin Trautmann
Subject: Re: Token types with constructor
Date: Fri, 03 Sep 2004 00:35:34 +0200

Hi,

Am Donnerstag, den 02.09.2004, 19:38 +0200 schrieb Hans Aberg: 
> > that allows using Token Classes with
> >constructor. This works via yyoverflow and a solution of type "3a"
> ...
> >Like this no memory should be left. The memory originally allocated by
> >bison on the stack of yyparse is freed automatically calling
> >destructors.
> >And as I already described all other copying of Tokens uses the
> >approriate operator=().
> 
> But you still need to make a C++ copy over when the stack re-allocates.
> Check, for example, the suggestion by Wolfgang Wieser.
> 
OOps. I forgot the copy loop.
But with established solution I mean the yyoverflow construction that
seems to be introduced exactly for the purpose we need it.
I don't want to change the parser skeletons any more. From the memory
organization it's solution "3a" but without being "3" as the default
skeleton is sufficient.

Corrected version of the code:
==============================
// open namespaces
namespace funcgen
{
  short *parserstack_yyss = 0;
  Token *parserstack_yyvs = 0;
  
# define yyoverflow handle_stack_overflow
  void init_stack()
  {
    parserstack_yyss = 0;
    parserstack_yyvs = 0;
  }
  void free_stack()
  {
    if( parserstack_yyss != 0 ) delete[] parserstack_yyss;
    if( parserstack_yyvs != 0 ) delete[] parserstack_yyvs;
    init_stack();
  }
  void handle_stack_overflow( char*, short ** yyss, size_t, Token
**yyvs, size_t, size_t *yystacksize )
  {
    if( parserstack_yyss != 0 ) assert( *yyss == parserstack_yyss );
    if( parserstack_yyvs != 0 ) assert( *yyvs == parserstack_yyvs );
    size_t new_yystacksize = *yystacksize * 2;
    short *new_yyss = new short[new_yystacksize];
    Token *new_yyvs = new Token[new_yystacksize];
    for(size_t i=0; i<*yystacksize; ++i )
    {
      new_yyss[i] = (*yyss)[i];
      new_yyvs[i] = (*yyvs)[i];
    }
    *yyss = new_yyss;
    *yyvs = new_yyvs;
    *yystacksize = new_yystacksize;
    free_stack();
    parserstack_yyss = new_yyss;
    parserstack_yyvs = new_yyvs;
  }
%{
...rules...
%}
  parse_adl(...)
  {
    init_stack();
    ret = yyparse(...);
    free_stack();
  }
===================
I tested this with using an initial stack size of 2. It was increased
several times until 32 in my tests.

bye

Martin
-- 






reply via email to

[Prev in Thread] Current Thread [Next in Thread]