Related articles |
---|
Maintaining scope while parsing C with a YACC grammar eliben@gmail.com (eliben) (2011-04-25) |
Re: Maintaining scope while parsing C with a YACC grammar bobduff@shell01.TheWorld.com (Robert A Duff) (2011-04-26) |
Re: Maintaining scope while parsing C with a YACC grammar bobduff@shell01.TheWorld.com (Robert A Duff) (2011-04-26) |
Re: Maintaining scope while parsing C with a YACC grammar eliben@gmail.com (eliben) (2011-04-28) |
Re: Maintaining scope while parsing C with a YACC grammar bobduff@shell01.TheWorld.com (Robert A Duff) (2011-05-02) |
Re: Maintaining scope while parsing C with a YACC grammar torbenm@diku.dk (2011-05-03) |
Re: Maintaining scope while parsing C with a YACC grammar paul@paulbmann.com (Paul B Mann) (2011-05-06) |
Re: Maintaining scope while parsing C with a YACC grammar idbaxter@semdesigns.com (Ira Baxter) (2011-05-13) |
Maintaining scope while parsing C with a Yacc grammar cfc@shell01.TheWorld.com (Chris F Clark) (2011-06-12) |
From: | Paul B Mann <paul@paulbmann.com> |
Newsgroups: | comp.compilers |
Date: | Fri, 6 May 2011 10:43:51 -0700 (PDT) |
Organization: | Compilers Central |
References: | 11-04-036 |
Keywords: | parse, yacc |
Posted-Date: | 10 May 2011 10:50:52 EDT |
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
Return to the
comp.compilers page.
Search the
comp.compilers archives again.