Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top