Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!noao!arizona!naucse!jdc From: jdc@naucse.UUCP (John Campbell) Newsgroups: comp.software-eng Subject: Re: C source lines in file Message-ID: <1658@naucse.UUCP> Date: 19 Aug 89 17:43:55 GMT References: <6500@pdn.paradyne.com> Organization: Northern Arizona University, Flagstaff, AZ Lines: 98 From article <6500@pdn.paradyne.com>, by reggie@dinsdale.nm.paradyne.com (George W. Leach): > In article <10707@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn) writes: >>In article <35120@ccicpg.UUCP> swonk@ccicpg.UUCP (Glen Swonk) writes: >>-Does anyone have a program or a method of determing >>-the number of C source lines in a source file? >>-My assumption is that comments don't count as source >>-lines unless the comment is on a line with code. > >>What precisely is this supposed to measure? > > I also want to know just what you are going to measure with this number? Who knows? I, for one, think the comments are the only valid part of the code--what if the language changes or a better algorithm is found. Hope Glen's employeer isn't trying to create some sweatshop. Anyway, here's a lex goodie I use to count comments, *exactly* what he wanted, right? Note that the output is in lines of 'C' code, so you could look very productive if you counted those lines of code instead! OBTW, this comment recognizer works well enough for my style of commenting. It does not solve the general problem of recognizing ANSI 'C' comments with a regular expression. A solution to that problem was posted a while back, but it's pretty ugly... =====cut here for comments.l==== %{ FILE *ifp; int lineno=0, incom=0, com_bytes=0, cod_bytes=0,comments=0, code=0, scom = 0; #define yyin ifp %} %% \/\* { incom = 1; scom = 1; com_bytes +=2; } \*\/ { incom = 0; com_bytes +=2; } \n { lineno++; if (incom) com_bytes++; else cod_bytes++; if (scom) comments++; else code++; if (!incom) scom = 0; } . { if (incom) com_bytes += yyleng; else cod_bytes += yyleng; } %% main(argc, argv) int argc; char *argv[]; { int i; FILE *fopen(); if (argc < 2) { fprintf (stderr, "useage: %s in_file [in_file2] [...]\n", argv[0]); exit(1); } for (i = 1; i < argc; i++) { if ((ifp = fopen (argv[i], "r")) == NULL) { fprintf (stderr, "%s: can't open %s\n", argv[0], argv[i]); exit(1); } yylex(); if (argc > 2) printf ("\n"); printf ("%-14s lines:%5d, comment:%5d, code:%5d, comment/lines: %5.3f\n", argv[i], lineno, comments, code, ((float )comments)/lineno); printf (" bytes:%5d, comment:%5d, code:%5d, comment/total: %5.3f\n", com_bytes+cod_bytes, com_bytes, cod_bytes, ((float )com_bytes)/(com_bytes+cod_bytes)); } /* test.c lines: 10, comment: 4, code: 6, comment/lines: 0.400 bytes: 201, comment: 81, code: 120, comment/total: 0.403 */ } yywrap() { return(1); } -- John Campbell ...!arizona!naucse!jdc CAMPBELL@NAUVAX.bitnet unix? Sure send me a dozen, all different colors.