Re: Parse tree creation question

"Quinn Tyler Jackson" <qjackson@wave.home.com>
21 Mar 2000 23:40:18 -0500

          From comp.compilers

Related articles
Parse tree creation question lojedaortiz@interlink.com.ar (Nicolas) (2000-03-11)
Re: Parse tree creation question qjackson@wave.home.com (Quinn Tyler Jackson) (2000-03-21)
Re: Parse tree creation question glouis@univ-lr.fr (Georges LOUIS) (2000-03-23)
Re: Parse tree creation question chrisd@reservoir.com (Chris Dodd) (2000-03-23)
| List of all articles for this month |

From: "Quinn Tyler Jackson" <qjackson@wave.home.com>
Newsgroups: comp.compilers
Date: 21 Mar 2000 23:40:18 -0500
Organization: Compilers Central
References: 00-03-077
Keywords: parse, OOP

> I am creating my abstract syntax tree with bison. I have some rules like
> this:
>
> a: b c { $$ = gProgram = new a( $1, $2 ); } ;
> b: d { $$ = new b( $1 ); } ;
> c: e { $$ = new c( $1 ); } ;
>
> Now, if the parsing phase doesn't fail, when a gets reduced, gProgram
> is a pointer to the entire parse tree, so I can't deallocate it when I
> am done working with it. But, if the parsing phase fails for any
> reason, I can't deallocate the nodes constructed up to the momento of
> failure, since I have no pointer to them.


Why not derive all of your parse-created classes from a common base
class, say CDisposableObject, and in the ctor of that class, add a
pointer to the base to a [thread-guarded] static set. The dtor for
this class would be virtual. Then, give the base class a static
member function called CDisposableObject::CleanUp(void), which would
then traverse the set, deleting each object.


Then, in your abort routine for the parse, call the static member
CleanUp().


The chances are, if you are allocating objects with new and assigning
them to $$ as above, you are already deriving them from a common
class, and it would be a simple matter to add the static set to that
class.


--
Quinn Tyler Jackson


http://qtj.n3.net/~quinn/


Post a followup to this message

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