문제

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?

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top