Want a way to strip comments from a C file

Michael Condict mnc at m10ux.UUCP
Mon Mar 13 13:58:06 AEST 1989


I recently posted to this group a shell script that calls three sed scripts
to extract function prototypes from (practically) any C source.  One of the
three sed scripts consisted of little more than comment removal -- exactly
what you are looking for.  Here is the relevant portion:

--------------------------------------------------------------------------
# Delete comments:
: delcom
/\/\*/{
	# Change first comment delim to @ (after eliminating existing @'s):
	s/@/<Used#to%be+an-At>/g
	s:/\*:@:

	# Read until we have the end comment:
	: morecm
	/\*\//!{
		# Just to cut down on max buffer length:
		s/@.*/@/
		N
		b morecm
	}

	# Get rid of any $'s:
	s/\$/<Used#to%be+a-Dollar>/g

	# First occurrence of */ is guaranteed to be the corresponding end
	# comment, because it is otherwise not legal C, so:
	s:\*/:$:
	s/@[^$]*\$/ /

	# Restore $'s and @'s:
	s/<Used#to%be+a-Dollar>/$/g
	s/<Used#to%be+an-At>/@/g

	b delcom
}
------------------------------------------------------------------------------

The disclaimers are that (1) it only works with BSD-derived sed, unless you
get rid of all the comments; and (2) it will fail for programs that contain
the extremely unlikely "Used#to%be..." strings used as markers in the script.

This has been tested on thousands of lines of source code from various sources,
but no guarantees.  You get what you pay for.

Mike Condict
-- 
Michael Condict		{att|allegra}!m10ux!mnc
AT&T Bell Labs		(201)582-5911    MH 3B-416
Murray Hill, NJ



More information about the Comp.lang.c mailing list