473,513 Members | 2,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ grammar on function declarations

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
4 2502
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5268
by: Karalius, Joseph | last post by:
Can anyone explain what is happening here? I haven't found any useful info on Google yet. Thanks in advance. mmagnet:/home/jkaralius/src/zopeplone/Python-2.3.5 # make gcc -pthread -c...
0
1490
by: Bengt Richter | last post by:
We have where syntax in combination with suite expression syntax (bear with me, I think a good synergy will emerge ;-) ...
2
1818
by: Chris Gordon-Smith | last post by:
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...
7
2194
by: xxx | last post by:
I am having difficulty seeing why cv_qualifier, which is used as a type specifier in the declaration section of the ISO/IEC grammar, is defined in the declarator section. A declaration can consist...
12
2155
by: Ali | last post by:
I have the following web page with a script in it. <html> <head> <title>Make Your Own Objects test</title> <script>
5
2811
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
5
3776
by: Remco van Engelen | last post by:
Hello, I have a question regarding the ISO C grammar. The syntax of a direct-declarator reads (section A.2.2, page 413 in my copy; the (R1) is just to 'name' the rule for later reference): ...
0
1642
by: Ole Nielsby | last post by:
The C++ syntax goes like: type-id: type-specifier-seq abstract-declarator(opt) type-specifier-seq: type-specifier type-specifier-seq(opt) which means, a type-id has 1 or more...
6
2284
by: aarklon | last post by:
Hi all, here is the on ANSI C grammar dependency graph Flickr - Photo Sharing! http://www.flickr.com/photo_zoom.gne?id=281055530&size=o
0
7161
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.