Declarations and Definitions: Grammar

  • Thread starter Chris Gordon-Smith
  • Start date
C

Chris Gordon-Smith

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
 
R

Ruslan Abdikeev

Chris Gordon-Smith said:
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.
 
G

Gary Labowitz

Chris Gordon-Smith said:
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.)
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top