为什么下面的flex代码不显示输出?

问题描述 投票:0回答:1
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语句不起作用。你能解释我应该在这段代码中包含什么。

c compiler-construction flex-lexer
1个回答
0
投票

如果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
© www.soinside.com 2019 - 2024. All rights reserved.