This one bit me today

Michael Condict condict at cs.vu.nl
Thu Nov 9 21:06:50 AEST 1989


In article <136 at csnz.co.nz> paul at csnz.co.nz (Paul Gillingwater) writes:
>In article <11463 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
>>The most reasonable one was to add //-comment-to-end-of-line style
>>comments.  ...  However, it was quite a bother
>>when our //-ed code had to be compiled by a compiler that hadn't
>>received this hack!
>
>sed -d ?^//? < foo.c > tmp.c ; cc tmp.c -o foo

I get a little irritated when people post pithy little chunks of code or
scripts of shell sessions with no commentary as a reply to a question or to
refute a claim that something is hard to do -- it seems like an unnecessarily
smug response.  It is particulary irritating when the code is wrong.

I don't know what the -d flag does in your sed command (it's not accepted by
any version I've seen), but in order for the above sed command to work
correctly, it would have to mean something like: ``ignore ^ at beginning
of following pattern argument, pretend it has ".*$" at the end of it and delete
substrings of any line that match the (assumed) pattern, but only if the
substring is not part of any C quoted string, char or /**/-type comment''.

Seriously, it seems that you were making an attempt to do the following:

	sed -e '?^//.*$?d'  . . .

using some bizarre abbreviation in your sed, which maps ``-d ?pat?'' to
``-e ?pat?d''.  This won't work for two reasons: (1) Doug Gwyn never said that
the comments had to start in column one. (2) Therefore, they may be preceded
by C quoted strings, char constants or /**/-type comments that happen to
contain the string //.  Even if they are restricted to column one, what about:

	/* This comment contains what looks
	// like a double-slash comment but is not. */ int f(x) int x {
		return x+1;
	}

The problem can be correctly handled by a sed script, but one considerably
more complicated then the one you show.
-- 
Michael Condict		condict at cs.vu.nl
Vrije University
Amsterdam



More information about the Comp.lang.c mailing list