473,418 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

C grammar for direct-declarator question


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):

(R1)
direct-declarator:
identifier
"(" declarator ")"
direct-declarator "[" type-qualifier-list? assignment-expression? "]"
direct-declarator "[" static type-qualifier-list?
assignment-expression "]"
direct-declarator "[" type-qualifier-list "static"
assignment-expression "]"
direct-declarator "[" type-qualifier-list? "*" "]"
direct-declarator "(" parameter-type-list ")"
direct-declarator "(" identifier-list? ")"

I can left-factor this rule into:

(R2)
direct-declarator:
( identifier
| "(" declarator ")"
)
( "[" type-qualifier-list? assignment-expression? "]"
| "[" static type-qualifier-list? assignment-expression "]"
| "[" type-qualifier-list "static" assignment-expression "]"
| "[" type-qualifier-list? "*" "]"
| "(" parameter-type-list ")"
| "(" identifier-list? ")"
)*

Without changing the grammar (I think thats correct, right?).

The semantics of declarations states that in a valid C program, a
function may never return a function or an array type, and an array may
not contain a function type. What I am wondering is if this means that
I can further rewrite the grammar rule for direct-declarator to read:

(R3)
direct-declarator:
( identifier
| "(" declarator ")"
)
( ( "[" type-qualifier-list? assignment-expression? "]"
| "[" static type-qualifier-list? assignment-expression "]"
| "[" type-qualifier-list "static" assignment-expression "]"
| "[" type-qualifier-list? "*" "]"
)*
| ( "(" parameter-type-list ")"
| "(" identifier-list? ")"
)
)

Without getting syntax errors on valid C programs ofcourse.

So, the real question is: is there a valid C program which is accepted
in a grammar in which I use rule R1, but which would be rejected by a
grammar in which I use rule R3?

Thanks for any insights or comments.

Regards

Remco van Engelen

Sep 14 '06 #1
5 3767
Remco van Engelen schrieb:
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):

(R1)
direct-declarator:
identifier
"(" declarator ")"
direct-declarator "[" type-qualifier-list? assignment-expression? "]"
direct-declarator "[" static type-qualifier-list?
assignment-expression "]"
direct-declarator "[" type-qualifier-list "static"
assignment-expression "]"
direct-declarator "[" type-qualifier-list? "*" "]"
direct-declarator "(" parameter-type-list ")"
direct-declarator "(" identifier-list? ")"

I can left-factor this rule into:

(R2)
direct-declarator:
( identifier
| "(" declarator ")"
)
( "[" type-qualifier-list? assignment-expression? "]"
| "[" static type-qualifier-list? assignment-expression "]"
| "[" type-qualifier-list "static" assignment-expression "]"
| "[" type-qualifier-list? "*" "]"
| "(" parameter-type-list ")"
| "(" identifier-list? ")"
)*

Without changing the grammar (I think thats correct, right?).

The semantics of declarations states that in a valid C program, a
function may never return a function or an array type, and an array may
not contain a function type. What I am wondering is if this means that
I can further rewrite the grammar rule for direct-declarator to read:

(R3)
direct-declarator:
( identifier
| "(" declarator ")"
)
( ( "[" type-qualifier-list? assignment-expression? "]"
| "[" static type-qualifier-list? assignment-expression "]"
| "[" type-qualifier-list "static" assignment-expression "]"
| "[" type-qualifier-list? "*" "]"
)*
| ( "(" parameter-type-list ")"
| "(" identifier-list? ")"
)
)

Without getting syntax errors on valid C programs ofcourse.

So, the real question is: is there a valid C program which is accepted
in a grammar in which I use rule R1, but which would be rejected by a
grammar in which I use rule R3?

Thanks for any insights or comments.
Plenty:
Start from
,---
#include <stdio.h>

typedef int (*T_ReturnsInt) (void);

static int foo (void)
{
return 42;
}

static T_ReturnsInt bar (int baz)
{
return foo;
}

