help-bison
[Top][All Lists]
Advanced

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

Re: Exception safety of generated C++ parser


From: Luca
Subject: Re: Exception safety of generated C++ parser
Date: Wed, 19 Sep 2012 21:22:02 +0200
User-agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120827 Thunderbird/15.0

On 13/09/2012 22:52, Oleksii Taran wrote:
13.09.12 19:32, Hans Aberg wrote:
On 13 Sep 2012, at 17:54, Oleksii Taran wrote:

Does Bison guarantees exception safety in C++ parser, i.e. will it make
any clean-up and free memory if an exception is thrown on rule action?
Yes, the default stack is std::deque.

I
know about %destructor directive, but it seems irrelevant, since
destructor code is called only if parser recovers from _syntax_ error.
This is for the C parser, since C does not provide automatic cleanup. I might 
be possible to compile this parser as C++ (supported at least in the past), in 
which case %destructor would be needed.
If "C" the parser doesn't recover from a syntax error context (i.e. the code is syntactically correct), you have to free yourself the memory. I just suggest to create a simple tree inside the actions, and to free it from the leaves up to the root. When a syntax error happens instead, you have to free many sub-trees. Please keep also into account that freeing memory is really needed only when you design an interactive always running parser, like a command line one; for a "batch" compiler the memory will be freed when the process will exit.
But what if deque holds not objects, but pointers to them? e.g. code
like this:

%union {
   ::std::string *token;
}

How Bison knows that it should call something like `delete token;` ?

Using %destructor you can specify the action (delete) and the production to apply, for example:
%destructor{delete $$}  exp stmt
since $$ is a

::std::string *token;


for "exp" and "stmt".
Also using "C", you can always put a void* inside the union.




Luca



reply via email to

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