Xref: utzoo comp.lang.c:17084 comp.unix.wizards:15129 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!unmvax!pprg.unm.edu!hc!lll-winken!uunet!mcvax!hp4nl!botter!star.cs.vu.nl!maart From: maart@cs.vu.nl (Maarten Litmaath) Newsgroups: comp.lang.c,comp.unix.wizards Subject: Sed wins! It IS possible to strip C comments with 1 sed command! Message-ID: <2186@solo11.cs.vu.nl> Date: 21 Mar 89 01:22:14 GMT References: <7150@siemens.UUCP> <9900010@bradley> <4221@omepd.UUCP> <981@philmds.UUCP> <982@philmds.UUCP> Organization: V.U. Informatica, Amsterdam, the Netherlands Lines: 120 leo@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 ''". 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); } -- Modeless editors and strong typing: |Maarten Litmaath @ VU Amsterdam: both for people with weak memories. |maart@cs.vu.nl, mcvax!botter!maart