Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: Parsing with typedefs (http://www.velocityreviews.com/forums/t313948-re-parsing-with-typedefs.html)

Kevin Easton 07-02-2003 04:15 AM

Re: Parsing with typedefs
 
Mauro Persano <mpersano@lycos.com> 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.


Mauro Persano 07-02-2003 03:21 PM

Re: Parsing with typedefs
 
Hi,

Kevin Easton <kevin@-nospam-pcug.org.au> wrote in message news:<newscache$jdqdhh$kqk$1@tomato.pcug.org.au>.. .
> > 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

Kevin Easton 07-03-2003 03:37 AM

Re: Parsing with typedefs
 
Mauro Persano <mpersano@lycos.com> wrote:
> Hi,
>
> Kevin Easton <kevin@-nospam-pcug.org.au> wrote in message news:<newscache$jdqdhh$kqk$1@tomato.pcug.org.au>.. .
>> > 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.


Morris Dovey 07-03-2003 03:55 AM

Re: Parsing with typedefs
 
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


Kevin Easton 07-03-2003 04:17 AM

Re: Parsing with typedefs
 
Morris Dovey <mrdovey@iedu.com> 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.


Morris Dovey 07-03-2003 04:27 AM

Re: Parsing with typedefs
 
Kevin Easton wrote:
> Morris Dovey <mrdovey@iedu.com> 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


Jun Woong 07-03-2003 03:05 PM

Re: Parsing with typedefs
 

"Mauro Persano" <mpersano@lycos.com> wrote in message news:a3fcfd8c.0307030345.6f454da4@posting.google.c 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 (mycoboco@hanmail.net)
Dept. of Physics, Univ. of Seoul





All times are GMT. The time now is 06:03 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.