%{ /* In der Vorlesung am 26.11.2007 gemeinsam erstellte Loesung fuer das * Zaehlen von Code- und Kommentarzeilen. Diese Erweiterung behandelt * auch Slash-Stern in Kommentaranfaengen richtig. * Diese Loesung zaehlt ebenfalls 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 STRING %% "\"" { ccode_found = 1; BEGIN(STRING); } "\"" { ccode_found = 1; BEGIN(INITIAL); } "/*" { comment_found = 1; BEGIN(COMMENT); } "*/" { BEGIN(INITIAL); } \n { if (comment_found) comment_lines++; ccode_lines++; comment_found = 0; } \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 */ [^"\n] /* nichts tun */ %% int main() { yylex(); printf("Zeilen mit C-Code: %d, Zeilen mit Kommentaren: %d, Leerzeilen: %d\n", ccode_lines, comment_lines, empty_lines); }