Eric Sosman <eso...@comcast-dot-net.invalid> writes:
> On 9/24/2013 6:55 AM, Ben Bacarisse wrote:
>> Öö Tiib <
oot...@hot.ee> writes:
<snip>
>>> do
>>> while(a > 0) // does do-while end here? No. There are no semicolon.
>>
>> There's no ambiguity here. 'do' must be followed by a statement, so the
>> 'while' is clearly the start of a statement, not the end of a
>> 'do...while'. The shortest 'do...while' has an empty statement in the
>> middle:
>>
>> do ; while (C);
>>
>>> {
>>> printf("Such a: %d\n", a);
>>> a--;
>>> }
>>> while(a = b--);
>>>
>>> Other option could be to require always compound statement inside
>>> 'do-while' loop. The authors of C did choose to require semicolon
>>> at end.
>>
>> I don't think there would be any ambiguity if the ';' at the end of
>> 'do...while' where not there. I think it's needed for symmetry rather
>> than for parsing.
>
> do while(A) while(B) while(C) while(D);
>
> This is unambiguous in C-as-it-stands,
Hmm, it's a syntax error, is it not? (I'm not contradicting you, a
syntax error is as unambiguous as anything else, but I am not 100% sure
what you gain from the example).
> but if the trailing
> semicolon after `do-while' were omitted there would need to be
> some other rule to decide whether a `while' begins a statement
> nested within the `do-while' or whether it ends the `do-while'.
I don't follow. In a construct like the above, each while must be the
start of a new statement (the first inside the do and the rest inside
the previous while. The semicolon ends the chain, so the parser now
expects a 'while' to end the 'do'. When it seems it, it need not care
if there is a ';' after the ')' or not. I.e the valid form of the above:
do while(A) while(C) while(C) while(D); while(E);
is as unambiguous (to my casual review of the syntax) with or without
the trailing ';'.
> Such a rule could certainly be invented, something like the rule
> for associating an `else' with the proper `if'. But I don't
> think simply dropping the semicolon would work without some
> other compensating change to the language.
--
Ben.