home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / question / 9555 < prev    next >
Encoding:
Text File  |  1992-07-29  |  3.4 KB  |  147 lines

  1. Path: sparky!uunet!darwin.sura.net!mips!bridge2!patience!div
  2. From: div@NAD.3Com.COM (Dinesh Venkatachalam x5498)
  3. Newsgroups: comp.unix.questions
  4. Subject: Re: Good way to strip K&R style comments
  5. Message-ID: <1251@bridge2.NSD.3Com.COM>
  6. Date: 30 Jul 92 00:50:44 GMT
  7. Sender: news@bridge2.NSD.3Com.COM
  8. Reply-To: div@NAD.3Com.COM
  9. Organization: 3Com Corp
  10. Lines: 134
  11. Nntp-Posting-Host: patience.nad.3com.com
  12.  
  13. 1.The shortest perl solution was from  guru and was as follows
  14.  
  15. perl -e 'undef($/);$_=<>;s/\/\*[^\000]*\*\///g;print'   filename
  16.  
  17. 2.The most popular method was 
  18.  
  19. pipe it through cpp
  20.  
  21. 3. The lex solution ( flex ?) did not work on my Sun OS lex processor but is as follows
  22.    from johnl
  23.  
  24. %x COMMENT
  25.  
  26. %%
  27.  
  28. "/*"        BEGIN COMMENT;
  29. <COMMENT>"*/"    BEGIN INITIAL;
  30. <COMMENT>.    /* ignore */ ;
  31. <COMMENT>\n    /* ignore */ ;
  32.  
  33. In AT&T lex, I ususally write explicit code to eat the comment once I've
  34. seen the /*, like this:
  35.  
  36. "/*"        {    int c1 = 0, c2 = 0;
  37.  
  38.             for(;;) {
  39.                 c1 = c2; c2 = input();
  40.                 if(c1 == '*' && c2 == '/')
  41.                     break;
  42.                 if(c2 == EOF)
  43.                     /* complain about unterm'ed comment */
  44.             }
  45.         }
  46.  
  47.  
  48. Is there any way to make this work on standard lex ??
  49.  
  50. 4. The C program solution from matt via bhuff was
  51.  
  52. /******************************************************************************
  53. **  File Name:        uncommentc.c
  54. **  Procedure Name:    uncommentc
  55. **  Purpose:        Remove comments from C source.
  56. **  Created by:        Matthew Rabuzzi
  57. **
  58. **  Calling Example:    uncommentc foo.c bar.c baz.c
  59. **            more $* | uncommentc | awk '...'
  60. **
  61. **  Parameters:        Optional filename(s).  If none, reads from stdin.
  62. **   
  63. **  Description:    Uncomments C code, that is, any text enclosed in
  64. **            the comment delimiters '/*' and its inverse,
  65. **            including the delimiters, except within quotes.
  66. **            I.e., text of 'x /* y *\ z' (pretend the \ is a / )
  67. **            becomes 'x  z', but 'x "/* y *\" z' is output as is.
  68. **
  69. **  Bugs:        Does not yet handle C++ // end-of-line comments.
  70. **
  71. ******************************************************************************/
  72.  
  73. #include <stdio.h>
  74.  
  75. void uncomment (void);
  76.  
  77. main (int argc, char **argv)
  78. {
  79.     int        i, rtnstatus = 0;
  80.  
  81.     if (argc > 1)
  82.         for (i=1; i < argc; i++) {
  83.         if (!freopen(argv[i],"r",stdin)) {
  84.         rtnstatus = 2;
  85.         fprintf(stderr,"%s: could not open file %s\n",
  86.             argv[0], argv[i]);
  87.         }
  88.         else uncomment();
  89.     }
  90.     else uncomment();
  91.  
  92.     exit(rtnstatus);
  93. }
  94.  
  95. void uncomment (void)
  96. {
  97.     int        c, d, comment=0, quote=0;
  98.     long    lineno=1;
  99.  
  100.     while ((c = getchar()) != EOF) {
  101.  
  102.     if (comment) {
  103.         if (c == '*') {
  104.         if ((d = getchar()) == '/') comment = 0;
  105.         else ungetc(d,stdin);
  106.         }
  107.  
  108.     } else if (quote) {
  109.         if (c == '"') quote = 0;
  110.         putchar(c);
  111.  
  112.     } else if (c == '/') {
  113.         if ((d = getchar()) == '*') comment = lineno;
  114.         else {
  115.         ungetc(d,stdin);
  116.         putchar(c);
  117.         }
  118.  
  119.     } else if (c == '"') {
  120.         quote = lineno;
  121.         putchar(c);
  122.  
  123.     } else {
  124.         putchar(c);
  125.     }
  126.  
  127.     if (c == '\n') lineno++;
  128.  
  129.     }
  130.  
  131.     /* Sanity checks.  If the code compiled, these should never occur. */
  132.     if (comment) printf ("/* Begin-comment on line %d, no end-comment */\n",
  133.              comment);
  134.     if (quote)   printf ("/* Begin-quote on line %d, no end-quote */\n",
  135.              quote);
  136. }
  137.  
  138.  
  139. -------------------------------------------------------------------------------------
  140.  
  141. Well thats all, and thanks for the suggestions and replies. I am still looking for
  142. a lex solution as it will help me not running through a pre-processor.
  143.  
  144.  
  145. Dinesh
  146.  
  147.