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

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!europa.asd.contel.com!darwin.sura.net!convex!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: Good way to strip K&R Style comments
  5. Message-ID: <1992Jul28.222222.1507@news.eng.convex.com>
  6. Originator: tchrist@pixel.convex.com
  7. Sender: usenet@news.eng.convex.com (news access account)
  8. Nntp-Posting-Host: pixel.convex.com
  9. Reply-To: tchrist@convex.COM (Tom Christiansen)
  10. Organization: CONVEX Realtime Development, Colorado Springs, CO
  11. References: <1249@bridge2.NSD.3Com.COM>
  12. Date: Tue, 28 Jul 1992 22:22:22 GMT
  13. X-Disclaimer: This message was written by a user at CONVEX Computer
  14.               Corp. The opinions expressed are those of the user and
  15.               not necessarily those of CONVEX.
  16. Lines: 40
  17.  
  18. From the keyboard of div@NAD.3Com.COM:
  19. :
  20. :I am looking for any way ( using awk, sed, perl, lex ) to strip K&R style
  21. :comments.
  22. :/* Start of comment till this 
  23. :    and on the next line */
  24. :
  25. :The comments can span multiple lines, and I want this script to process the
  26. :input file and strip out everything including and between the /* and */
  27.  
  28. This works well enough for me -- it's derived from lwall's h2ph program.
  29. Partial comments in #if 0 clauses may surprise you.
  30.  
  31.     #!/usr/bin/perl
  32.     # uncomment -- strip C comments
  33.     while (<>) {
  34.         chop;
  35.         while (/\\$/) {
  36.             chop;
  37.             $_ .= <>;
  38.             chop;
  39.         }
  40.         if (s:/\*:\200:g) {
  41.             s:\*/:\201:g;
  42.             s/\200[^\201]*\201//g;      # delete single line comments
  43.             if (s/\200.*//) {           # begin multi-line comment?
  44.                 $_ .= '/*';
  45.                 $_ .= <>;
  46.                 redo;
  47.             }
  48.         }
  49.     print $_, "\n";
  50.     }
  51.  
  52. --tom
  53. -- 
  54.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  55.     If you have ever seen the grim word "login:" on a screen, your mind
  56.     is now a wholly-owned subsidiary of The Death Star.
  57.                 John Woods in <14105@ksr.com> of comp.unix.bsd
  58.