How to get string value of token in flex and bison?
In the absence of a %union
declaration, bison defines YYSTYPE
to be int, so all of the
symbol values are integers.
In fact you have a few solutions for this problem:
1) yylval|union solution (RECOMMENDED):
As you may know yylval is a global variable used by the lexer to store yytext variable (yylval = yytext;
) so you should tell your lexer which types you would to store.you can simply add this line to the header of your YACC grammar:
#define YYSTYPE char *
you will store here only string values.
By the way if you want to store different types you should specify in your yacc file:
%union {
char *a;
double d;
int fn;
}
then in your lex you will have
[a-zA-Z0-9]+ { **yylval.a** = yytext; return ALPHANUM; }
2) Using yytext:
Advice: for callbacks after rules in yacc i,personally prefer to use functions. not the whole code as you do :)
this solution is really simple .
Sentence: "Sphere("{callback_your_function(yytext);} ALPHANUM ")."
the yytext here will have the value of your ALPHANUM token because it's the next token.
user1007632
Updated on July 19, 2022Comments
-
user1007632 almost 2 years
I have this token in my .lex file:
[a-zA-Z0-9]+ { yylval = yytext; return ALPHANUM; }
and this code in my .y file:
Sentence: "Sphere(" ALPHANUM ")." { FILE* file = fopen("C:/test.txt", "a+"); char st1[] = "polySphere -name "; strcat(st1, $2); strcat(st1, ";"); fprintf(file,"%s", st1); fclose(file); }
I get this error when I try to compile:
warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast
So $2 is an int, how do I make it a string?
For example: "Sphere(worldGlobe)." I want $2 to have the string value worldGlobe here.
Thanks for any help
-
Kaz about 12 yearsI'd be careful sticking the raw yytext pointer into the yylval. Next time you lex something, the buffer is overwritten. It's not a suitable approach if you have a parser that will collect several such tokens into a rule and you refer to them with $1, $2, at the same time. It may be okay if you're just calling yylex() in a loop and processing tokens one by one.