Declarations and Definitions: Grammar

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Gordon-Smith

    #1

    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
  • Ruslan Abdikeev

    #2
    Re: Declarations and Definitions: Grammar

    "Chris Gordon-Smith" <c_gordon_smith @yahoo.com> wrote in message
    news:c3723a6c.0 410022214.7c349 82f@posting.goo gle.com...[color=blue]
    > 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.[/color]

    Yes.
    Specifically, it contains one "simple declaration" and one "function
    definition".
    Both "simple declaration" and "function definition" are treated as
    declarations ("declaration") .
    [color=blue]
    > a) I can't find a definition for function-decl.[/color]

    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".
    [color=blue]
    > The idea that a declaration cannot include a function definition
    > contradicts the idea that a translation unit is (solely) a sequence of
    > declarations.[/color]

    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.


    Comment

    • Gary Labowitz

      #3
      Re: Declarations and Definitions: Grammar

      "Chris Gordon-Smith" <c_gordon_smith @yahoo.com> wrote in message
      news:c3723a6c.0 410022214.7c349 82f@posting.goo gle.com...[color=blue]
      > 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.)[/color]
      <<snip>>[color=blue]
      > 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.[/color]

      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


      Comment

      Working...