| fei.liu@gmail.com 2008-01-25, 7:20 pm |
| Hello,
I wrote this lex spec for chapter 2, question 2. This works for
counting bracers but to do the same thing for keywords, function
signatures etc is obviously not in the spirit of Lex. I can hack it if
I didn't have to count lines of comments, source codes, blank lines
etc...
What's your thought? How should I go from here to count keywords,
function definitions and declarations etc..Thanks,
Fei
%{
int comments, code, whitespace;
int lcount, rcount, bcount;
%}
bracer [{}]{1}
non_bracer [^{}/]
%x COMMENT
%%
^[ \t]*"/*" { BEGIN COMMENT; /* enter comment eating
state */ }
^[ \t]*"/*".*"*/"[ \t]*\n { comments ++; /*self contained comment
*/ }
<COMMENT>"*/"[ \t]*\n { BEGIN 0; comments ++; }
<COMMENT>"*/" { BEGIN 0; }
<COMMENT>.?\n { comments ++; }
^[ \t]*\n { whitespace ++; }
"{" { lcount ++; printf("bline %d\n",
yylineno); }
"}" { rcount ++; printf("bline %d\n",
yylineno); }
..+"/*".*"*/".*\n { code ++; check_b(yytext); }
..*"/*".*"*/".+\n { code ++; check_b(yytext); }
..+"/*".*\n { code ++; check_b(yytext); BEGIN
COMMENT; }
\n { code ++; }
.. ; /* ignore everything else */
%%
int main(){
yylex();
printf("code %d, comments %d, whitespace %d\n",
code, comments, whitespace);
printf("lcount %d rcount %d bracercount %d bcount %d\n",
lcount, rcount, lcount+rcount, bcount);
}
void check_b(char * token){
/* need to do bracer count here
because the code line matches are greedy matches
that will take all {}s ;
bcount ++ won't work for {{ $comment pattern
Is there a way to avoid counting {}s in c code?
*/
extern int yylineno;
printf("line %d:%s", yylineno-1, token);
while(*token){
if(*token == '{' || *token == '}') bcount ++;
token ++;
}
}
|