Topic: Grammar Error in Standard?


Author: david.thompson1@worldnet.att.net (Dave Thompson)
Date: Mon, 31 Jul 2006 05:13:29 GMT
Raw View
On Sun, 23 Jul 2006 13:47:39 GMT, kanze.james@neuf.fr (James Kanze)
wrote:

> Alf P. Steinbach wrote:
>
>  > It would of course be simpler with a more elegant Pascal-like
>  > syntax ;-) where the semicolons are not part of the
>  > statements, but rather separate statements: then the for-loop
>  > syntax could include the semicolon.
>
> Not separate statements, but statement separators.  <snip>

I think he meant, and that certainly can be read as:
(semicolons are not part of the statements)
but (semicolons separate (v.t.) statements)
which is semantically correct though a little awkward.

Your: (semicolons are not part of the statements)
but ([semicolons are] statement separators)
is I agree better.

My preferred and I believe traditional formulation is
(semicolons are not statement terminators)
but ([semicolons are statement] separators)

- David.Thompson1 at worldnet.att.net

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Seungbeom Kim <musiphil@bawi.org>
Date: Mon, 24 Jul 2006 14:47:43 CST
Raw View
Jack Klein wrote:
>
> I will say I much prefer the grammar as shown in the C99 standard.

The actual grammar for the 'for' statement in C++ is the following:

iteration-statement
::= for ( for-init-statement condition_opt ; expression_opt ) statement

for-init-statement
::= expression-statement
  | simple-declaration

and in C99:

iteration-statement
::= for ( expression_opt ; expression_opt ; expression_opt ) statement
  | for ( declaration expression_opt ; expression_opt ) statement

where the second form is not very different from the C++ version,
in that the semicolon is absorbed in the definition of 'declaration'.
And you'll see that the C++ version just combined the two forms in the
C99 version, and hopefully that these two are basically the same (except
that the C++ version also accepts a declaration in the second part).

I guess the grammar(s for C99 and C++) had to be that way in order to
reuse that for the declaration, which already contains a semicolon. Reuse
not only means concise description but also consistency. Though it may
not be very easy at first sight (which is true in general when reading
the standard), I understand that it will be hard to make it much better
than the current one.

--
Seungbeom Kim

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Jack Klein <jackklein@spamcop.net>
Date: Sun, 23 Jul 2006 01:58:12 CST
Raw View
A poster named Matthias Hofmann asked about for loops in
comp.lang.c++.moderated, below is a Google URL to his question.

http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/be847de4954e32f5/10cef801a3e710fa#10cef801a3e710fa

At first I thought he must have misread the C++ standard, but in
looking at both the 1998 and 2003 PDF files, I think there is an error
in the grammar section.

In 6.5 P1 of both versions, and in 6.5.3 P1 of both versions, this
appears:

   for ( for-init-statement condition opt ; expression opt ) statement

Unless I am seriously mistaken, there is a missing "opt" subscript and
semicolon after "for-init-statement", and is should read:

for ( for-init-statement opt;  condition opt ; expression opt )
statement

.without wrapping, of course.

6.5.3 P2 of both versions adds to the confusion:

"Either or both of the condition and the expression can be omitted. A
missing condition makes the implied while clause equivalent to
while(true)."

Note that it does not say that "for-init-statement" may be omitted.

Am I right, or is:

   for ( ; ; ) /* some statement */ ;

.invalid ISO C++?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: James Dennett <jdennett@acm.org>
Date: Sun, 23 Jul 2006 02:09:33 CST
Raw View
Jack Klein wrote:
> A poster named Matthias Hofmann asked about for loops in
> comp.lang.c++.moderated, below is a Google URL to his question.
>
> http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/be847de4954e32f5/10cef801a3e710fa#10cef801a3e710fa
>
> At first I thought he must have misread the C++ standard, but in
> looking at both the 1998 and 2003 PDF files, I think there is an error
> in the grammar section.
>
> In 6.5 P1 of both versions, and in 6.5.3 P1 of both versions, this
> appears:
>
>    for ( for-init-statement condition opt ; expression opt ) statement
>
> Unless I am seriously mistaken, there is a missing "opt" subscript and
> semicolon after "for-init-statement", and is should read:
>
> for ( for-init-statement opt;  condition opt ; expression opt )
> statement
>
> .without wrapping, of course.

for-init-statement includes the  trailing ";", and may
be  empty apart from that, as ";" is a valid
expression-statement.  (Every expression-statement and
every simple-declaration ends in a ";".)

So I think the grammar is correct as given.

