问题
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