Re: Reclaiming dynamic memory if parse fails?

Hans-Peter Diettrich <DrDiettrich1@aol.com>
Mon, 16 Jul 2007 02:40:20 +0200

          From comp.compilers

Related articles
Reclaiming dynamic memory if parse fails? carsonbj@gmail.com (carsonbj@gmail.com) (2007-07-13)
Re: Reclaiming dynamic memory if parse fails? chris.dollin@hp.com (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? kenshin_40@htomail.com (Mark Holland) (2007-07-16)
Re: Reclaiming dynamic memory if parse fails? martin@gkc.org.uk (Martin Ward) (2007-07-16)
Re: Reclaiming dynamic memory if parse fails? blume@hanabi.local (Matthias Blume) (2007-07-18)
| List of all articles for this month |
From: Hans-Peter Diettrich <DrDiettrich1@aol.com>
Newsgroups: comp.compilers
Date: Mon, 16 Jul 2007 02:40:20 +0200
Organization: Compilers Central
References: 07-07-051
Keywords: parse, errors, storage
Posted-Date: 18 Jul 2007 20:02:21 EDT

carsonbj@gmail.com wrote:


> 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.


DoDi



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.