[comp.lang.c] Want a way to strip comments from a C program

jim@bilpin.UUCP (Jim G) (03/22/89)

    	#{ v_langC.1 }
	Lots of postings on how not do this, and how other peoples
	suggestions won't work, but something of a shortage of answers. 
	Weep no more ...
	if( Followup ~ /doesn't work with/ && Followup !~ /solution is/ )
	  print Followup > "/dev/null"
	:-)

#{ zapcom.sh }
#  Remove comments from a C program
#  sed removes comment strings which begin and end on the same line
#  awk removes comment strings which extend across multiple lines
#  sed/awk both handle nesting of comments within their context

sed -e ':nest' \
    -e 's?/\*[^/\*][^/\*]*\*/??g' \
    -e 'tnest' yourinput.c \
| awk '
/\/\*/	{ if( COML == 0 )
	    print substr( $0, 1, index( $0, "/*" )-1 )
	  for( F=1; F<=NF; F++ )
	    if( $F == "/*" )
	      COML++
	}
/\*\//	{ REST = $0
	  for( F=1; F<=NF; F++ )
	    if( $F == "*/" )
	    { COML-- ; REST = substr( REST, index( REST, "*/" ) ) }
	  if( COML == 0 )
	    print substr( REST, 3 )
	  next
	}
COML==0	{ print }
' > youroutput.c
-- 
	   <Path: mcvax!ukc!icdoc!bilpin!jim> <UUCP: jim@bilpin.uucp>
		  This line has been intentionally left blank.