Calling input() at EOF doesn't return EOF character(-1).
Minimal reproducible example:
%%
"xyzw" {
printf("A:%X\n", input());
printf("B:%s-%s-%s\n", yytext, yytext+1, yytext+2);
}
%%
int main() {
yy_scan_string("xyzw\0\0");
yylex();
}
int yywrap() {
return 1;
}
Expected output:
A:FFFFFFFF
B:xyzw-yzw-zw
Actual output:
A:0
B:--zw
Explanation:
A) On reading EOF, input returns 0 instead of expected -1 (EOF character).
B) On reading EOF, input calls yyrestart, which calls yy_init_buffer, which calls yy_flush_buffer, which initializes first two bytes of yy_ch_buf to 0, which, in this case, clobbers yytext.
Related issue: #540
Related issue: #448
Related issue: #444
This is expected behavior for Posix input() - it is supposed to return 0 - not EOF - on end of file - or end of string input in this case - https://pubs.opengroup.org/onlinepubs/9699919799/utilities/lex.html
int input(void)
Returns the next character from the input, or zero on end-of-file. It shall obtain input from the stream pointer yyin, although possibly via an intermediate buffer. Thus, once scanning has begun, the effect of altering the value of yyin is undefined. The character read shall be removed from the input stream of the scanner without any processing by the scanner.
btw yy_scan_string does not need input terminated with two nul characters - that's for yy_scan_buffer - also you can skip yywrap() with %option noyywrap