C++ grammar on function declarations

Discussion in 'C++' started by Paulo Matos, Nov 14, 2006.

  1. Paulo Matos

    Paulo Matos Guest

    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.
    First, I can't find a function-declaration non-terminal. The closest is
    function-definition, but that will need the function-body, which cannot
    end up with ;.

    I guess this has something to do with direct-declarator, but still I
    can't see how ';' shows up in the end.

    Any help on where to start would be extremmely helpful.

    Regards,

    Paulo Matos
     
    Paulo Matos, Nov 14, 2006
    #1
    1. Advertising

  2. Paulo Matos wrote:
    > 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.
    > First, I can't find a function-declaration non-terminal. The closest
    > is function-definition, but that will need the function-body, which
    > cannot end up with ;.


    It can within a class definition (unfortunately).

    > I guess this has something to do with direct-declarator, but still I
    > can't see how ';' shows up in the end.
    >
    > Any help on where to start would be extremmely helpful.


    You probably should ask specific questions.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 14, 2006
    #2
    1. Advertising

  3. Paulo Matos

    Paulo Matos Guest

    Victor Bazarov wrote:
    > Paulo Matos wrote:
    > > 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.
    > > First, I can't find a function-declaration non-terminal. The closest
    > > is function-definition, but that will need the function-body, which
    > > cannot end up with ;.

    >
    > It can within a class definition (unfortunately).
    >


    I thought I was asking a specific question, sorry. In fact, what I
    wanted to know is which is the non-terminal which start function
    declarations inside class definitions. Problem is that looking by the
    grammar I can't understand how it copes with
    class foo {
    int xpto();
    };

    for example. This is because the only thing I can find is
    function-definition which cannot end up with ; [see grammar], so it
    won't parse
    int xpto();

    Now, or I am missing a non-terminal symbol or I'm not reading
    function-definition correctly.

    Cheers,

    Paulo Matos
     
    Paulo Matos, Nov 15, 2006
    #3
  4. Paulo Matos wrote:
    > Victor Bazarov wrote:
    >> Paulo Matos wrote:
    >>> 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.
    >>> First, I can't find a function-declaration non-terminal. The closest
    >>> is function-definition, but that will need the function-body, which
    >>> cannot end up with ;.

    >>
    >> It can within a class definition (unfortunately).
    >>

    >
    > I thought I was asking a specific question, sorry.


    Don't worry about it. It's my fault. I didn't see it.

    > In fact, what I
    > wanted to know is which is the non-terminal


    I guess I am not sure what "non-terminal" you're referring to. I am
    not a grammar pro, and C++ standard doesn't have that term.

    > which start function
    > declarations inside class definitions. Problem is that looking by the
    > grammar I can't understand how it copes with
    > class foo {
    > int xpto();
    > };
    >
    > for example. This is because the only thing I can find is
    > function-definition which cannot end up with ; [see grammar], so it
    > won't parse
    > int xpto();


    The grammar says that the class definition contains a potentially empty
    set of 'member-specifications' inside the curly braces. Each
    'member-specification' is a sequence of 'member-declarations' (possibly
    preceded by an 'access-specifier'). Each 'member-declaration' ends
    with a semicolon unless it's a 'function definition' (after which the
    semicolon is optional), or it's a 'using-declaration', or it's
    a 'template-declaration'. A 'using-declaration' shall end with
    a semicolon. You can probably arrive at a semicolon or a curly brace
    for a 'template-declaration' as well.

    > Now, or I am missing a non-terminal symbol or I'm not reading
    > function-definition correctly.


    You should be reading a 'member-declaration' inside a class definition.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 15, 2006
    #4
  5. Paulo Matos wrote:

    > 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.


    annex A of iso 14882 is the reference.



    > 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.
    > First, I can't find a function-declaration non-terminal. The closest is
    > function-definition, but that will need the function-body, which cannot
    > end up with ;.


    in 3 basic concepts
    translation-unit
    : declaration-seqopt

    in 6 Declarations
    declaration-seq
    : declaration 6.2
    | declaration-seq declaration

    declaration
    : block-declaration 6.3
    | function-definition 7.16
    | template-declaration 12.1
    | explicit-instantiation 12.9
    | explicit-specialization 12.10
    | linkage-specification 6.33
    | namespace-definition 6.21

    block-declaration
    : simple-declaration 6.4
    | asm-definition 6.32
    | namespace-alias-definition 6.28
    | using-declaration 6.30
    | using-directive 6.31

    simple-declaration
    : decl-specifier-seqopt 6.6 init-declarator-listopt 7.1 ;

    > I guess this has something to do with direct-declarator, but still I
    > can't see how ';' shows up in the end.


    grammar has by augmented with reference i.e. 6.6
    non-terminal may have suffix opt, which meaning should be obvious

    the answer is the semi-colon of last rule
    that is "init-declarator-listopt ;"
     
    Gilles =?UTF-8?B?Si4gU8OpZ3Vpbg==?=, Nov 19, 2006
    #5
    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. Dave Theese

    Local function declarations

    Dave Theese, Sep 5, 2003, in forum: C++
    Replies:
    1
    Views:
    459
    Jack Klein
    Sep 5, 2003
  2. Chris Gordon-Smith

    Declarations and Definitions: Grammar

    Chris Gordon-Smith, Oct 3, 2004, in forum: C++
    Replies:
    2
    Views:
    376
    Gary Labowitz
    Oct 3, 2004
  3. Douglas
    Replies:
    2
    Views:
    340
    Herbert Rosenau
    Jul 5, 2004
  4. Replies:
    3
    Views:
    467
  5. Ken Sington

    grammar function

    Ken Sington, Dec 8, 2004, in forum: Perl Misc
    Replies:
    14
    Views:
    209
    Bart Lateur
    Dec 13, 2004
Loading...

Share This Page