home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / c_lines.zip / C_LINES.L < prev    next >
Text File  |  1992-01-28  |  6KB  |  179 lines

  1. %{
  2.     #include <string.h>
  3.  
  4.     /* These are counts on a per-file basis. (ie. number of whatever's in  */
  5.     /* the current file.  */
  6.  
  7.     unsigned nl=0;               /* lines                                */
  8.     unsigned ppd=0;              /* pre-processor directives             */
  9.     unsigned comments=0;         /* comments ( of course )               */
  10.     unsigned comment_lines=0;    /* lines occupied by comments           */
  11.     unsigned fl_cpp=0;           /* lines occupied only by c++ comments  */
  12.     unsigned skipped=0;          /* lines skipped due to #if 0 directives*/
  13.     unsigned files=0;            /* files we've looked at                */
  14.  
  15.     unsigned nested=0;           /* nesting level of pre-processor lines */
  16.  
  17.     /* this is a list of extensions to search for if none is given      */
  18.     /* if you modify it, be sure to keep a NULL as the last entry       */
  19.     char *extensions[] = {
  20.         ".C",".CPP",".CXX",".H",".HPP",".HXX",".CC",NULL
  21.     };
  22.  
  23.     unsigned matched=0;
  24.  
  25. %}
  26. ws      [ \t\v]*
  27. WS      [ \t\v]+
  28.  
  29. %s SKIP COMMENT
  30. %%
  31.  
  32. <SKIP>^{ws}#{ws}else  {
  33.                         if (--nested == 0)
  34.                             BEGIN INITIAL;
  35.                       }
  36.  
  37. <SKIP>^{ws}#{ws}endif {
  38.                         ppd++;
  39.                         if (--nested==0)
  40.                             BEGIN INITIAL;
  41.                 }
  42.  
  43. <SKIP>\n        {       skipped++;  }
  44.  
  45. <SKIP>^{ws}#{ws}if   {       nested++;   }
  46.  
  47. <INITIAL>^{ws}#  {
  48.                     ppd++;
  49.                 }
  50.  
  51. <INITIAL>^{ws}#{ws}if{ws}0.*$ {
  52.                         ppd++;
  53.                         nested++;
  54.                         BEGIN SKIP;
  55.                 }
  56.  
  57. <INITIAL>"/*"   {
  58.                     comments++;
  59.                     BEGIN COMMENT;
  60.                 }
  61.  
  62. <COMMENT>\n     {
  63.                     comment_lines++;
  64.                 }
  65.  
  66. <COMMENT>"*/"   {
  67.                     BEGIN INITIAL;
  68.                 }
  69.  
  70. <INITIAL>^"//".*$        {
  71.                     fl_cpp++;
  72.                     comments++;
  73.                     comment_lines++;
  74.                 }
  75.  
  76. <INITIAL>"//".*$         {
  77.                     comments++;
  78.                 }
  79.  
  80. <INITIAL>"*/"   {
  81.                     printf("found unmatched close-comment");
  82.                 }
  83.  
  84. \"(\\.|[^\"])*\"        ;   /* This recognizes and ignores string constants */
  85. '[^\\]'|'\\[0-0xFF]'    ;   /* This does the same for character constants   */
  86. <INITIAL>\n             nl++;
  87.  
  88. .                       ;
  89.  
  90. %%
  91.  
  92. int main(int argc,char **argv) {
  93.  
  94. #ifdef __TURBOC__
  95. #include <stdlib.h>
  96. #define _MAX_PATH MAXPATH
  97. #endif
  98.  
  99.     unsigned tppd=0,tnl=0,tl=0;
  100.     unsigned total_comments=0,total_comment_lines=0;
  101.     unsigned total_skipped=0;
  102.     char file_name[_MAX_PATH];
  103.     char **extension;
  104.     char *end;
  105.  
  106.     if (argc<2) {
  107.         /* read standard input if no file name supplied */
  108.         yylex();
  109.         nl-=ppd+fl_cpp;
  110.         tppd+=ppd;
  111.         tnl+=nl;
  112.         total_comments+=comments;
  113.         total_comment_lines+=comment_lines;
  114.         total_skipped+=skipped;
  115.         tl+=fl_cpp+ppd+nl+comment_lines+skipped;
  116.         files=2;
  117.     }
  118.  
  119.     else while (--argc) {
  120.  
  121.         nl=ppd=fl_cpp=comments=comment_lines=skipped=0;
  122.  
  123.         /* otherwise, open each file supplied in turn. */
  124.         strcpy(file_name,*++argv);
  125.         end=strchr(file_name,'\0');
  126.         if ((yyin=fopen(file_name,"r"))==NULL && strchr(*argv,'.')==NULL) {
  127.             for (extension=extensions;**extension;extension++) {
  128.                 strcpy(end,*extension);
  129.                 if ((yyin=fopen(file_name,"r"))!=NULL)
  130.                     break;
  131.             }
  132.         }
  133.         if (yyin==NULL) {
  134.             printf("\n\nError: Unable to open %s\n",*argv);
  135.             continue;
  136.         }
  137.         files++;
  138.         yylex();        /* do dirty work on file itself */
  139.         /* subtract preprocessor lines and full-line    */
  140.         /* c++ comments to get number of code lines.    */
  141.         /* new-lines contained in normal comments are   */
  142.         /* dealt with in the rule for comments          */
  143.         nl-=ppd+fl_cpp;
  144.         /* report what we found                         */
  145.         printf("\n\t\"%s\" contains:"
  146.             "\n%u preprocessor directives, %u comments on %u lines,"
  147.             "\n%u lines skipped due to pre-processor directives"
  148.             "\nand %u lines of code, for a total of %u lines",
  149.             file_name,ppd,comments,comment_lines,skipped,nl,
  150.             ppd+nl+comment_lines+skipped);
  151.         /* current file's stats to totals               */
  152.         tppd+=ppd;
  153.         tnl+=nl;
  154.         total_comments+=comments;
  155.         total_comment_lines+=comment_lines;
  156.         total_skipped+=skipped;
  157.         tl+=fl_cpp+ppd+nl+comment_lines+skipped;
  158.         /* reset yylex() so we can do another file.  If     */
  159.         /* this isn't done, it won't read other files       */
  160.         /* after it has reached te end of the first one.    */
  161.         /* This particular call is specific to  Flex.       */
  162.         /* lex, LeX, etc. use other  mechanisms.  LeX       */
  163.         /* requires that you open files with ii_newfile()   */
  164.         /* I'm not sure how reall AT&T lex deals with this. */
  165.         yyrestart(yyin);
  166.         fclose(yyin);
  167.     }
  168.     /* print final totals                               */
  169.     if (files>1)
  170.         printf("\n\tTotals:"
  171.             "\n%u preprocessor directives, %u comments on %u lines,"
  172.             "\n%u lines skipped due to pre-processor directives"
  173.             "\nand %u lines of code, for a grand total of %u lines"
  174.             "\nmodules average %d lines",
  175.             tppd,total_comments,total_comment_lines,total_skipped,tnl,tl,tl/files);
  176.         /* and we're outta here !                           */
  177.     return 0;
  178. }
  179.