By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,780 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,780 IT Pros & Developers. It's quick & easy.

C++ grammar on function declarations

P: n/a
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

Nov 14 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Nov 14 '06 #2

P: n/a

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

Nov 15 '06 #3

P: n/a
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
Nov 15 '06 #4

P: n/a
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 ;"

Nov 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.