[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

A polymorphic YYSTYPE for C++ (instead of the %union)

From: Michiel De Wilde
Subject: A polymorphic YYSTYPE for C++ (instead of the %union)
Date: Fri, 15 Jun 2007 13:26:11 +0200

Dear Bison maintainers,

The attached patch implements a solution to a limitation that bison
imposes on C++ developers: the %union construct could only contain
Plain Old Datatypes (POD) if you needed different types of semantic
values. This meant that you could not use classes with constructors or
destructors, so you had to use pointers to classes and implement your
own memory management (with limited help of the %destructor clause ->
only in the case of parsing errors, not to destroy unused
(non)terminals inside an action).

A solution without bison changes is to define YYSTYPE as a polymorphic
C++ class such as boost::variant (whose objects can be any of a number
of types; see ). The downside of
this approach is that you lose Bison's built-in type handling (the
union fields between angled brackets which are automatically
selected). The risk associated to a polymorphic type is the assignment
of an object of type A to a nonterminal in an action, and the
attempted use of that object as type B in another action.

I have implemented full support for boost::variant AND Bison type
selection. You can still build and use Bison without Boost if you
don't use it (the generated code will be identical). Attached is a
patch for Bison 2.3 (tested) and the same patch adapted for current
Bison cvs (untested but it could work). The attached file
"variant-test.yy" is a grammar example and contains a full explanation
of the principles and the usage.

If you don't want to read the whole file, this is the usage in short:
* use the clause %define "variant" "int, std::string,ANY OTHER TYPE"
 to define all types that are being used.
* in %token and %type clauses, directly write the type instead of a union field.
 Examples are "%token <int> NUMBER" or "%type <std::string> text".
* in actions, the dollar constructs ($$, $1, $2, ...) are to be
interpreted as references
 to objects of the right type. The result ($$) will have been
 at the beginning of the action (no implicit $$=$1).
* The function yylex will be called with a yylval of type boost::variant<...>.
* no %destructor needed anymore

I'd like to see the patch incorporated in future Bison releases (then
we do not have to maintain and upgrade this patch for new Bison
releases; we kindly contribute it to the community and also to fulfill
the GPL license requirements). Could you commit the patch and/or
comment on the code?

Kind regards,

Michiel De Wilde
Michiel De Wilde
R&D Engineer
Agilent Technologies

Attachment: bison-variant.2.3.patch
Description: Text Data

Attachment: bison-variant.cvs20070615.patch
Description: Text Data

Attachment: variant-test.yy
Description: Binary data

reply via email to

[Prev in Thread] Current Thread [Next in Thread]