Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 匹配EOF,但转到flex中的无止境循环_Regex_Match_Lex_Flex Lexer_Eof - Fatal编程技术网

Regex 匹配EOF,但转到flex中的无止境循环

Regex 匹配EOF,但转到flex中的无止境循环,regex,match,lex,flex-lexer,eof,Regex,Match,Lex,Flex Lexer,Eof,我需要在flex中匹配EOF 这是我代码lex.l的主要错误部分 %{ %} %% <<EOF>> {printf("match EOF\n");} %% int main(){ yylex(); } 但当我执行lex.exe时,它会进入无限循环并输出无限行“match EOF\n” 如何解决问题?您需要从EOF操作返回一些内容;否则,flex将继续尝试读取输入,每次读取时,它都会发现它处于EOF,导致它再次执行您的操作 事实上,如果您使用的是yacc/biso

我需要在flex中匹配EOF

这是我代码lex.l的主要错误部分

%{
%}
%%
<<EOF>> {printf("match EOF\n");}
%%
int main(){
    yylex();
}
但当我执行lex.exe时,它会进入无限循环并输出无限行“match EOF\n”


如何解决问题?

您需要从
EOF
操作返回一些内容;否则,
flex
将继续尝试读取输入,每次读取时,它都会发现它处于
EOF
,导致它再次执行您的操作

事实上,如果您使用的是
yacc/bison
,那么您应该始终从
模式返回
0
,因为这就是您向解析器发出信号表示它已到达输入的末尾的方式。但是,在大多数情况下,实际上不需要显式执行任何操作,因为
return0
是默认的
EOF
操作。

yyterminate()
flex lex.l  ;  gcc lex.yy.c -o lex.exe -lfl ; lex.exe < text 
abc(no \n or \r at the end of file)