Bison: reentrant C++ abstract-syntax-tree parser

Lucian Wischik <lu.nn@wischik.com>
23 Sep 2003 13:23:19 -0400

          From comp.compilers

Related articles
Bison: reentrant C++ abstract-syntax-tree parser lu.nn@wischik.com (Lucian Wischik) (2003-09-23)
Re: Bison: reentrant C++ abstract-syntax-tree parser haberg@matematik.su.se (2003-09-27)
Re: Bison: reentrant C++ abstract-syntax-tree parser lu.nn@wischik.com (Lucian Wischik) (2003-09-27)
Re: Bison: reentrant C++ abstract-syntax-tree parser yves@news.be (Yves Deweerdt) (2003-09-27)
Re: Bison: reentrant C++ abstract-syntax-tree parser luca.dattila@tin.it (Luca D'Attila) (2003-09-27)
| List of all articles for this month |

From: Lucian Wischik <lu.nn@wischik.com>
Newsgroups: comp.compilers
Date: 23 Sep 2003 13:23:19 -0400
Organization: University of Bologna
Keywords: parse, yacc, question, comment
Posted-Date: 23 Sep 2003 13:23:19 EDT

I'm trying to use bison to make an abstract syntax tree. I'm trying to
do it in a nice C++ sort of way. But I'm finding it very difficult,
and would really find it helpful if someone could point me to an
example of "best practice".


I'd really like to just return an object in response to a grammar
rule, like I did with JavaCC. But bison wants to put all possible
return types in a union, and it's not allowed to put a class in a
union, so I can't.


I found some examples which used pointers instead. So when the grammar
matched a rule, it created a new instance of a particular struct and
returned its pointer. This seems clear enough, but I don't understand
how to delete everything in the end. What happens to all the objects
that have already been created, if a subsequent parse fails? The
examples I saw didn't handle this.


My next idea was to have an argument 'parm' to yyparse. Inside 'parm'
I could keep a "global" list of all the objects that had so far been
created. Then, if there were errors, I could just delete everything in
that global list. Actually, I could even make the global list just
contain a vector<ASTNode>, and make every grammar-rule return an
integer into this vector.


I'm still worried about what happens when exceptions get thrown.


Anyway, there seem to be so many issues that I'm left confused. So if
anyone can point me to an elegant C++/STL AST parser, done with
yacc/bison, I'd be very grateful.


--
Lucian
[It's a challenge, since yacc and bison generate parsers in C, not C++.
-John]


Post a followup to this message

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