%{ /* In der Vorlesung am 4.12.2006 gemeinsam erstellte Lösung für einen * C-Quellcode-Zähler. */ #include int comments = 0, ccode = 0, wspace = 0; int commentFlag = 0, ccodeFlag = 0; %} %x COMMENT %x STRING %option noyywrap %% <*>\n { if (!commentFlag && !ccodeFlag) wspace++; else if (!ccodeFlag) comments+=commentFlag; else ccode+= ccodeFlag; commentFlag=0; ccodeFlag=0; } "/*" { BEGIN COMMENT; commentFlag = 1; } "\"" { ccodeFlag = 1; BEGIN STRING; } "\"" { ccodeFlag = 1; BEGIN INITIAL; } "\\\"" ccodeFlag = 1; "\\\\" ccodeFlag = 1; . ccodeFlag = 1; [^ \t] { ccodeFlag = 1; } [^ \t] { commentFlag = 1; } "*/" { BEGIN INITIAL; commentFlag = 1; } <*>[ \t] /* no-op */ %% int main() { yylex(); printf("%d Code-Zeilen, %d Nur-Kommentar-Zeilen, %d ganz leere Zeilen\n", ccode, comments, wspace); }