Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Parsing with typedefs

Reply
Thread Tools

Re: Parsing with typedefs

 
 
Kevin Easton
Guest
Posts: n/a
 
      07-02-2003
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.

 
Reply With Quote
 
 
 
 
Mauro Persano
Guest
Posts: n/a
 
      07-02-2003
Hi,

Kevin Easton <kevin@-nospam-pcug.org.au> 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
 
Reply With Quote
 
 
 
 
Kevin Easton
Guest
Posts: n/a
 
      07-03-2003
Mauro Persano <> wrote:
> Hi,
>
> Kevin Easton <kevin@-nospam-pcug.org.au> 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.

 
Reply With Quote
 
Morris Dovey
Guest
Posts: n/a
 
      07-03-2003
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

 
Reply With Quote
 
Kevin Easton
Guest
Posts: n/a
 
      07-03-2003
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.

 
Reply With Quote
 
Morris Dovey
Guest
Posts: n/a
 
      07-03-2003
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

 
Reply With Quote
 
Jun Woong
Guest
Posts: n/a
 
      07-03-2003

"Mauro Persano" <> wrote in message news: om...
[...]
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using declarations with nested typedefs Dave C++ 4 12-05-2003 11:45 PM
Templates and Typedefs dwrayment C++ 6 08-14-2003 05:52 AM
STL typedefs and base class pointer problem emerth C++ 3 08-08-2003 05:47 AM
Re: Parsing with typedefs Mauro Persano C Programming 2 07-02-2003 03:41 PM
Re: visibility of typedefs Alexander Stippler C++ 5 06-25-2003 09:34 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57