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

C grammar for direct-declarator question

P: n/a

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
Share this Question
Share on Google+
5 Replies


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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.