[C2hs] A problem parsing a C header

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Sun Dec 11 06:42:56 EST 2005


On Fri, 2005-12-09 at 11:23 +0000, Duncan Coutts wrote:
> On Wed, 2005-12-07 at 00:12 +0100, Alain Cremieux wrote:
> > Hi,
> > I have a problem with c2hs parsing a file in graphviz.
> > I have preprocessed it.
> > The large file chokes on line 1243 (I think)
> > The small one is archi reduced, but works on the same line.
> > Any hint ?
> 
> Ah ha! Found it.
> 
> Here's a minimal test case:
> 
> typedef struct path {
> } path;
> 
> typedef struct Agedgeinfo_t {
>  int path;
> } Agedgeinfo_t;
> 
> 
> It's because the path has already been defined as a type identifer and
> the parser is looking for a non-type identifer. However since it is
> obviously allowed to name a variable the same as a typedef'ed name then
> the parser should instead be looking for a non-type or type identifer.
> 
> However the change to the grammar to make this possible is non-trivial.

Gah! All the formulations of the C grammar that I've looked at (the C99
standard and several yacc implementations) have the same problem. The
only one that does it fully is the gcc yacc grammar (which is very
complex).

Either they gloss over the issue by not distinguishing typedef'ed
identifiers from ordinary identifiers (in which case they are ambiguous)
or the do distinguish but do not allow typedef'ed names in all the
necessary places, eg:

typedef foo bar;

enum bar { ... }

This is legal but ruled out by most of the grammars I've seen. Similarly
the problem we're having of typdef'ed names as structure members is not
addressed.

Duncan



More information about the C2hs mailing list