|Reclaiming dynamic memory if parse fails? firstname.lastname@example.org (email@example.com) (2007-07-13)|
|Re: Reclaiming dynamic memory if parse fails? firstname.lastname@example.org (Chris Dollin) (2007-07-16)|
|Re: Reclaiming dynamic memory if parse fails? DrDiettrich1@aol.com (Hans-Peter Diettrich) (2007-07-16)|
|Re: Reclaiming dynamic memory if parse fails? email@example.com (Mark Holland) (2007-07-16)|
|Re: Reclaiming dynamic memory if parse fails? firstname.lastname@example.org (Martin Ward) (2007-07-16)|
|Re: Reclaiming dynamic memory if parse fails? email@example.com (Matthias Blume) (2007-07-18)|
|From:||Hans-Peter Diettrich <DrDiettrich1@aol.com>|
|Date:||Mon, 16 Jul 2007 02:40:20 +0200|
|Keywords:||parse, errors, storage|
|Posted-Date:||18 Jul 2007 20:02:21 EDT|
> I'm using lex and yacc for parsing a configuration file that builds a
> dynamically allocated tree structure for runtime searching as user
> requests come in. When a parse fails, in the middle of the
> configuration file, and the tree is partially built, how can I reclaim
> all of the dynamic memory gracefully? The tree is built from the
> bottom up, the references to the pointers of the tree are passed
> between each of the grammar rules using the $$ = (some pointer)
> scheme, finally the root of the tree is set at the start symbol.
I'm not sure how far this can be done in traditional lex/yacc, perhaps
it's already implemented in bison++:
Processing is based on tokens, which are put onto an stack (shift) or
are combined (reduce) into nonterminals, i.e. into tree nodes. When
everything on that stack is treated as kind of tree-node objects, of a
common base class, the stack contains the references to all allocated
objects. On success this will be the single Goal node, or AST, on a
failure the stack contains more elements.
> I thought of having a global array of some structure holding a pointer
> to each dynamically allocated structure and an enum to identify the
> type of the object. Then as each item is allocated, it is added to
> the array. Then if yyerror is called, it could free all of the
> references in that list. This method seems clumsy to me though...
> Any other ideas?
Linked lists might be used, instead of an array, to build a tree
structure. On success, that list is the AST. All but the leaf nodes
(tokens?) also are root nodes of subtrees, i.e. of another linked list
of child nodes.
Return to the
Search the comp.compilers archives again.