C grammar for direct-declarator question

Discussion in 'C Programming' started by Remco van Engelen, Sep 14, 2006.

  1. 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
     
    Remco van Engelen, Sep 14, 2006
    #1
    1. Advertising

  2. Remco van Engelen

    Michael Mair Guest

    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];
    and other fun things.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Sep 14, 2006
    #2
    1. Advertising

  3. 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.
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Sep 14, 2006
    #3
  4. Remco van Engelen

    Michael Mair Guest

    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.
     
    Michael Mair, Sep 14, 2006
    #4
  5. 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 <declarator> contains more parentheses.
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Sep 14, 2006
    #5
  6. Remco van Engelen

    Michael Mair Guest

    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 <declarator> contains more parentheses.


    *g* I give up, I am too tired today...

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Sep 14, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vasileios
    Replies:
    4
    Views:
    490
    Rolf Magnus
    Nov 4, 2003
  2. Gil
    Replies:
    0
    Views:
    468
  3. Per Johansson
    Replies:
    3
    Views:
    1,330
    Mike Wahler
    Nov 7, 2004
  4. xxx
    Replies:
    7
    Views:
    887
  5. mdh

    Declarator question

    mdh, Jul 16, 2008, in forum: C Programming
    Replies:
    6
    Views:
    338
    David Thompson
    Jul 28, 2008
Loading...

Share This Page