bison, union and struct YYSTYPE, c++

Marco Morandini <>
30 May 2001 00:23:59 -0400

          From comp.compilers

Related articles
bison, union and struct YYSTYPE, c++ (Marco Morandini) (2001-05-30)
| List of all articles for this month |

From: Marco Morandini <>
Newsgroups: comp.compilers
Date: 30 May 2001 00:23:59 -0400
Organization: Politecnico di Milano
Keywords: C++, yacc
Posted-Date: 30 May 2001 00:23:58 EDT

Dear all,

I'm using bison to generate a scanner and g++ to compile it.
Unfortunately, I found that I can't insert classes with a constructor
into a union (YYSTYPE), and I found it quite annoyng. My workaround
is no to use the %union declaration, but to explicitly define YYSTYPE
as a struct, i.e.

#include "MyClass.h"


typedef struct {
MyClass c1;
MySecondClass c2;
double number;

extern YYSTYPE yylval;


%token <c1> CLASS1_TOK
%token <c2> CLASS2_TOK
%token <number> NUMBER_TOK

%type <c1> c1exp



c1exp: CLASS1_TOK { $$ = $1; }
| ......


It seems to work, but is this a correct solution to the problem?
Am I making some silly errors?

MyClass and MySecondClass are not big, and have defined
a copy operator.

Thanks in advance for your answers and your suggestions,

Marco Morandini

Post a followup to this message

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