为什么下面的 flex 代码不显示输出?
Why the below flex code doesn't show output?
digit [0-9]
letter [A-Za-z]
%{
int count;
%}
%%
/* match identifier */
{letter}({letter}|{digit})* count++;
%%
int main(void) {
yylex();
printf("number of identifiers = %d\n", count);
return 0;
}
不起作用printf 语句。
你能解释一下我应该在这段代码中包含什么吗?
如果 yywrap
有误——只需添加 %option noyywrap
:
digit [0-9]
letter [A-Za-z]
%{
int count;
%}
%option noyywrap
%%
/* match identifier */
{letter}({letter}|{digit})* count++;
%%
int main(void) {
yylex();
printf("number of identifiers = %d\n", count);
return 0;
}
然后编译:
flex f.l
gcc lex.yy.c
运行 和 不要忘记在最后发送 EOF(使用 Ctrl-D):
./a.out
a a a
number of identifiers = 3
digit [0-9]
letter [A-Za-z]
%{
int count;
%}
%%
/* match identifier */
{letter}({letter}|{digit})* count++;
%%
int main(void) {
yylex();
printf("number of identifiers = %d\n", count);
return 0;
}
不起作用printf 语句。 你能解释一下我应该在这段代码中包含什么吗?
如果 yywrap
有误——只需添加 %option noyywrap
:
digit [0-9]
letter [A-Za-z]
%{
int count;
%}
%option noyywrap
%%
/* match identifier */
{letter}({letter}|{digit})* count++;
%%
int main(void) {
yylex();
printf("number of identifiers = %d\n", count);
return 0;
}
然后编译:
flex f.l
gcc lex.yy.c
运行 和 不要忘记在最后发送 EOF(使用 Ctrl-D):
./a.out
a a a
number of identifiers = 3