Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Christmas Quiz 2022 Date: Tue, 20 Dec 2022 10:22:05 -0800 Organization: None to speak of Lines: 80 Message-ID: <87pmceq6k2.fsf@nosuchdomain.example.com> References: <20221220054654.91@kylheku.com> <87tu1quou7.fsf@bsb.me.uk> <87ili6unpn.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain Injection-Info: reader01.eternal-september.org; posting-host="e07f61ae636564fa3e440a4cbc40a9ec"; logging-data="803716"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+0rpD8biWQYgTqDUT3la0d" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:M+sAqXcXuh1egZb+T11DmBL8S/U= sha1:+IGbJ13t4XVEd3bqSuxTiamR2R0= Xref: csiph.com comp.lang.c:168617 Lew Pitcher writes: > On Tue, 20 Dec 2022 15:03:36 +0000, Lew Pitcher wrote: > >> On Tue, 20 Dec 2022 14:57:56 +0000, Ben Bacarisse wrote: > [snip] >>>> The notable point is that the semicolon between clause-1 and >>>> expression-2 is /not/ part of clause-1, but is, instead part of the >>>> for statement itself. >>> >>> But that does not help if the grammar for a declaration still includes >>> a semicolon. I wonder what all the relevant parts of the draft you are >>> referring to look like when taken together. >>> >>>> Because of this, I don't believe that your count can be correct. >>>> But, I'm willing to be educated on this :-) >>> >>> Well, I did add (but then deleted) that the count depends on how the >>> grammar is structured. I should look at a more recent draft that will >>> have taken the confirmed C11 text as a starting point. I might do that >>> later, but I am in a rush! >> >> Perhaps I've got an outdated copy of the draft standard, and the semi- >> colon question was asked and answered in a later release. >> >> Your explanation sounds valid to me. 'Tis a puzzle to be resolved later. > > Oops... I found it. > > Under 6.8.5 Iteration statements > Syntax > ... > for ( expression ; expression ; expression ) statement > for ( declaration expression ; expression ) statement > > (all expressions are marked optional) > > Crisis averted. You are indeed correct, and I've learned something > new today. :-) > > But, that doesn't explain why 6.8.5.3 ("the for statement") shows > the syntax it does. Perhaps the draft missed something in resolving > the text between 6.8.5 and 6.8.5.3. Summary: You've found a bug in the standard, and not just in the draft. Yes, the grammar is defined in 6.8.5 "Iteration statements". The text in 6.8.5.3 "The for statement" talks about the semantics of for statements: The statement for ( clause-1 ; expression-2 ; expression-3 ) statement behaves as follows: [...] If clause-1 is a declaration, [...] The problem is that if clause-1 is a declaration, it includes its own trailing semicolon, which contradicts the grammar in 6.8.5. The ability to have a declaration in the header of a for statement was introduced in C99 (borrrowed from C++). In C90, the "Iteration statements" and "The for statement" sections were consistent, and the grammar was for ( expression[opt] ; expression[opt] ; expression[opt ) statement (I use square brackets to denote a subscript.) My guess is that when the grammar was updated for C99, the author of the update initially tried to have a unified grammar, replacing the first expression by "clause-1" which could be either an expression or a declaration. They then realized that semicolon caused a problem and updated 6.8.5 accordingly, but forgot to update 6.8.5.3. This error occurs in the published C99 and C11 standards and in drafts of C17 and C23 (N3054). -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for XCOM Labs void Void(void) { Void(); } /* The recursive call of the void */