%{ /* In der Vorlesung am 26.11.2007 gemeinsam erstellte Loesung fuer das * Zaehlen von Code- und Kommentarzeilen. * Diese Loesung zaehlt alle Kommentarzeilen, nicht nur die * Kommentarzeilen ohne Code, wie dies die Musterloesung tut. */ #include int ccode_found = 0; int comment_found = 0; int ccode_lines = 0; int comment_lines = 0; int empty_lines = 0; %} %option noyywrap %x COMMENT %% "/*" { comment_found = 1; BEGIN(COMMENT); } "*/" { BEGIN(INITIAL); } \n { if (comment_found) comment_lines++; if (ccode_found) ccode_lines++; if (!comment_found && !ccode_found) empty_lines++; comment_found = 0; ccode_found = 0; } \n { comment_lines++; if (ccode_found) ccode_lines++; ccode_found = 0; } [^ \t] { ccode_found = 1; } [ \t] /* nichts tun */ . /* nichts tun */ %% int main() { yylex(); printf("Zeilen mit C-Code: %d, Zeilen mit Kommentaren: %d, Leerzeilen: %d\n", ccode_lines, comment_lines, empty_lines); }