Article View: comp.sys.sun
Article #13362Re: C comment remover
From: Mark Moraes
Date: Thu, 21 Dec 1989 22:24
Date: Thu, 21 Dec 1989 22:24
129 lines
2489 bytes
2489 bytes
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@cs.vu.nl (Maarten Litmaath) to
comp.unix.wizards seems to remove comments cleanly and correctly.
X_From: maart@cs.vu.nl (Maarten Litmaath)
X_Subject: Sed wins! It IS possible to strip C comments with 1 sed command!
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 '<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);
}
Technical Details
Message-ID:
<4041@brazos.Rice.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!rice!sun-spots-requestShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!rice!sun-spots-request From: moraes@cs.toronto.edu (Mark Moraes) Newsgroups: comp.sys.sun Subject: Re: C comment remover Keywords: Source Message-ID: <4041@brazos.Rice.edu> Date: 21 Dec 89 22:24:30 GMT Sender: root@rice.edu Organization: Sun-Spots Lines: 129 Approved: Sun-Spots@rice.edu X-Refs: Original: v8n219 X-Sun-Spots-Digest: Volume 8, Issue 231, message 1 of 16