gpt4 book ai didi

c - 为什么下面的 Flex 代码不显示输出?

转载 作者:行者123 更新时间:2023-11-30 16:43:05 27 4
gpt4 key购买 nike

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

关于c - 为什么下面的 Flex 代码不显示输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45524561/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com