how to execute code after yylex(); command

最后都变了- 提交于 2020-01-06 14:06:01

问题


I have a simple flex source code which skips the comments in /* */ and should get the count of comments found:

%{
  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;
}

First half works fine - it really skips the comments, but they are not counted... what can I do to execute printf line?


回答1:


I just tried it myself. So I copied your source code to "x.l" and did a make x ld then complained about the missing yywrap() function. After adding

%option noyywrap

The compile succeeded and a test showed:

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

UPDATE:

If the text is not loaded from a file (just ./x), you have to end your manual input by CTRL + D



来源:https://stackoverflow.com/questions/21233024/how-to-execute-code-after-yylex-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!