help-bison
[Top][All Lists]
Advanced

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

Re: %union errors


From: Luca
Subject: Re: %union errors
Date: Mon, 08 Dec 2008 15:30:44 +0100
User-agent: Thunderbird 2.0.0.18 (Windows/20081105)

Laurence Finston ha scritto:
On Fri, 5 Dec 2008, Benny Hardage wrote:

%union
{
        int intVal;
        bool boolVal;
        char* strVal;
        std::list<const Variable::Qualifier*>* qList;
        Variable* var;
        UnaryOperation* uop;
        BinaryOperation* binop;
        Expression* expr;
        DataLiteral* datalit;
}


I had a problem trying to use types defined in the STL in the `%union'. I don't remember the details, but I believe it involved types where the size isn't known at compile-time not being permitted in unions.
I think you're wrong. The size of a pointer is always known at compile time, std::list<const Variable::Qualifier*>* qList is a pointer and not an object. The size is 4 bytes using a 32 bit OS. Bison union is a standard union, you can put any type inside. For example, I can compile a union with vector:

%union
{
       int intVal;
       bool boolVal;
        std::vector<int>* pVectorInt;
};

You can also insert a pointer to a known class, if you need

class MyClass;
%union
{
       int intVal;
       bool boolVal;
        MyClass* pMyClass;
};

I think the problem concern Variable::Qualifier. This type is known by the 
compiler in y.tab.c (or y.tab.cpp) because the user add the right include file 
(or add some definitions) in the first section (before the first %%) inside .y 
file. The problem arises when your using y.tab.h: here the union is reported 
without the user definitions: for example the previous union uses MyClass but 
it is an undefined symbol

//inside y.tab.h
typedef union YYSTYPE
{
       int intVal;
       bool boolVal;
        MyClass* pMyClass;              //compiler can complain about MyClass 
type!!!

} YYSTYPE;

so if you include y.tab.h inside a .c or .cpp file where MyClass isn't a 
defined type you'll have an error.



There is a simple fix, use 3 .h file:

-y.tab.h
-userdef.h
-bisondef.h

In userdef.h put all user definition, for example:
class MyClass;


in bisondef.h include put user definitions BEFORE y.tab.h
#include userdef.h
#include y.tab.h

then use biosondef.h in place of y.tab.h anywhere in your source code. In this way you can also redefine YYLTYPE (user custom locations), YYLLOC_DEFAULT (to merge locations) and YY_LOCATION_PRINT (to print locations) without any trouble.
Luca

I think there was something about constructors as well. I believe this is documented in the section about unions in Stroustrup's _The C++ Programming Language_. At any rate, I couldn't get it to work.

My unions for Bison usually contain an `int', a `float', a `char' vector, and
a `void*' for everything else. `void*' as `YYSTYPE' would do the trick, but I find it convenient to have semantic values of the other types
without having to use casts.

Laurence Finston

On Fri, 5 Dec 2008, Benny Hardage wrote:

Hello,

I'm receiving the following errors when I compile:

In file included from compiler.l:17:
parser.y:27: error: ISO C++ forbids declaration of 'list' with no type
parser.y:27: error: invalid use of '::'
parser.y:27: error: expected ';' before '<' token
parser.y:28: error: ISO C++ forbids declaration of 'Variable' with no type
parser.y:28: error: expected ';' before '*' token
parser.y:29: error: ISO C++ forbids declaration of 'UnaryOperation' with no type
parser.y:29: error: expected ';' before '*' token
parser.y:30: error: ISO C++ forbids declaration of 'BinaryOperation'
with no type
parser.y:30: error: expected ';' before '*' token
parser.y:31: error: ISO C++ forbids declaration of 'Expression' with no type
parser.y:31: error: expected ';' before '*' token
parser.y:32: error: ISO C++ forbids declaration of 'DataLiteral' with no type
parser.y:32: error: expected ';' before '*' token

I've seen a fairly old thread that attempted to address this issue,
saying that my include files might not be in the right order, but the
top of my file looks like the following:

%error-verbose

%{

int yylex();

#include <iostream>
#include "expressions.hpp"
#include "statements.hpp"

extern bool outputTokens;
extern bool outputParser;

void yyerror( char* msg )
{
        std::cerr << msg << std::endl;
}

%}

%union
{
        int intVal;
        bool boolVal;
        char* strVal;
        std::list<const Variable::Qualifier*>* qList;
        Variable* var;
        UnaryOperation* uop;
        BinaryOperation* binop;
        Expression* expr;
        DataLiteral* datalit;
}

expressions.hpp contains the datatypes in question.

I was wondering if someone might let me know what I was doing wrong.

Thanks,
B.J.


_______________________________________________
address@hidden http://lists.gnu.org/mailman/listinfo/help-bison



_______________________________________________
address@hidden http://lists.gnu.org/mailman/listinfo/help-bison




reply via email to

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