help-bison
[Top][All Lists]
Advanced

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

Re: %destructor documentation not quite right...


From: Joel E. Denny
Subject: Re: %destructor documentation not quite right...
Date: Sat, 22 Apr 2006 00:03:14 -0400 (EDT)

On Fri, 21 Apr 2006, Steve Murphy wrote:

> I read the manual entry (for the first time) on the %destructor
> directive, and thought, "Wow! Fantastic! Now I can get rid of the
> 'syntax err == leaked memory' problem!!!!
> 
> But I encountered some bumps in the road!

I recommend you try out 2.1a, which you can download from:

  ftp://alpha.gnu.org/gnu/bison/

We've made some %destructor changes since 2.1, and the documentation no 
longer refers to %destructor as an experimental feature.

> If no syntax errs occur, I already have mechanisms to free the 
> structs that yyparse allocated, AFTER THE APP IS FINISHED USING THEM.
> 
> But, I'm getting calls to yydestruct even when there are no errors.

Did you notice that Bison calls it for the start symbol?  If you're 
building a tree and you've defined a cascading destructor, this might wipe 
out many more values.

> I've included the .info text for the bison manual below... I do not see
> it clearly stated below that just about ALL tokens will be "discarded"
> during the parsing process,

Because it's not true.  Once Bison invokes one of your semantic actions, 
it will not directly invoke destructors for any of that production's RHS 
symbols.  This was almost true in 2.1 (YYABORT and YYACCEPT being the 
notable exceptions), and it should be completely true in 2.1a.  Here's an 
important quote from the 2.1a documentation:

  As a rule of thumb, destructors are invoked only when user actions 
  cannot manage the memory.

> and those which are discarded because of
> errors will have a yymsg argument to yydestruct of "Error:
> discarding.../popping", and those which are discarded because of who-
> knows-what-other-reason (maybe via reduce?) begin with "Cleanup:
> discarding.../popping". So, if I make my code look like this:
> 
> %destructor {  if (yymsg[0] != 'C') free_value($$); }  start expr TOKEN
>                                             TOK_COND TOK_COLONCOLON ...

I recommend you don't depend on this.  yymsg is not documented for access 
by the user.

> Even worse, the text below seems to imply that yydestruct will only be
> called in association with error processing

It mentions destruction of the start symbol value as well.

> , yet the code seems to
> indicate that the calls for "Cleanup" purposes is not a typo, but
> carefully designed to allow the user (me) the option of freeing any or
> all memory as the parser processes it.

No.  Bison uses that `Cleanup' prefix for any destructor calls that don't 
occur during error recovery, in which case there's an `Error' prefix 
instead.  For example, YYACCEPT, YYABORT, or cleanup of the start symbol.

> I'm not complaining that
> yydestruct is called in this manner, only that the documentation hasn't
> been updated to indicate this behavior, and provide relevant examples!

Even though there aren't many examples, I find the 2.1a documentation to 
be fairly clear.  Does it help?

Joel




reply via email to

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