C comment remover

Mark Moraes moraes at cs.toronto.edu
Fri Dec 22 09:24:30 AEST 1989


In Sun-Spots-Digest Volume 8, Issue 219, Mukul Agrawal asks for a tool to
remove C comments.

The following sed script, posted by maart at cs.vu.nl (Maarten Litmaath) to
comp.unix.wizards seems to remove comments cleanly and correctly.

X_From: maart at cs.vu.nl (Maarten Litmaath)
X_Subject: Sed wins! It IS possible to strip C comments with 1 sed command!

leo at philmds.UUCP (Leo de Wit) writes:
\Can it be proven to be impossible (that is, deleting the comments
\with one sed command - multi-line comments not considered) ?

No, because the script below WILL do it. It won't touch "/*...*/" inside
strings. Multi-line comments ARE considered and handled OK.
One can either use "sed -f script" or "sed -n '<contents of script>'".
After the script some test input follows (an awful but valid C program).
Spoiler: the sequence

	H
	x
	s/\n\(.\).*/\1/
	x
	s/.//

deletes the first character of the pattern space and appends it to the hold
space; this space contains the characters not to be deleted.

----------8<----------8<----------8<----------8<----------8<----------
#n

: loop
/^$/{
	x
	p
	n
	b loop
}
/^"/{
	: double
	/^$/{
		x
		p
		n
		b double
	}
	H
	x
	s/\n\(.\).*/\1/
	x
	s/.//
	/^"/b break
	/^\\/{
		H
		x
		s/\n\(.\).*/\1/
		x
		s/.//
	}
	b double
}
/^'/{
	: single
	/^$/{
		x
		p
		n
		b single
	}
	H
	x
	s/\n\(.\).*/\1/
	x
	s/.//
	/^'/b break
	/^\\/{
		H
		x
		s/\n\(.\).*/\1/
		x
		s/.//
	}
	b single
}
/^\\/{
	H
	x
	s/\n\(.\).*/\1/
	x
	b break
}
/^\/\*/{
	s/.//
	: comment
	s/.//
	/^$/n
	/^*\//{
		s/..//
		b loop
	}
	b comment
}
: break
H
x
s/\n\(.\).*/\1/
x
s/.//
b loop

----------8<----------8<----------8<----------8<----------8<----------

main()
{
	/* this
	 * is
	   a comment
	 */
	char /* Z /* Z / Z * Z /*/ *s = "/*", /* Z /* Z / Z * Z **/ c = '*',
		d = '/', f = '\\', g = '\'',

		*q = "*/", *p = "\
/* these characters are\
 inside a string \"\\\
*/";
	int	i = 12 / 2 * 3;

	exit(0);
}



More information about the Comp.sys.sun mailing list