473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Declarations and Definitions: Grammar

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
Jul 22 '05 #1
2 1807
"Chris Gordon-Smith" <c_************@yahoo.com> wrote in message
news:c3**************************@posting.google.c 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.
Jul 22 '05 #2
"Chris Gordon-Smith" <c_************@yahoo.com> wrote in message
news:c3**************************@posting.google.c 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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Joerg Schuster | last post by:
Hello, according to http://mail.python.org/pipermail/tutor/2001-July/007246.html the order of function definitions does matter in python. Does anyone know a trick to avoid this? Is there a...
0
by: Rikard Land | last post by:
I try to model a data definition language in XML. It can be seen as C without any executable statements other than variable assignments. I want to allow for: (1) type declarations ("structs" in...
4
by: Steven T. Hatton | last post by:
Now why didn't *_I_* think of that! Declarations and Definitions in One Header C++ object definitions can be quite complex. In principle, your source code will need two kinds of things for...
14
by: Arthur J. O'Dwyer | last post by:
Well, I'm trying to write that program that was requested a few weeks back, the one that could take struct definitions and create portable functions to read and write those structs. Hence the...
5
by: Neil Zanella | last post by:
Hello, Unlike in pre-C99 versions of C where variables can only be defined at the beginning of blocks, C99 allows variables to be defined in arbitrary places inside blocks. However, gcc 3.2.2...
3
by: pyramus | last post by:
I am wondering which one should be preferred for creating a local synonym within a function definition. Let's say I have a type in my library called MyLib::Foundation::int32, which I can't stand...
10
by: Xiaoshen Li | last post by:
Dear All, I am confused with prototypes in C. I saw the following code in a C book: void init_array_1(int data) { /* some code here */ }
4
by: Paulo Matos | last post by:
Hi all, I'm trying to work out a parser for function declarations but it turns out that it is harder than I initially thought. I'm looking at 3rd Ed of Stroustrup, page 808. I'm trying to parse...
15
by: Jess | last post by:
Hello, Sometimes declarations are all what we need when we define/declare classes (or functions?), but sometimes we need definitions. I learned that if we define a class (B) that has an object...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.