The bug is simple enough:
if (strcmp(yylval.str_val, "int"))
does not do what you think it does. strcmp
returns 0
if the two strings are equal, a negative value if the first one is lexicographically earlier, and a positive value if the first one is lexicographically later. Used as a boolean, that means that strcmp
is false
if the string compare equal, and true
otherwise. So the token int
will not generate the token value IVAL
.
But I don't think that's really what you wanted to do, anyway. Your instructor's hint about having the lexer return a token type corresponding to the known datatype of the token is referring to looking the variable up in the symbol table and returning a token type corresponding to the declaration of the variable. It's not referring to recognizing the reserved words int
and float
, which should be done with simple lexer rules, much like your rule for the reserved word sqrt
.
As written, your grammar does not allow expressions to use variables, so (even after you fix the bug I referred to), the following will fail:
int b = 0;
int a = b + 3;
because b
won't be recognized as an int_exp
. It's in this context that the instructor's hint applies. (Although personally, I would suggest doing it differently.)
Finally, I don't know what strdupclean
does, but I presume it involves making a copy of yytext
. That's almost certainly unnecessary in the case of operator tokens (+
, -
, and so on) or reserved words, since you will never refer to the "semantic value" of those tokens. (As evidence, you don't declare any of these tokens to even have a semantic type.) Unnecessary copying does have a cost, particularly if you need to clean up the memory allocated for the copy.
Good luck.