Re: [Flex-help] yytext return as char*
flex is a tool for generating scanners
Brought to you by:
wlestes
From: John L. <jo...@ta...> - 2011-10-04 02:36:00
|
"aaa" {yyval.string = yytext; return TRIPLE_A} This must be the most common bug in flex programsm along with a typo. The typo is that the value goes in yylval, not yyval. yytext is a pointer to the text of the matched token, but it is pointing into the input buffer which will change as soon as flex moves on to the next token. The usual fix is to make a copy of the string: "aaa" {yylval.string = strdup(yytext); return TRIPLE_A} Note that strdup() calls malloc(), so your program will have storage leaks unless it carefully frees string values when it's done with them. R's, John PS: The suggestion to use strcpy into yylval is just wrong, by the way, unless the yacc %union defines a large character array in each value which is probably not what he meant. |