Handling Scope in a C Compiler

"Bart T." <bart@dynarec.com>
18 Oct 2002 23:45:18 -0400

          From comp.compilers

Related articles
Handling Scope in a C Compiler bart@dynarec.com (Bart T.) (2002-10-18)
Re: Handling Scope in a C Compiler tina.falkenberg@worldnet.att.net (Tina Falkenberg) (2002-10-20)
Re: Handling Scope in a C Compiler vbdis@aol.com (VBDis) (2002-10-25)
| List of all articles for this month |

From: "Bart T." <bart@dynarec.com>
Newsgroups: comp.compilers
Date: 18 Oct 2002 23:45:18 -0400
Organization: http://groups.google.com/
Keywords: C, symbols
Posted-Date: 18 Oct 2002 23:45:18 EDT


I'm writing a compiler for a subset of the C89 language (I'm not sure
how limited this subset will be just yet -- but I'll probably just
omit floating point support) and was wondering how variable scope
could be handled.

Holub ("Compiler Design in C") shows an interesting way of doing it
with the symbol manager by creating a new scope level each time a
block is entered and deleting all variables of that scope when the
block is done.

As far as I can tell, this method only works if the compiler is
working with 1 block at a time (parses 1 block, generates the code for
1 block, repeat.) Is there a better way of doing it which would allow
me to parse everything in 1 big phase, then pass control to the
optimizer and finally to the back end?

This is my first compiler project, so I still don't have a full and
clear picture of how everything will work. I was planning on
separating the compiler into 3 parts: front end, optimizer, and back
end. Each of these parts would do all of its work before moving on to
the next. Perhaps there would be a 4th part, an intermediate code
generator which would do its work right before the optimizer. The
front end would pass an AST to the I-code generator, the generator
would pass the intermediate code to the optimizer, etc.

Is this the approach I should be taking? How can scope be handled?

Thanks in advance. Sending copies of replies to bart@dynarec.com would
be very much welcome :)

Post a followup to this message

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