0
votes

Bison throws this

error: $2 of ‘exp’ has no declared type

but I believe I've declared everything.

FLEX:

[0-9]+ {
yylval.string =yytext; //yes I want ints as strings.
return INT;
}
"*"     {yylval.string=yytext; return MUL;} 
"/"     {yylval.string=yytext; return DIV;} 
"-"     {yylval.string=yytext; return MINUS;} 
"+"     {yylval.string=yytext; return PLUS;} 
"^"     {yylval.string=yytext; return POW;}

BISON:

 %union {
 char * string;
}

%token <string>  INT DEC STRING PLUS MINUS MUL DIV POW
%type <string> OP
.
.
.
 exp: type {foo(x,$1);}
    |exp 
    OP {foo(x,$2);}
    exp; 

OP: PLUS | MINUS| MUL| DIV| POW ;
.
.
.

There is a lot more code but this is the part where the error occurs. When I create a rule for every operator I get no error but I get other problems because of the recursion so I believe I need to do it this way.

1
I suspect your actual code is other than what you've posted (you have a simple typo you unconsciously corrected when you summarized it). Try posting a summary .y file that you can actually run through bison and generate the error.Chris Dodd

1 Answers

1
votes

First off, you don't want your flex code to straight up return the value of yytext. It may (well, more than likely will) be overwritten before bison can fully utilize it. If you want to save the numbers as a string, you'll need to malloc and strdup them. For the operators, you probably are best off just setting yylval.string="+";, etc, instead of dynamically creating new memory.

Secondly, it seems like something is missing from your bison code. Before your problem can fully be diagnosed, can you please post the signature of function foo as well as the type of x?

Thirdly, is this a typo in your bison code? Should

exp: type {foo(x,$1);}
    |exp 
    OP {foo(x,$2);}
    exp; 

be

exp: type {foo(x,$1);}
    |exp OP exp {foo(x,$2);}
    ; 

?