I am using a char*
as YYSTYPE
in a compiler built with flex and bison. The line
#define YYSTYPE char*
is at the top of my grammar file. A few of the tokens in my lexer need to pass the entire string that they match to my grammar, and the others just need to pass their token, so this works well for me. I do this sort of thing in my lexer:
[(foo|bar)] {yylval = *strdup(yytext); return FOOBAR;}
In my grammar, I use them with productions like this one:
fb:
FOOBAR
{
sprintf($$, "%s", &$1);
}
;
This sets the value of $$
to the first character in the original matched token. I (probably) understand why, since a dereferenced char*
is a char
, but the steps I took to fix it caused problems. For instance, removing the &
from the sprintf()
line causes a segfault. Removing the *
from the assignment causes a "makes integer from pointer without a cast". What do I do? I think the problem lies in the assignment to yylval
.