Reputation: 2668
I have declared the YYSTYPE union as
%union
{
char* stringValue;
union int_double_string* ids;
}
int_double_string
is declared as
union int_double_string
{
short type; //0:int 1:double 2:string
int intValue;
double doubleValue;
char* stringValue;
};
Some tokens
%token <stringValue> KEY
%token <int_double_string> VALUE
%token <stringValue> COMMENT
%type <stringValue> pair
%type <int_double_string> key_expr
But where-ever I use token VALUE
, it gives me that common error.
‘YYSTYPE’ has no member named ‘int_double_string’
pair:
KEY ws '=' ws VALUE {
char S5[15];
addPair($1, $5); //Error here and where-ever I use $5 in this function
...
Why is this so though I've declared it correctly? I've used the variable in my lex file as well. Its showing no error there.
lex file
{integer} {
yylval.ids = malloc(sizeof(union int_double_string));
yylval.ids->type = 0;
yylval.ids->intValue = atoi(yytext);
return VALUE;
}
I think it has something to with the concept of union inside a union.
What to do?
Upvotes: 4
Views: 6738
Reputation: 7044
‘YYSTYPE’ has no member named ‘int_double_string’
The id in %type <id>
and %token <id>
needs to be a field in the yyunion
.
So, the tokens defined as type int_double_string need to be type ids
%token <int_double_string> VALUE
%type <int_double_string> key_expr
like this
%token <ids> VALUE
%type <ids> key_expr
And the second argument to addPair
should be a union int_double_string*
In typical yacc usage you would put all these fields:
short type; //0:int 1:double 2:string
int intValue;
double doubleValue;
char *stringVal;
Into the yyunion itself and not have a union field in yyunion. I'm not saying you can't but it is unusual.
Upvotes: 3