home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!europa.asd.contel.com!darwin.sura.net!convex!convex!tchrist
- From: Tom Christiansen <tchrist@convex.COM>
- Subject: Re: Good way to strip K&R Style comments
- Message-ID: <1992Jul28.222222.1507@news.eng.convex.com>
- Originator: tchrist@pixel.convex.com
- Sender: usenet@news.eng.convex.com (news access account)
- Nntp-Posting-Host: pixel.convex.com
- Reply-To: tchrist@convex.COM (Tom Christiansen)
- Organization: CONVEX Realtime Development, Colorado Springs, CO
- References: <1249@bridge2.NSD.3Com.COM>
- Date: Tue, 28 Jul 1992 22:22:22 GMT
- X-Disclaimer: This message was written by a user at CONVEX Computer
- Corp. The opinions expressed are those of the user and
- not necessarily those of CONVEX.
- Lines: 40
-
- From the keyboard of div@NAD.3Com.COM:
- :
- :I am looking for any way ( using awk, sed, perl, lex ) to strip K&R style
- :comments.
- :/* Start of comment till this
- : and on the next line */
- :
- :The comments can span multiple lines, and I want this script to process the
- :input file and strip out everything including and between the /* and */
-
- This works well enough for me -- it's derived from lwall's h2ph program.
- Partial comments in #if 0 clauses may surprise you.
-
- #!/usr/bin/perl
- # uncomment -- strip C comments
- while (<>) {
- chop;
- while (/\\$/) {
- chop;
- $_ .= <>;
- chop;
- }
- if (s:/\*:\200:g) {
- s:\*/:\201:g;
- s/\200[^\201]*\201//g; # delete single line comments
- if (s/\200.*//) { # begin multi-line comment?
- $_ .= '/*';
- $_ .= <>;
- redo;
- }
- }
- print $_, "\n";
- }
-
- --tom
- --
- Tom Christiansen tchrist@convex.com convex!tchrist
- If you have ever seen the grim word "login:" on a screen, your mind
- is now a wholly-owned subsidiary of The Death Star.
- John Woods in <14105@ksr.com> of comp.unix.bsd
-