Want a way to strip comments from a C program

Jim G jim at bilpin.UUCP
Wed Mar 22 22:35:15 AEST 1989


    	#{ 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 at bilpin.uucp>
		  This line has been intentionally left blank.



More information about the Comp.lang.c mailing list