There are several approaches. The ones I recommend are.
Provide your own yywrap() function.
Since my compilers usually allow multiple source files, I prefer to define my own yywrap(). I'm compiling with C++, but the point should be obvious. If someone calls the compiler with multiple source files, I store them in a list or array, and then yywrap() is called at the end of each file to give you a chance to continue with a new file. NOTE: getNextFile() or compiler->getNextFile() are my own functions, not flex functions.
int yywrap() {
// open next reference or source file and start scanning
if((yyin = getNextFile()) != NULL) {
line = 0; // reset line counter for next source file
return 0;
}
return 1;
}
If building with C++ use:
extern "C" int yywrap() {
// open next reference or source file and start scanning
if((yyin = compiler->getNextFile()) != NULL) {
line = 0; // reset line counter for next source file
return 0;
}
return 1;
}
With older lex/flex, yywrap() was a macro, so to redefine it we had to do:
%{
#undef yywrap
%}
Potentially you could redefine a simple macro which would end scanning after a single file:
%{
#undef yywrap
#define yywrap() 1
%}
With newer Flex / POSIX lex, you can disable yywrap altogether at the command line:
flex --noyywrap
This works for other functions besides yywrap (--noyymore etc.)
Or put it in the options section.
%option noyywrap
/* end of rules */
%%
Where the general syntax of a lex file is:
Options and definitions
%%
Rules
%%
C code