473,587 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "declaratio n" 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 1822
"Chris Gordon-Smith" <c_************ @yahoo.com> wrote in message
news:c3******** *************** ***@posting.goo gle.com...
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
"declaratio n" and "translatio n 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.goo gle.com...
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 "declaratio n" 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
6280
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 way to "declare" functions without defining them?
0
1264
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 C), which can be nested, and where each member can be (optionally) associated with a) a comment (as a help to the user who will enter data) and b) a...
4
1289
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 each object that you use across more than one source file. First, you need an interface specification, describing its structure with type declarations...
14
2151
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 'savestruct' in the subject line. I cannot for the life of me figure out how to parse C declarations correctly! I've come up with an intermediate...
5
10135
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 seems to reveal that there are still places where this is illegal. For instance, consider the following code snippet. int main(void) { int i = 0;
3
4484
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 typing all the time. Should I do: void foo() { typedef MyLib::Foundation::int32 int32; int32 x = 0; }
10
1570
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
2510
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 something like: int foo(int, int); const double *xpto(mytype *, mytype &) const; But I'm not being able to find my way around the grammar.
15
2157
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 (a_obj) of a class type (A), then we need to define A as well, but if B has a pointer to A, then we only need to forward declare A. I was told this...
0
7918
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6621
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.