parsing typedef names 
Author Message
 parsing typedef names

This is silly, but I can't figure out why the following code provokes
a parse error.  Note that I know it's illegal, and I don't actually
want to do this (it was more or less a mistake that I ran across it in
the first place), but now I'm curious.

typedef int foo;
unsigned foo bar;

As far as I can see (working from the grammar in c99, page 410), the
second line should parse as follows:

declaration:
    declaration-specifiers init-declarator-list ;

declaration-specifiers:
    type-specifier declaration-specifiers2

type-specifier:
    unsigned

declaration-specifiers2:
    type-specifier2

type-specifier2:
    typedef-name

typedef-name:
    identifier

identifier:
    foo

init-declarator-list:
    init-declarator

init-declarator:
    declarator

declarator:
    direct-declarator

direct-declarator:
    identifier

identifier:
    bar

But three compilers I've tried have all choked with a parse error.

Is this perhaps a manifestation of the greediness of the C parser?
(similar to how "a+++++b" doesn't parse to "a++ + ++b" ?)

-andy
--



Tue, 08 Jul 2003 10:49:39 GMT  
 parsing typedef names

Quote:

> This is silly, but I can't figure out why the following code provokes
> a parse error.  Note that I know it's illegal, and I don't actually
> want to do this (it was more or less a mistake that I ran across it in
> the first place), but now I'm curious.

> typedef int foo;
> unsigned foo bar;

this should work if you use #define in place of #typedef.  But foo
is now a type in its own right, and the conpiler has resolved its
range, size, etc.  Too late to change it.

I didn't work thru your derivation, but if there isn't something
there there should be IMHO.  Grammars omit semantics.

Quote:
> As far as I can see (working from the grammar in c99, page 410), the
> second line should parse as follows:

> declaration:
>     declaration-specifiers init-declarator-list ;

> declaration-specifiers:
>     type-specifier declaration-specifiers2

> type-specifier:
>     unsigned

> declaration-specifiers2:
>     type-specifier2

> type-specifier2:
>     typedef-name

> typedef-name:
>     identifier

> identifier:
>     foo

> init-declarator-list:
>     init-declarator

> init-declarator:
>     declarator

> declarator:
>     direct-declarator

> direct-declarator:
>     identifier

> identifier:
>     bar

> But three compilers I've tried have all choked with a parse error.

> Is this perhaps a manifestation of the greediness of the C parser?
> (similar to how "a+++++b" doesn't parse to "a++ + ++b" ?)

> -andy
> --


--

http://www.qwikpages.com/backstreets/cbfalconer
   (Remove "NOSPAM." from reply address. my-deja works unmodified)

--



Thu, 10 Jul 2003 13:22:09 GMT  
 parsing typedef names
Quote:

> This is silly, but I can't figure out why the following code provokes
> a parse error.  Note that I know it's illegal, and I don't actually
> want to do this (it was more or less a mistake that I ran across it in
> the first place), but now I'm curious.

> typedef int foo;
> unsigned foo bar;

> As far as I can see (working from the grammar in c99, page 410), the
> second line should parse as follows:

[SNIP - parse omitted]

Quote:

> But three compilers I've tried have all choked with a parse error.

> Is this perhaps a manifestation of the greediness of the C parser?
> (similar to how "a+++++b" doesn't parse to "a++ + ++b" ?)

> -andy
> --


This isn't a parse error. Did the compilers actually emit the diagnostic
``Parse error'' or did they say ``syntax error''? There is a difference.
This is a syntax error since it violates the syntactical rules of the
language. I compiled the snippet under gcc 2.95.2 and it correctly emits
the diagnostic: `foo.c:2: syntax error before `bar'''.

If they did emit a parse error, then chances are that they've rolled
some of the syntax into the grammar specification or they are checking
for stuff like this during the parse.

------------------------------------------------------------------------
Dave Shawley                                  Embedded Software Engineer

"More computing sins are committed in the name of efficiency (without
 necessarily achieving it) that for any other single reason -- including
 blind stupidity." -- W.A. Wulf
------------------------------------------------------------------------
--
--



Thu, 10 Jul 2003 13:22:48 GMT  
 parsing typedef names

Quote:

> This is silly, but I can't figure out why the following code provokes
> a parse error.  Note that I know it's illegal, and I don't actually
> want to do this (it was more or less a mistake that I ran across it in
> the first place), but now I'm curious.

> typedef int foo;
> unsigned foo bar;

> As far as I can see (working from the grammar in c99, page 410), the
> second line should parse as follows:

[snip]

The restriction is not in the grammar, but rather in a constraint.  See
Section 6.7.2, pg. 99.

--
Eric Amick
Columbia, MD

--



Thu, 10 Jul 2003 13:23:13 GMT  
 parsing typedef names
Andy Isaacson a crit dans le message ...

Quote:
>This is silly, but I can't figure out why the following code provokes
>a parse error.  Note that I know it's illegal, and I don't actually
>want to do this (it was more or less a mistake that I ran across it in
>the first place), but now I'm curious.

>typedef int foo;
>unsigned foo bar;

qualifiers only work with basic types.

typedef int foo;
foo bar;

or

typedef unsigned int ufoo;
ufoo bar;

--
-hs-    Tabs out, spaces in.
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc
--



Fri, 11 Jul 2003 01:30:38 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Parsing Typedef Names

2. When is a typedef name not a typedef name?

3. When is a typedef name not a typedef name?

4. typedef struct tagNAME {} NAME; vs. typedef struct NAME {};

5. typedef name/argument [Is this legal C?]

6. typedef struct on several NAMES

7. same name for structure, member, typedef

8. typedef and #define with same name - OK?

9. Typedef names in ANSI-C

10. typedef-name redefinition (ANSI C parser)

11. Parsing Function names out of a configuration file

12. problems parsing typedefed names

 

 
Powered by phpBB® Forum Software