Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: Programming exercise/challenge
Date: Sat, 23 Jan 2021 11:22:33 -0800
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <86eeibmpae.fsf@linuxsc.com>
References: <86wnxwkyol.fsf@linuxsc.com> <86r1o1hxtn.fsf@linuxsc.com> <20201209030320.4dad4f2c624e613bd47fceed@gmail.com> <86a6tlqdh8.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="52b538951ae99e6f169b0f62774a9fae"; logging-data="30895"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+NIuXhxKzn1Hmv+m749vS3/giVpZehnGQ="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:5czI4MC8cpikYI8A86z3ndoC4i0= sha1:ePgpbYkyhB1mI9hpjFG8m9fF9v4=
Xref: csiph.com comp.lang.c:158586
Bart writes:
> On 23/01/2021 04:27, Dave Dunfield wrote:
>
>>> I forgot that that last example would be taken care of elsewhere, so
>>> decided to have a go. I also combined the handling for string and char
>>> handling:
>>
>> Hey,
>>
>> Sorry - don't mean to be critical (but that does seem to be
>> the norm here so I guess I'm "fitting in" :)
>
> That's fine. I used to test other people's contributions too and point
> out problems!
>
>
>> --- Consider the source ---
>> #include
>> #define TAB /*
>> comment */ '\\
>> t'
>> main()
>> {
>> putc('A', stdout);
>> putc(TAB, stdout);
>> putc('B', stdout);
>> }
>>
>> --- The program you posted produces ---
>> #include
>> #define TAB
>> '\\
>> t'
>
> The issue here is with /*...*/ comments, not the \\ part.
>
> I made a decision to convert /*ABC*/ to all spaces, eg. 7 spaces here,
> but because this could screw up layout, I also decided to keep
> newlines within the comment.
>
> Clearly, this causes a problem with examples like yours.
I have written a decommenting program that leaves characters
outside of comments in their original character and line positions
(including dealing with cases in preprocessor lines like the one
shown above). It is decidedly a more challenging program to
write, which is one reason I suggested replacing removed comments
by a single space. (That is just for /**/ comments; new style
comments can simply be removed entirely, except of course for
the newline at the end, which properly speaking is not part of
the comment.)
> The fix is easy; just comment out this line (line 99 I think):
>
> outchar((c=='\n' ? c : ' '));
>
> (A single space will have been output when /* is detected.)
In your latest program, there are five occurrences of
outchar(' ');
all of which have to do with replacing comments with spaces.
In addition to commenting out the outchar() call on line 99,
if all of the "outchar(' ')" calls are commented out, _except_
the one inside the "if (peekchar=='/')", then the program
should behave in the way suggested and preferred for this
exercise, which is to say removed /**/ comments shall be
replaced by a single space.