I am writing a flex program to deal with string constants.
I want to return an ERROR token when the input file meets EOF inside a string.
I got the following error after the file meets EOF and "ERROR" is printed:
fatal flex scanner internal error--end of buffer missed
Here is my code: (a simplified version which can reproduce this error)
%option noyywrap
#define ERROR 300
#define STRING 301
char *text;
%x str
%%
\" {BEGIN(str); yymore();}
<str>\" {BEGIN(INITIAL); text=yytext; return STRING;}
<str>. {yymore();}
<str><<EOF>> {BEGIN(INITIAL); return ERROR;}
%%
int main(){
int token;
while((token=yylex())!=0){
if(token==STRING)
printf("string:%s\n",text);
else if(token==ERROR)
printf("ERROR\n");
}
return 0;
}
When I delete the yymore()
function call, the error disappeared and the program exited normally after printing "ERROR".
I wonder why this happens and I want to solve it without removing yymore()
.