Sed wins! It IS possible to strip C comments with 1 sed command!
Maarten Litmaath
maart at cs.vu.nl
Tue Mar 21 11:22:14 AEST 1989
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);
}
--
Modeless editors and strong typing: |Maarten Litmaath @ VU Amsterdam:
both for people with weak memories. |maart at cs.vu.nl, mcvax!botter!maart
More information about the Comp.unix.wizards
mailing list