env
- OS: MacOS Monterey 12.6 with apple M1 Pro
- version: flex 2.6.4 Apple(flex-34)
Problem
%{
#include <stdio.h>
int yywrap(void) {
return 1;
}
%}
ID [a-z][a-zA-Z0-9]*
DIGIT [0-9]
%%
{DIGIT}+ {printf("num");}
{ID} printf("id: %s\n", yytext);
"/*" {
int c;
for ( ; ; ) {
while ( (c = input()) != '*' && c != EOF )
printf("%x\n", c);
if ( c == '*' ) {
while ( (c = input()) == '*' )
printf("%x\n", c);
if ( c == '/' )
break; /* found the end */
}
if ( c == EOF ) {
printf( "EOF in comment" );
break;
}
}
}
%%
int main(int argc, char **argv) {
++argv, --argc;
if (argc > 0) {
yyin = fopen(argv[0], "r");
} else {
yyin = stdin;
}
yylex();
return 0;
}
I try this code, hope to handle EOF in Action by myself. But it fail to handle this file, like:
/* **jfiowejfiowe
*
*
Yes, I just want to test unclosed comment in the end of file.
lex test.lex
gcc lex.yy.c -o lextest
./lextest comment.c
and it output
20
2a
66
69
6f
77
65
6a
66
69
6f
77
65
a
20
20
0
0
0
...
with endless 0.
I also try to rewrite yywrap()
int yywrap() {
return 0;
}
But it output like
20
2a
66
69
6f
77
65
6a
66
69
6f
77
65
a
20
20
[1] 57558 segmentation fault ./lextest comment.c
has a segmentation fault error.
Also I try yyterminate like
#define yyterminate() return YY_NULL
int yywrap(void) {
yyterminate();
}
still has a segmentation fault error.