For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > January 2008 > lex specification for an exercise in Lex & Yacc book









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author lex specification for an exercise in Lex & Yacc book
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 ++;
}
}
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com