Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SouravKB opened this issue Dec 6, 2022 · 4 comments
Open

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

SouravKB opened this issue Dec 6, 2022 · 4 comments

Comments

@SouravKB
Copy link

SouravKB commented Dec 6, 2022

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
Copy link
Author

SouravKB commented Dec 6, 2022

Related issue: #540

@SouravKB
Copy link
Author

SouravKB commented Dec 6, 2022

Related issue: #448

@SouravKB
Copy link
Author

SouravKB commented Dec 6, 2022

Related issue: #444

@zmajeed
Copy link
Contributor

zmajeed commented Nov 3, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants