C++ grammar on function declarations

P

Paulo Matos

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
 
V

Victor Bazarov

Paulo said:
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
 
P

Paulo Matos

Victor said:
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
 
V

Victor Bazarov

Paulo said:
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
 
G

Gilles =?UTF-8?B?Si4gU8OpZ3Vpbg==?=

Paulo said:
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 ;"
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top