[ILUG] strip comments utility

Charles Sharp charles_sharp at yahoo.com
Fri Dec 8 14:41:54 GMT 2000


Greetings All,

--- ilug-admin at linux.ie wrote: (I'm in digest mode...)

> Date: Fri, 8 Dec 2000 02:30:59 +0000
> From: Conor Daly <conor.daly at oceanfree.net>
> To: ilug at linux.ie
> Subject: Re: [ILUG] strip comments utility
> 
> On Fri, Dec 08, 2000 at 01:07:17AM +0000 or so it is
> rumoured hereabouts, 
> Smelly Pooh thought:
> > In reply to John McCormac's flatulent wordings, 
> > > 
> > > 
> > > On Thu, 7 Dec 2000, Michael Turley wrote:
> > > 
> > > > Does anyone know of a utility to strip C style (/*
> */)
> > > > comments from code. I have a rake of stuff with
> alot
> > > > of CVSg material which I would like to remove. Does
> > > > CVS have this type of functionality?
> > > 
> > > You could try something along the lines of:
> > > 
> > > perl -p -i.bak -e 's/oldstring/newstring/g' filename
> > > 
> > > The incantation for the comments would be something
> along the lines of
> > > [\/*][a-zA-Z0-9-.,]*[*\/] though that may blow the
> machine up and do
> > > irreprable harm. Perhaps some of the Regexp experts
> here could give the
> > > right incantation.
> > 
> > cpp will strip comments, but also add stuff in include
> files
> > 
> > perl wise... 
> > 
> > perl -n -e 'print unless (/\/\*/ .. /\*\//)' filename
> > 
> > should do fine
> > 
> Will that work across lines also (since you can use
> comments of the form
> 
> /* First line
>  * Second line
>  * Last line */
> )?
> -- 
> Conor Daly <conor.daly at oceanfree.net>
> 
> Domestic Sysadmin :-)
> 


Well, no it won't.

In fact, it turns out that the regexp required for general
C comment removal job is extremely difficult. (Some
compilers permit embedded comments...)  A full (and
then some) discussion of this can be found in a truly
excellent book, _Mastering Regular Expressions_ by Jeff
Friedl, published by O'Reilly.  This book dedicates a lot
of space to perl's use of regexp's as well as containing a
lot of very useful general information about regexp
construction and how the different regexp engines work. 
(I'd enclose the "best" regexp for C comments from the
book, but my books are in transit from the U.S. -- I'll
post it later unless someone has already done it by then).

regards,
cas

Charles Sharp, Developer, Sun Ireland

__________________________________________________
Do You Yahoo!?
Yahoo! Shopping - Thousands of Stores. Millions of Products.
http://shopping.yahoo.com/




More information about the ILUG mailing list