Reputation: 89
I have regex in my .l file that grabs a variable name. I run that name through a function to get a pointer for it and then try to return that pointer. In the transition between lex and yacc, the pointer gets changed somehow. This does not happen when I do the same thing when detecting strings:
\"([^"]+)\" yylval = removeQuotes(yytext); return STRING;
[a-z_][a-z0-9_]* yylval = returnVar(yytext); return VAR;
%%
long removeQuotes(char* str)
{
int i;
for (i=1; str[i]!='\0'; i++){
str[i-1] = str[i];
}
str[i-2] = '\0';
return (long)str;
}
long returnVar(char* name){
return (long)name;
}
STRING works and will allow me to use strings as I like in my program. VAR does not pass the pointer to yacc correctly, so when I try to use the pointer to grab the variable name string in yacc, it obviously can't find the same string. Does anyone know why it is not passing the pointer as I expect? Is there something about making the changes to the input in removeQuotes that affects the pointer at all?
EXAMPLE: returnVar passes the pointer 39035008 yacc shows 39051408
Upvotes: 0
Views: 79
Reputation: 126498
yytext
is a pointer into lex's internal scanning buffer, so it is only valid until the next call to yylex
which will likely overwrite the buffer with different data. As such, you shou NEVER store it into yylval and try to use it in the yacc code as you may see it randomly "corrupted" with data from subsequent tokens. Instead, if you want to return a string from lex, you need to allocate some other memory for the string(s), copy the characters from yytext to there and return a pointer to that instead.
Upvotes: 1