Tommy Nordgren ha scritto:
C++ dont allow unions containing c++ classes
I think you're wrong. The size of a pointer is always known at compile
time, pair<const string,int>* it; 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 a STL
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 concerns the definition. 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 file 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
On May 22, 2009, at 8:11 PM, Paritosh Aggarwal wrote:
/*JIT PARSER BISON FILE*/
%{
#include<map>
#include "heading.h"
using namespace std;
int yyerror(char *s);
int yylex(void);
%}
%union
{
pair<const string,int>* it;
int val;
}
................
compiling this gives the following error: jitparser.y:15: error: ISO C++
forbids declaration of ‘pair’ with no type
jitparser.y:15: error: expected ‘;’ before ‘<’ token
I want to include a pointer to an STL map entry, and
map<parameters>::iterator gives a similar error. Is there any way to
accomplish this.
BTW - removing the "const" before string removes this error, but
iterator is
pointer to a pair<const string, int> type.
_______________________________________________
help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison
----------------------------------
The three things an undertaker should never say to a client:
-Nice doing business with you.
-Welcome back.
-Have a nice day. (The King of ID)
----------------------------------
Tommy Nordgren
tommy.nordg...@comhem.se
_______________________________________________
help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison
_______________________________________________
help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison