Re: Handing typedefs in yacc generated parsers

Dibyendu Majumdar <>
19 Jan 1999 00:56:48 -0500

          From comp.compilers

Related articles
Handing typedefs in yacc generated parsers (Dibyendu Majumdar) (1999-01-11)
Re: Handing typedefs in yacc generated parsers (1999-01-15)
Re: Handing typedefs in yacc generated parsers (Dibyendu Majumdar) (1999-01-19)
| List of all articles for this month |

From: Dibyendu Majumdar <>
Newsgroups: comp.compilers
Date: 19 Jan 1999 00:56:48 -0500
Organization: Compilers Central
References: 99-01-030 99-01-046
Keywords: types, C

Des Watson wrote:

> I had this problem when writing a C compiler some while ago. My
> approach was to move to the Roskind grammar (and, in fact, this proved
> not to be all that much effort). I initially tried adding context
> sensitivity to the lexer in a half-hearted way, and I never felt
> entirely happy with the results. But your algorithm looks somewhat
> more thoughtful than mine...!

Probably if I write a parser from scratch I too would use the
Roskind grammar.

> A useful program which may break some compilers is:
> #include <stdio.h>
> typedef char x;
> void foo() {
> int a=sizeof(x),x,b=sizeof(x);
> x=1;
> printf("a=%d, b=%d, x=%d\n",a,b,x);
> }
> void main() {
> foo();
> }

Thank you for this. It has highlighted a bug in my solution - my
algorithm didnot check for the situation where a variable declarator
appears after a COMMA - the lexer returned TYPEDEF_NAME in this
situation causing the parser to fail.

I have fixed this problem as follows:

In the parser, when I encounter a COMMA before an
init_declarator, I set a flag Expecting_declarator to TRUE.

The lexer checks this flag and if set returns IDENTIFIER
when it encounters a TYPEDEF_NAME.

If the flag Expecting_declarator is set, as soon as the parser
sees an IDENTIFIER- , it assumes that that is the direct_declarator,
and resets the flag to FALSE.

As far as I can see, this seems to have fixed
the bug (at least for now ...).

The Interpreter still does not give the correct result. The reason
for this is that the current implementation starts a new variable scope
only after the entire declaration is seen - i.e., the variable x is not
defined until after the ';' is consumed. This behaviour is not ISO C
- and therefore the second sizeof(x) yields sizeof(char).
However following gives correct results:

typedef char x;

void foo() {
    int a=sizeof(x), x=2;
    int b=sizeof(x);
    printf("a=%d, b=%d, x=%d\n",a,b,x);


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.