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

时间:2014-01-20 11:37:42

标签: c++ flex-lexer

我有一个简单的灵活源代码,可以跳过/* */中的评论,并且应该得到评论的数量:

%{
  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行?

1 个答案:

答案 0 :(得分:1)

我自己就试过了。所以我将您的源代码复制到“x.l”并执行了make x ld然后抱怨缺少yywrap()函数。添加后

%option noyywrap

编译成功,测试显示:

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

<强>更新

如果文本未从文件加载(仅./x),则必须通过CTRL + D

结束手动输入
相关问题