flex icon indicating copy to clipboard operation
flex copied to clipboard

Calling input() at EOF doesn't return EOF character(-1).

Open SouravKB opened this issue 3 years ago • 4 comments

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.

SouravKB avatar Dec 06 '22 18:12 SouravKB

Related issue: #540

SouravKB avatar Dec 06 '22 18:12 SouravKB

Related issue: #448

SouravKB avatar Dec 06 '22 18:12 SouravKB

Related issue: #444

SouravKB avatar Dec 06 '22 18:12 SouravKB

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

zmajeed avatar Nov 03 '23 12:11 zmajeed