C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Grammar Error in Standard?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Jack Klein
Guest





PostPosted: Sun Jul 23, 2006 7:58 am    Post subject: Grammar Error in Standard? Reply with quote



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 ]
Back to top
James Dennett
Guest





PostPosted: Sun Jul 23, 2006 8:09 am    Post subject: Re: Grammar Error in Standard? Reply with quote



Jack Klein wrote:
Quote:
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 ]
Back to top
Alf P. Steinbach
Guest





PostPosted: Sun Jul 23, 2006 9:10 am    Post subject: Re: Grammar Error in Standard? Reply with quote



* Jack Klein:
Quote:
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 Wink
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 ]
Back to top
James Kanze
Guest





PostPosted: Sun Jul 23, 2006 6:47 pm    Post subject: Re: Grammar Error in Standard? Reply with quote

Alf P. Steinbach wrote:

Quote:
It would of course be simpler with a more elegant Pascal-like
syntax Wink 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.

Quote:
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.

--
James Kanze kanze.james (AT) neuf (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, 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 ]
Back to top
Seungbeom Kim
Guest





PostPosted: Mon Jul 24, 2006 8:47 pm    Post subject: Re: Grammar Error in Standard? Reply with quote

Jack Klein wrote:
Quote:

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 ]
Back to top
Dave Thompson
Guest





PostPosted: Mon Jul 31, 2006 9:10 am    Post subject: Re: Grammar Error in Standard? Reply with quote

On Sun, 23 Jul 2006 13:47:39 GMT, kanze.james (AT) neuf (DOT) fr (James Kanze)
wrote:

Quote:
Alf P. Steinbach wrote:

It would of course be simpler with a more elegant Pascal-like
syntax Wink 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 ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.