Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

match EOF but go to endless loop in flex

I need to match EOF in flex

this is the main error part of my code lex.l

%{
%}
%%
<<EOF>> {printf("match EOF\n");}
%%
int main(){
    yylex();
}

i use

flex lex.l  ;  gcc lex.yy.c -o lex.exe -lfl ; lex.exe < text 

to execute

and this is my text file just one line

abc(no \n or \r at the end of file)

but when I execute lex.exe it goes into endless loop and output infinite line of "match EOF\n"

How to solve the problem?

like image 435
bengxy Avatar asked May 09 '14 02:05

bengxy


1 Answers

You need to return something from the EOF action; otherwise, flex will continue to attempt to read the input, and everytime it does it will find that it is at the EOF, causing it to execute your action again.

In fact, you should always return 0 from an <<EOF>> pattern, if you are working with yacc/bison, because that's how you signal the parser that it has reached the end of input. However, in most cases it is not actually necessary to explicitly do anything because return 0 is the default EOF action.

like image 131
rici Avatar answered Nov 04 '22 13:11

rici