Groups | Search | Server Info | Login | Register


Groups > comp.compilers > #119

Re: Maintaining scope while parsing C with a YACC grammar

From Paul B Mann <paul@paulbmann.com>
Newsgroups comp.compilers
Subject Re: Maintaining scope while parsing C with a YACC grammar
Date 2011-05-06 10:43 -0700
Organization Compilers Central
Message-ID <11-05-009@comp.compilers> (permalink)
References <11-04-036@comp.compilers>

Show all headers | View raw


There are two independent topics being discussed here.

(1) Scope of variables.
(2) typedef variables.

(1) Scope of variables is solved easily.  In your symbol table you
have to keep track of the level for all variables.  Every time you
see a '{' you have to increment the level.  So the 'a' will be put
into the symbol table as a level 1 variable and the 'b' will be a
level 2.  The bottom-up quality of LALR will not affect this.  The
'a' will be seen first and the 'b' later, so no problem here.

(2) The infamous 'typedef' problem continues to plague the newbie or
part-time LALR grammar hacker, but it was solved way back in 1987 by
an LALR parser generator which used an integrated symbol table and
semantic grammar symbols (e.i. {typedef}).  The state-of-the-art
simple solution works fine with the LRSTAR parser generator (see
http://highperware.com). Here is the simple LALR(1) grammar which
solves this 'typedef' problem:

/* C Typedef Solution. */

   <error>          => error();
   <identifier>    => lookup();  /* Symbol table lookup. */

/* Rules. */

   Input -> [Declaration]... <eof>                +> input_

   Declaration
       ->         VarDecl  [',' Var ]... ';'    +> decl_
       -> typedef VarDecl2 [',' Var2]... ';'    +> typedefdecl_

   VarDecl  -> Type...  Ident

   Var      -> [Ptr]... Ident

   VarDecl2 -> Type...  Ident2

   Var2     -> [Ptr]... Ident2

   Ident    -> <identifier>	          +> ident_(1)

   Ident2   -> <identifier>             +> ident_(1,{typedef})

   Type
          -> SimpleType	                +> type_(1)
          -> Type Ptr

   Ptr    -> '*'                        +> ptr_

   SimpleType
          -> char
          -> int
          -> short
          -> unsigned
          -> {typedef}


It handles the input file:

typedef unsigned int UINT, * UINTPTR;
UNIT a, b, c;
UINTPTR x, y, z;

Note, no hacks or kludges are required, just a state-of-the-art parser
generator.

Paul B Mann

Back to comp.compilers | Previous | NextPrevious in thread | Find similar


Thread

Maintaining scope while parsing C with a YACC grammar eliben <eliben@gmail.com> - 2011-04-25 05:14 -0700
  Re: Maintaining scope while parsing C with a YACC grammar Robert A Duff <bobduff@shell01.TheWorld.com> - 2011-04-26 12:22 -0400
    Re: Maintaining scope while parsing C with a YACC grammar Robert A Duff <bobduff@shell01.TheWorld.com> - 2011-04-26 14:08 -0400
    Re: Maintaining scope while parsing C with a YACC grammar eliben <eliben@gmail.com> - 2011-04-28 23:20 -0700
      Re: Maintaining scope while parsing C with a YACC grammar Robert A Duff <bobduff@shell01.TheWorld.com> - 2011-05-02 20:19 -0400
        Re: Maintaining scope while parsing C with a YACC grammar "Ira Baxter" <idbaxter@semdesigns.com> - 2011-05-13 17:46 -0500
        Maintaining scope while parsing C with a Yacc grammar Chris F Clark <cfc@shell01.TheWorld.com> - 2011-06-12 20:43 -0400
      Re: Maintaining scope while parsing C with a YACC grammar torbenm@diku.dk (Torben Ægidius Mogensen) - 2011-05-03 09:51 +0200
  Re: Maintaining scope while parsing C with a YACC grammar Paul B Mann <paul@paulbmann.com> - 2011-05-06 10:43 -0700

csiph-web