Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Declarations and Definitions: Grammar

Reply
Thread Tools

Declarations and Definitions: Grammar

 
 
Chris Gordon-Smith
Guest
Posts: n/a
 
      10-03-2004
I am currently in India and have treated myself to the Indian reprint
of O'Reilly's "C++ In A Nutshell". (Books in India come in at 1/3 to
1/2 of the price in Britain.)

I thought that I would have a look at what Chapter 12 on grammar says
about Declarations and Definitions. Now I'm more baffled than when I
started.

Here's the problem.

Firstly some terminology. The book defines the symbol '::=' for a
complete definition of a particular item. It gives the following
example:-

function-specifier ::= explicit | inline | virtual

(The vertical bar represents a choice.)

The symbol ':=' indicates an incomplete definition, eg:=

function-specifier := inline.

Now, under the section on 'declaration', we have:-

translation-unit ::= [declaration-seq]
declaration-seq ::= declaration | declaration-seq declaration

(The [] brackets represent an optional item.)

What the above amounts to is the idea that a translation unit
(typically a source file and the code in all the #includes it brings
in) contains a series of zero or more declarations.

This implies that the following translation unit contains two
declarations and one definition:-

void MyFunc(int a); // In MyFunc.h: declaration without a definition

#include "MyFunc.h" // In MyFunc.cpp
void MyFunc(int a) { cout << a << endl; } // Declaration with a
definition.

So far so good (I think). Now the odd bit.

In the same (declaration) section of the book, declaration is defined
as follows:-

declaration ::= block-decl | function-decl | template-decl |
explicit-instantiation | explicit-specialization |
linkage-specification | namespace-defn

By contrast, under the section 'function' we have:-

declaration := function-defn

a) I can't find a definition for function-decl.

b) The two definitions for "declaration" seem to be contradictory. The
second says that a declaration can be a function-defn, the first says
that it cannot.

The idea that a declaration cannot include a function definition
contradicts the idea that a translation unit is (solely) a sequence of
declarations.

Can anyone throw any light on this. Is it an error in the book, or
have I misunderstood something?

Chris Gordon-Smith
Pune, India
 
Reply With Quote
 
 
 
 
Ruslan Abdikeev
Guest
Posts: n/a
 
      10-03-2004
"Chris Gordon-Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> This implies that the following translation unit contains two
> declarations and one definition:-
>
> void MyFunc(int a); // declaration without a definition
> void MyFunc(int a) { cout << a << endl; } // Declaration with a
> definition.


Yes.
Specifically, it contains one "simple declaration" and one "function
definition".
Both "simple declaration" and "function definition" are treated as
declarations ("declaration").

> a) I can't find a definition for function-decl.


I think there may be a typo in the book: there is no such thing as a
"function declaration".
A function declaration is just a "simple declaration".

> The idea that a declaration cannot include a function definition
> contradicts the idea that a translation unit is (solely) a sequence of
> declarations.


Actually, grammar summary in C++ standard treats "function definition" as
"declaration" and "translation unit" as "(possibly empty) sequence of
declarations".

Hope it make things clearer,
Ruslan Abdikeev.


 
Reply With Quote
 
 
 
 
Gary Labowitz
Guest
Posts: n/a
 
      10-03-2004
"Chris Gordon-Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I am currently in India and have treated myself to the Indian reprint
> of O'Reilly's "C++ In A Nutshell". (Books in India come in at 1/3 to
> 1/2 of the price in Britain.)

<<snip>>
> a) I can't find a definition for function-decl.
>
> b) The two definitions for "declaration" seem to be contradictory. The
> second says that a declaration can be a function-defn, the first says
> that it cannot.


I believe that some refer to a function declaration as the same thing as
what was in C called a prototype.
For that matter, many people call it a prototype in C++. I don't know what
the standard says, however.
The idea, if I'm right, is that a declaration indicates to the compiler what
an identifer means. It need not cause allocation of any storage (as in a
forward declaration or a function declaration) or it might also include
allocation of storage (as in a data declarations) which means it is also a
definition. With functions it is a little easier to see since the definition
(function body) is a separate part from the header. But I believe the entire
structure of functions (header and body) would be a declaration and
definition.
--
Gary


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strange grammar about volatile and operator overload George2 C++ 8 01-31-2008 07:45 PM
Want Grammar of VHDL'87 and '93 saorjag1729 Hardware 0 12-13-2007 09:37 AM
C++ grammar on function declarations Paulo Matos C++ 4 11-19-2006 10:04 AM
grammar for where/letting/with and suite expressions (thunks etc) Bengt Richter Python 0 04-19-2005 03:31 AM
make a spell and grammar check possible in text area of a web page. Bhupesh Naik ASP .Net 2 08-07-2003 08:04 AM



Advertisments