Declarations and Definitions: Grammar

Discussion in 'C++' started by Chris Gordon-Smith, Oct 3, 2004.

  1. 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
     
    Chris Gordon-Smith, Oct 3, 2004
    #1
    1. Advertising

  2. "Chris Gordon-Smith" <> wrote in message
    news:...
    > 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.
     
    Ruslan Abdikeev, Oct 3, 2004
    #2
    1. Advertising

  3. "Chris Gordon-Smith" <> wrote in message
    news:...
    > 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
     
    Gary Labowitz, Oct 3, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rikard Land
    Replies:
    0
    Views:
    358
    Rikard Land
    Oct 19, 2004
  2. Steven T. Hatton

    Declarations and Definitions in One Header

    Steven T. Hatton, Apr 25, 2004, in forum: C++
    Replies:
    4
    Views:
    342
    Steven T. Hatton
    Apr 25, 2004
  3. Paulo Matos
    Replies:
    4
    Views:
    426
    Gilles =?UTF-8?B?Si4gU8OpZ3Vpbg==?=
    Nov 19, 2006
  4. Jess
    Replies:
    15
    Views:
    521
  5. panku

    declarations and definitions

    panku, Feb 1, 2011, in forum: C Programming
    Replies:
    6
    Views:
    312
    David Thompson
    Feb 11, 2011
Loading...

Share This Page