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

haberg@matematik.su.se (Hans Aberg)
27 Sep 2003 13:50:36 -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: haberg@matematik.su.se (Hans Aberg)
Newsgroups: comp.compilers
Date: 27 Sep 2003 13:50:36 -0400
Organization: Mathematics
References: 03-09-085
Keywords: yacc, C++
Posted-Date: 27 Sep 2003 13:50:36 EDT

Lucian Wischik <lu.nn@wischik.com> wrote:


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


You can't use the Bison %union with a C++ class, as Bison implements
it as a C++ union, and C++ does not admit types with nontrivial
constructors in unions.


In the thread "Parser error handling" I recently described how to use
Bison together with C++, by describing how to make a polymorphic
hierarchy, which is pretty much what you need in order to build a
syntax tree and similar objects.


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


Just tweak the skeleton file: Later Bison versions use M4 to produce
the parser output sources from the skeleton file, so it is easy to
make ones own skeleton files.


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


In the actions you mean. If you want the parser to pick up the
exceptions and then recover, then you would have to tweak the skeleton
file, putting a try {...} around its switch statement, followed by a
catch (...) {...}.


If you don't need that, just wrap the parser function in a try-catch statement.


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


Bison now uses M4 to produce a source file from the skeleton file plus
parser info, so it is easy to write a skeleton file for use with
genuine C++, with some limitations: C++ requires one to put the code
in many places, in the header before, in the parser class, after it,
and in the parser sources, before and after the parser. Currently
Bison does not have a feature admitting that.


    Hans Aberg


Post a followup to this message

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