You can tell flex
to generate a header file as well as the C source file, using the --header-file=<filename>
command line option, or by including %option header-file="<filename>"
in the flex source. I typically invoke flex
with:
flex --header-file=file.h -o file.c file.l
(Actually, I use make
rules to generate a command like that, but that's the idea.) Then you can #include "file.h"
in any source file which needs to invoke a flex function.
Normally, yylex
returns the token type (an integer). The global variable yytext
contains a pointer to the token string itself, which is probably sufficient for your purposes. However, please read "A Note About yytext And Memory" in the flex
manual. (Summary: if you need to save the value of yytext
, you must make a copy of it; strdup
is recommended. Don't forget to free the copy when you don't need it anymore.)
Sometimes, the token string itself is not exactly what you want as a semantic value. By convention, flex
actions place the semantic value of the token in the global yylval
, which is where bison
-generated parsers will look for it. However, yylval
is not declared anywhere by flex
-generated code, so you need to include a declaration yourself, both in the flex-generated code and in any source file which includes it. (If you use bison
to generate your parser, bison
will generate this declaration and put it in the header file it generates.)