C++ 如何在yylex()之后执行代码;命令

C++ 如何在yylex()之后执行代码;命令,c++,flex-lexer,C++,Flex Lexer,我有一个简单的flex源代码,它跳过了/**/中的注释,应该可以得到找到的注释数: %{ int in_comment = 0; int count = 0; %} %% \/\* { in_comment = 1; count++; } \*\/ { in_comment = 0; } . { if (!in_comment) ECHO; } %% int main(void) { yylex(); printf("Comments found %d\n", coun

我有一个简单的flex源代码,它跳过了
/**/
中的注释,应该可以得到找到的注释数:

%{
  int in_comment = 0;
  int count = 0;
%}

%%
\/\* { in_comment = 1; count++; }
\*\/ { in_comment = 0; }
.    { if (!in_comment) ECHO; }
%%

int main(void)
{
  yylex();
  printf("Comments found %d\n", count); // never executed
  return 0;
}

前半部分很好——它确实跳过了评论,但它们不被计算在内。。。如何执行
printf
行?

我自己刚试过。所以我把你的源代码复制到了“x.l”,并做了一个
make x
ld然后抱怨缺少yywrap()函数。添加后

%option noyywrap
编译成功,测试显示:

ronald@cheetah:~/tmp$ ./x < cribbage.c
... lots of output ...
Comments found 15
ronald@cheetah:~/tmp$./x
更新:


如果文本不是从文件加载的(只是
/x
),您必须通过
CTRL+D

结束手动输入谢谢。。。在我的例子中,所有的东西都是在没有警告或错误的情况下编译的,但是找到的注释却丢失了:(你的程序是首先返回还是挂起?在后一种情况下,lexer需要更多的输入(由yywrap()函数控制)。所以请尝试我的建议。我已经尝试过了,但没有成功。它正在打印我想要的没有注释的句子,没有挂起,一切似乎都很好,但计数没有打印奇怪…你如何编译和链接?OS?flex版本?(很抱歉有这么多问题,但现在我们必须深入研究)嗯,你是否以“ctrl-D”结束你的输入?