Topic: Is it an ambiguity in syntax productions for "declaration"?


Author: restor <akrzemi1@gmail.com>
Date: Mon, 21 Feb 2011 13:20:55 CST
Raw View
Hi,
I am looking at grammar productions for /declaration/. They are in
N3225 at the beginning of chapter 7. Among other /declaration/s, there
are two:

simple-declaration:
 attribute-specifier-seq[opt] decl-specifier-seq[opt] init-declarator-
list[opt] ;

empty-declaration:
 ;

Since everything, except a semicolon, is optional in /simple-
declaration/, if I have a the following code
   {;}
(brace, semicolon, brace), is a semicolon captured by /simple-
declaration/ with all optional elements gone, or /empty-declaration/?
I do not know how those syntax productions work, but is it not an
ambiguity?

Regards,
&rzej


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





Author: "Johannes Schaub (litb)" <schaub.johannes@googlemail.com>
Date: Tue, 22 Feb 2011 11:00:30 CST
Raw View
restor wrote:

>
> Hi,
> I am looking at grammar productions for /declaration/. They are in
> N3225 at the beginning of chapter 7. Among other /declaration/s, there
> are two:
>
> simple-declaration:
> attribute-specifier-seq[opt] decl-specifier-seq[opt] init-declarator-
> list[opt] ;
>
> empty-declaration:
> ;
>
> Since everything, except a semicolon, is optional in /simple-
> declaration/, if I have a the following code
>    {;}
> (brace, semicolon, brace), is a semicolon captured by /simple-
> declaration/ with all optional elements gone, or /empty-declaration/?
> I do not know how those syntax productions work, but is it not an
> ambiguity?
>

The token-sequence ";" cannot be a simple-declaration, because there is a
syntax constraint given by 7p3 which says

   In a simple-declaration, the optional init-declarator-list can be
   omitted only when declaring a class (Clause 9) or enumeration
   (7.2), that is, when the decl-specifier-seq contains either a
   class-specifier, an elaborated-type-specifier with a class-key (9.1),
   or an enum-specifier.

Therefor, to be a simple-declaration, either there must be a init-
declarator-list or the decl-specifier-seq must not be empty (and be parsed
by one of those productions it gives). The important thing is that parsing
is not only defined by the grammar, but also by supplementary rules.

But notice that in a block, the "declaration-statement" is a "block-
declaration". A "block-declaration" cannot be an "empty-declaration".
Therefor in your example, the "{;}" is not an "empty-declaration", but is a
block where there is an "expression-statement" with the optional
"expression" being omitted ("null statement").

The utility of "empty-declaration" is that it allows a mere ";" to appear in
namespace scope, for example after a function definition:

 void f() { } ;


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