-- James

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: alfps@start.no ("Alf P. Steinbach")
Date: Sun, 23 Jul 2006 07:28:02 GMT
Raw View
* Jack Klein:
> A poster named Matthias Hofmann asked about for loops in
> comp.lang.c++.moderated, below is a Google URL to his question.
>
> http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/be847de4954e32f5/10cef801a3e710fa#10cef801a3e710fa
>
> At first I thought he must have misread the C++ standard, but in
> looking at both the 1998 and 2003 PDF files, I think there is an error
> in the grammar section.
>
> In 6.5 P1 of both versions, and in 6.5.3 P1 of both versions, this
> appears:
>
>    for ( for-init-statement condition opt ; expression opt ) statement
>
> Unless I am seriously mistaken, there is a missing "opt" subscript and
> semicolon after "for-init-statement", and is should read:
>
> for ( for-init-statement opt;  condition opt ; expression opt )
> statement
>
> .without wrapping, of course.

You're mistaken: a for-init-statement is defined as expression-statement
or simple-declaration.

Both of which end with semicolon.

An expression-statement can be empty, consisting of a single semicolon.

It would of course be simpler with a more elegant Pascal-like syntax ;-)
where the semicolons are not part of the statements, but rather separate
statements: then the for-loop syntax could include the semicolon.

However, given section 5 of Brian Kernighan's paper at* <url:
http://cm.bell-labs.com/cm/cs/cstr/100.ps.gz>, I think that was never a
realistic option.


*) Pure text version at <url:
http://neil.franklin.ch/Jokes_and_Fun/Why_Pascal_not_Favorite_Language>;
there's also an HTML version at Lysator, but the server has hickups.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: kanze.james@neuf.fr (James Kanze)
Date: Sun, 23 Jul 2006 13:47:39 GMT
Raw View
Alf P. Steinbach wrote:

 > It would of course be simpler with a more elegant Pascal-like
 > syntax ;-) where the semicolons are not part of the
 > statements, but rather separate statements: then the for-loop
 > syntax could include the semicolon.

Not separate statements, but statement separators.  And simpler
only holds for writing the grammar.  (I presume you know this,
and that it is the reason for your smiley.)  This is, I think,
one of the rare points of programming language design where
actual objective information exists: studies have actually
measured the difference in the number of errors with semicolons
as separators and semicolons as terminators, and terminators win
hands down.

Wirth got around the problem in his later languages (e.g.
Modula-2) by designing the grammar so that anywhere one
statement was allowed, a list of statements was allowed.  If you
wrote "statement;", it was actually a list of two statements,
the statement you wrote, followed by an empty statement, exactly
as in Pascal.  Except that it was always legal, which wasn't the
case in Pascal.

Note that C++ uses separators in a lot of other cases.  And
often allows an optional additional separator at the end, simply
to make life easier for the programmer (or the program
generating the program).  The comma in an initialization list,
for example.

 > However, given section 5 of Brian Kernighan's paper at* <url:
 > http://cm.bell-labs.com/cm/cs/cstr/100.ps.gz>, I think that
 > was never a realistic option.

I think there is pretty much a consensus today that this was an
error in Pascal.  Even among people who like Pascal otherwise.

--=20
James Kanze                                    kanze.james@neuf.fr
Conseils en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France +33 (0)1 30 23 00 34

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: jackklein@spamcop.net (Jack Klein)
Date: Mon, 24 Jul 2006 13:56:06 GMT
Raw View
On Sun, 23 Jul 2006 01:58:12 CST, Jack Klein <jackklein@spamcop.net>
wrote in comp.std.c++:

> A poster named Matthias Hofmann asked about for loops in
> comp.lang.c++.moderated, below is a Google URL to his question.
>
> http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/be847de4954e32f5/10cef801a3e710fa#10cef801a3e710fa
>
> At first I thought he must have misread the C++ standard, but in
> looking at both the 1998 and 2003 PDF files, I think there is an error
> in the grammar section.
>
> In 6.5 P1 of both versions, and in 6.5.3 P1 of both versions, this
> appears:
>
>    for ( for-init-statement condition opt ; expression opt ) statement
>
> Unless I am seriously mistaken, there is a missing "opt" subscript and
> semicolon after "for-init-statement", and is should read:
>
> for ( for-init-statement opt;  condition opt ; expression opt )
> statement
>
> .without wrapping, of course.
>
> 6.5.3 P2 of both versions adds to the confusion:
>
> "Either or both of the condition and the expression can be omitted. A
> missing condition makes the implied while clause equivalent to
> while(true)."
>
> Note that it does not say that "for-init-statement" may be omitted.
>
> Am I right, or is:
>
>    for ( ; ; ) /* some statement */ ;
>
> .invalid ISO C++?

Obviously, followups both here and in comp.lang.c++.moderated indicate
that I am indeed wrong.

I will say I much prefer the grammar as shown in the C99 standard.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]