match EOF but go to endless loop in flex
Asked Answered
S

1

9

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?

Soften answered 9/5, 2014 at 2:40 Comment(0)
F
11

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.

Fantinlatour answered 9/5, 2014 at 4:6 Comment(1)
or yyterminate()Numeration

© 2022 - 2024 — McMap. All rights reserved.