www.pudn.com > progs.zip > ch2-09.l
%{
int comments, code, whiteSpace;
%}
%s COMMENT
%%
^[ \t]*"/*" { BEGIN COMMENT; /* enter comment eating state */ }
^[ \t]*"/*".*"*/"[ \t]*\n {
comments++; /* self-contained comment */
}
"*/"[ \t]*\n { BEGIN 0; comments++;}
"*/" { BEGIN 0; }
\n { comments++; }
.\n { comments++; }
^[ \t]*\n { whiteSpace++; }
.+"/*".*"*/".*\n { code++; }
.*"/*".*"*/".+\n { code++; }
.+"/*".*\n { code++; BEGIN COMMENT; }
.\n { code++; }
. ; /* ignore everything else */
%%
main()
{
yylex();
printf("code: %d, comments %d, white space %d\n",
code, comments, whiteSpace);
}