Re: Parsing with typedefs

Discussion in 'C Programming' started by Kevin Easton, Jul 2, 2003.

  1. Kevin Easton

    Kevin Easton Guest

    Mauro Persano <> wrote:
    > Hi,
    >
    > In the follow two lines:
    >
    > typedef int foo;
    > unsigned foo;
    >
    > Should the second line be read as an empty declaration
    > (`unsigned int;') or as an attempt to redeclare `foo'?


    You can't apply the unsigned modifier to the typedef foo in a
    declaration anyway - ie, even this is illegal:

    typedef int foo;
    unsigned foo x;

    ie, "unsigned" isn't something that modifies another type, it's just a
    part of some built-in type names that happen to be spelled with more
    than one token.

    > Also, is
    >
    > typedef int foo();
    > foo bar { }
    >
    > legal? Jeff Lee's ANSI C grammar can parse it, and gcc
    > understands at least the first line (although it coughs out
    > a syntax error on the second).


    It's just a semantic error - you can't have a function returning a
    function designator.

    foo *bar();

    should be fine.

    - Kevin.
    Kevin Easton, Jul 2, 2003
    #1
    1. Advertising

  2. Hi,

    Kevin Easton <> wrote in message news:<newscache$jdqdhh$kqk$>...
    > > typedef int foo();
    > > foo bar { }

    > It's just a semantic error - you can't have a function returning a
    > function designator.
    >
    > foo *bar();
    >
    > should be fine.


    Actually, what I meant was:

    typedef int foo(); /* type foo is `function returning int' */
    foo bar { } /* bar is of type foo, that is, bar is
    `function returning int' */

    Thanks,

    Mauro
    Mauro Persano, Jul 2, 2003
    #2
    1. Advertising

  3. Kevin Easton

    Kevin Easton Guest

    Mauro Persano <> wrote:
    > Hi,
    >
    > Kevin Easton <> wrote in message news:<newscache$jdqdhh$kqk$>...
    >> > typedef int foo();
    >> > foo bar { }

    >> It's just a semantic error - you can't have a function returning a
    >> function designator.
    >>
    >> foo *bar();
    >>
    >> should be fine.

    >
    > Actually, what I meant was:
    >
    > typedef int foo(); /* type foo is `function returning int' */
    > foo bar { } /* bar is of type foo, that is, bar is
    > `function returning int' */


    Oh, I see. Well, that's also not allowed. On page 225 of K&R2 we have:

    || Begin Quotation
    A10.1 Function Definitions

    ....

    The declarator in a function declaration must specify explicitly that
    the declared identifier has function type; that is, it must contain one
    of the forms

    direct-declarator ( parameter-type-list )
    direct-declarator ( identifier-list_opt )

    where the direct-declarator is an identifier or a parenthesized
    identifier. In particular, it must not achieve function type by means
    of a typedef.

    || End Quotation

    This is also present in the Standard; in N869 we have:

    6.9.1 Function Definitions

    Constraints

    2 The identifier declared in a function definition (which is the name of
    the function) shall have a function type, as specified by the declarator
    portion of the function definition.

    ...and footnote 128 further describes the forms that this is intended to
    prohibit, which includes yours as a specific example.

    - Kevin.
    Kevin Easton, Jul 3, 2003
    #3
  4. Kevin Easton

    Morris Dovey Guest

    Kevin Easton wrote:

    > On page 225 of K&R2 we have:
    >
    > || Begin Quotation
    > A10.1 Function Definitions
    >
    > ....
    >
    > The declarator in a function declaration must specify explicitly that
    > the declared identifier has function type; that is, it must contain one
    > of the forms
    >
    > direct-declarator ( parameter-type-list )
    > direct-declarator ( identifier-list_opt )
    >
    > where the direct-declarator is an identifier or a parenthesized
    > identifier. In particular, it must not achieve function type by means
    > of a typedef.
    >
    > || End Quotation
    >
    > This is also present in the Standard; in N869 we have:
    >
    > 6.9.1 Function Definitions
    >
    > Constraints
    >
    > 2 The identifier declared in a function definition (which is the name of
    > the function) shall have a function type, as specified by the declarator
    > portion of the function definition.
    >
    > ...and footnote 128 further describes the forms that this is intended to
    > prohibit, which includes yours as a specific example.


    On the other foot...

    C99: 7.23.2.3.1 The mktime function

    "Synopsis
    #include <time.h>
    time_t mktime(struct tm *timeptr);"

    Would lead the reader to believe that a typedef /can/ be used to
    achieve a function type.
    --
    Morris Dovey
    West Des Moines, Iowa USA
    C links at http://www.iedu.com/c
    Morris Dovey, Jul 3, 2003
    #4
  5. Kevin Easton

    Kevin Easton Guest

    Morris Dovey <> wrote:
    > Kevin Easton wrote:
    >
    >> On page 225 of K&R2 we have:
    >>
    >> || Begin Quotation
    >> A10.1 Function Definitions
    >>
    >> ....
    >>
    >> The declarator in a function declaration must specify explicitly that
    >> the declared identifier has function type; that is, it must contain one
    >> of the forms
    >>
    >> direct-declarator ( parameter-type-list )
    >> direct-declarator ( identifier-list_opt )
    >>
    >> where the direct-declarator is an identifier or a parenthesized
    >> identifier. In particular, it must not achieve function type by means
    >> of a typedef.
    >>
    >> || End Quotation
    >>
    >> This is also present in the Standard; in N869 we have:
    >>
    >> 6.9.1 Function Definitions
    >>
    >> Constraints
    >>
    >> 2 The identifier declared in a function definition (which is the name of
    >> the function) shall have a function type, as specified by the declarator
    >> portion of the function definition.
    >>
    >> ...and footnote 128 further describes the forms that this is intended to
    >> prohibit, which includes yours as a specific example.

    >
    > On the other foot...
    >
    > C99: 7.23.2.3.1 The mktime function
    >
    > "Synopsis
    > #include <time.h>
    > time_t mktime(struct tm *timeptr);"
    >
    > Would lead the reader to believe that a typedef /can/ be used to
    > achieve a function type.


    It is the parentheses there that cause the mktime identifier to achieve
    function type.

    - Kevin.
    Kevin Easton, Jul 3, 2003
    #5
  6. Kevin Easton

    Morris Dovey Guest

    Kevin Easton wrote:
    > Morris Dovey <> wrote:
    >
    >>Kevin Easton wrote:
    >>
    >>
    >>>On page 225 of K&R2 we have:
    >>>
    >>>|| Begin Quotation
    >>>A10.1 Function Definitions
    >>>
    >>>....
    >>>
    >>>The declarator in a function declaration must specify explicitly that
    >>>the declared identifier has function type; that is, it must contain one
    >>>of the forms
    >>>
    >>> direct-declarator ( parameter-type-list )
    >>> direct-declarator ( identifier-list_opt )
    >>>
    >>>where the direct-declarator is an identifier or a parenthesized
    >>>identifier. In particular, it must not achieve function type by means
    >>>of a typedef.
    >>>
    >>>|| End Quotation
    >>>
    >>>This is also present in the Standard; in N869 we have:
    >>>
    >>>6.9.1 Function Definitions
    >>>
    >>>Constraints
    >>>
    >>>2 The identifier declared in a function definition (which is the name of
    >>>the function) shall have a function type, as specified by the declarator
    >>>portion of the function definition.
    >>>
    >>>...and footnote 128 further describes the forms that this is intended to
    >>>prohibit, which includes yours as a specific example.

    >>
    >>On the other foot...
    >>
    >>C99: 7.23.2.3.1 The mktime function
    >>
    >>"Synopsis
    >> #include <time.h>
    >> time_t mktime(struct tm *timeptr);"
    >>
    >>Would lead the reader to believe that a typedef /can/ be used to
    >>achieve a function type.

    >
    > It is the parentheses there that cause the mktime identifier to achieve
    > function type.


    Oops! Please excuse my brain fart. I was thinking about the
    _return_ type as I wrote that; and wondering what you were
    thinking of...

    (Looks like time for me to shut this thing off and get some sleep)
    --
    Morris Dovey
    West Des Moines, Iowa USA
    C links at http://www.iedu.com/c
    Morris Dovey, Jul 3, 2003
    #6
  7. Kevin Easton

    Jun Woong Guest

    "Mauro Persano" <> wrote in message news:...
    [...]
    > The answer to my other question was also on the standard -
    > section 6.5.2, `Constraints'. Trying to actually *implement*
    > that is another story, as Chris Torek pointed out. Typedefs
    > seem to make C syntax painfully context-sensitive. :-(


    Yes, it's context-sensitive in fact. There are even some cases where
    we can't assert the meaning of a construct, without knowing if the
    identifiers used in the construct are defined and act as typedef names
    or not.


    --
    Jun, Woong ()
    Dept. of Physics, Univ. of Seoul
    Jun Woong, Jul 3, 2003
    #7
    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. Alexander Stippler

    Re: visibility of typedefs

    Alexander Stippler, Jun 24, 2003, in forum: C++
    Replies:
    5
    Views:
    811
    tom_usenet
    Jun 25, 2003
  2. emerth
    Replies:
    3
    Views:
    368
    emerth
    Aug 8, 2003
  3. dwrayment

    Templates and Typedefs

    dwrayment, Aug 12, 2003, in forum: C++
    Replies:
    6
    Views:
    389
    Sam Holden
    Aug 14, 2003
  4. Dave
    Replies:
    4
    Views:
    754
    Andrey Tarasevich
    Dec 5, 2003
  5. Mauro Persano

    Re: Parsing with typedefs

    Mauro Persano, Jul 1, 2003, in forum: C Programming
    Replies:
    2
    Views:
    727
    Jun Woong
    Jul 2, 2003
Loading...

Share This Page