int main (void)
{
int qux = bar(0)();
printf("%d\n", qux);
return 0;
}
`---

You can then work your way through arrays of function pointers,
returned pointers to arrays etc. and arrive at
a = (b(i)[c(i)])(i)[0][i];
and other fun things.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 14 '06 #2
Michael Mair wrote:
Remco van Engelen schrieb:
(R3)
direct-declarator:
( identifier
| "(" declarator ")"
)
( ( "[" type-qualifier-list? assignment-expression? "]"
| "[" static type-qualifier-list? assignment-expression "]"
| "[" type-qualifier-list "static" assignment-expression "]"
| "[" type-qualifier-list? "*" "]"
)*
| ( "(" parameter-type-list ")"
| "(" identifier-list? ")"
)
)

Without getting syntax errors on valid C programs ofcourse.

So, the real question is: is there a valid C program which is accepted
in a grammar in which I use rule R1, but which would be rejected by a
grammar in which I use rule R3?

Thanks for any insights or comments.

Plenty:
[...]
int qux = bar(0)();
bar(0)() is an expression, not a declarator.

Sep 14 '06 #3
Harald van Dijk schrieb:
Michael Mair wrote:
>>Remco van Engelen schrieb:
>>>(R3)
direct-declarator:
( identifier
| "(" declarator ")"
)
( ( "[" type-qualifier-list? assignment-expression? "]"
| "[" static type-qualifier-list? assignment-expression "]"
| "[" type-qualifier-list "static" assignment-expression "]"
| "[" type-qualifier-list? "*" "]"
)*
| ( "(" parameter-type-list ")"
| "(" identifier-list? ")"
)
)

Without getting syntax errors on valid C programs ofcourse.

So, the real question is: is there a valid C program which is accepted
in a grammar in which I use rule R1, but which would be rejected by a
grammar in which I use rule R3?

Thanks for any insights or comments.

Plenty:
[...]
int qux = bar(0)();

bar(0)() is an expression, not a declarator.
You are right -- I did not really look at it and my fingers coded
before I thought. Thanks for the correction; a _real_ example not
covered by (R3):
int (* (*a)(int))[5];

-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 14 '06 #4
Michael Mair wrote:
Harald van Dijk schrieb:
Michael Mair wrote:
>Remco van Engelen schrieb:

(R3)
direct-declarator:
( identifier
| "(" declarator ")"
)
( ( "[" type-qualifier-list? assignment-expression? "]"
| "[" static type-qualifier-list? assignment-expression "]"
| "[" type-qualifier-list "static" assignment-expression "]"
| "[" type-qualifier-list? "*" "]"
)*
| ( "(" parameter-type-list ")"
| "(" identifier-list? ")"
)
)

Without getting syntax errors on valid C programs ofcourse.

So, the real question is: is there a valid C program which is accepted
in a grammar in which I use rule R1, but which would be rejected by a
grammar in which I use rule R3?

Thanks for any insights or comments.

Plenty:
[...]
int qux = bar(0)();
bar(0)() is an expression, not a declarator.

You are right -- I did not really look at it and my fingers coded
before I thought. Thanks for the correction; a _real_ example not
covered by (R3):
int (* (*a)(int))[5];
Actually, that is covered too. It is of the form
"(" declarator ")" "[" assignment-expression "]"
regardless of the fact that <declaratorcontains more parentheses.

Sep 14 '06 #5
Harald van Dijk schrieb:
Michael Mair wrote:
>>Harald van Dijk schrieb:
>>>Michael Mair wrote:

Remco van Engelen schrieb:
>(R3)
>direct-declarator:
> ( identifier
> | "(" declarator ")"
)
( ( "[" type-qualifier-list? assignment-expression? "]"
> | "[" static type-qualifier-list? assignment-expression "]"
> | "[" type-qualifier-list "static" assignment-expression "]"
> | "[" type-qualifier-list? "*" "]"
)*
> | ( "(" parameter-type-list ")"
> | "(" identifier-list? ")"
> )
)
>
>Without getting syntax errors on valid C programs ofcourse.
>
>So, the real question is: is there a valid C program which is accepted
>in a grammar in which I use rule R1, but which would be rejected by a
>grammar in which I use rule R3?
>
>Thanks for any insights or comments.

Plenty:
[...]
int qux = bar(0)();

bar(0)() is an expression, not a declarator.

You are right -- I did not really look at it and my fingers coded
before I thought. Thanks for the correction; a _real_ example not
covered by (R3):
int (* (*a)(int))[5];

Actually, that is covered too. It is of the form
"(" declarator ")" "[" assignment-expression "]"
regardless of the fact that <declaratorcontains more parentheses.
*g* I give up, I am too tired today...

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
1
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
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 ;-) ...
0
by: Chad Whitacre | last post by:
Hey all, I've been playing around with the parser module, and based on the documentation I would expect all symbols in a parse tree to be part of the grammar. For example, I find this line in...
2
by: Peter Rilling | last post by:
I am written a program that will be used to parse the lexical syntax of code files. I would like to generalize the grammar logic so that I don't hardcode any specific grammar in my program. ...
4
by: ben | last post by:
getting a bit confused with the details of how c's grammar is specified, especially when you get self-reference like in this: postfix-expression: primary-expression postfix-expression ...
3
by: junky_fellow | last post by:
I got one link to the ANSI C Grammar http://www.lysator.liu.se/c/ANSI-C-grammar-y.html However, I don't know how to understand this grammar. I am not a Computer Science Guy. Can anybody please...
2
by: Bhupesh Naik | last post by:
This is a query regarding my problem to make a spell and grammar check possible in text area of a web page. We have aspx pages which are used to construct letters. The browser based screens...
7
by: Jon Slaughter | last post by:
I'm curious as to if .NET provides any direct means to modifying its own grammar? e.g., if say I want to had some "features" to C# which can easily be reinterpreted back into the original C# but I...
10
by: MBR | last post by:
Hello... I'm using the grammar at: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_c.asp as a reference in creating my own C# parser using a custom...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.