Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ grammar on function declarations

Reply
Thread Tools

C++ grammar on function declarations

 
 
Paulo Matos
Guest
Posts: n/a
 
      11-14-2006
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-14-2006
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


 
Reply With Quote
 
 
 
 
Paulo Matos
Guest
Posts: n/a
 
      11-15-2006

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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-15-2006
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


 
Reply With Quote
 
Gilles =?UTF-8?B?Si4gU8OpZ3Vpbg==?=
Guest
Posts: n/a
 
      11-19-2006
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 ;"

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the correct grammar to make a function call by using static member data which is a pointer to a ordinary class member function? zaeminkr@gmail.com C++ 3 07-06-2007 12:50 PM
grammar function Ken Sington Perl Misc 14 12-13-2004 01:55 PM
Declarations and Definitions: Grammar Chris Gordon-Smith C++ 2 10-03-2004 11:32 AM
function declarations, global/within a function? Douglas C Programming 2 07-05-2004 08:54 PM
Local function declarations Dave Theese C++ 1 09-05-2003 05:58 AM



Advertisments