beecode
beecode

Reputation: 1

Use typedef in union

I want to redeifine the float using:

typedef float decimal

because i'm using "uthash" table and it doesn't hava a method add_float it can only me done through a struct

i used this in bison

%union{
   decimal dec;
}
and then declare the token
%token <dec> DECIMAL

if I do in the flex

{DECIMAL} {yylval.dec=atof(yytext); return (DECIMAL);}

it tells me: ERRORS in the flex document: YYSTYPE has no member named dec

ERRORS in the bison (in the union) expected specifier-qualifier-list before decimal.

Any ideas? Any help appreciated!

Upvotes: 0

Views: 2016

Answers (1)

Kizaru
Kizaru

Reputation: 2513

It seems like you have a few issues.

First, your flex rule {DECIMAL} {yylval.dec=atof(yytext); return (DECIMAL);} makes no sense. Is DECIMAL a macro or replacement for some matching regexp pattern? It does not appear so because DECIMAL is declared as a TOKEN in Bison.

Anyway, the simple issue in your union for yylval in Bison is the type decimal was not declared anywhere. You should have typedef decimal float declared somewhere in your bison file and your flex file. This is what the error message

ERRORS in the bison (in the union) expected specifier-qualifier-list before decimal.

is referring to more than 99% of the time.

Now, to solve that issue you would need to either put it in a separate file and include that file into both the flex and bison files. This is messy, and a simpler approach is to just put it in the first section of the bison file (first part where C code goes). Then, if you use

bison -d myfile.y

you would get a new file called myfile.tab.h (along with the myfile.tab.c). The header should be included in the flex file. If you look in that header file, you will see all of your %token definitions also appear in it, so you can make a change at any time in bison and not need to worry about making the same changes in the flex file.

Upvotes: 1

Related Questions