The classic reference is "the Dragon Book", Aho Sethi and Ullman, Compiler Design, but there are many others. Louden Compiler Construction is a little easier to read. I have not found much comprehensive material in the Internet. A good book is the best way to go.
On Jul 14, 12:13 am, Anand <[email protected]> wrote: > @ Gene: Thanks for the nice explanation. Any pointers on where we can find > such detailed explanation about compilation/compiler. > > > > On Tue, Jul 13, 2010 at 6:25 PM, Ashish Goel <[email protected]> wrote: > > the highest scope may not be true, it uses the current scope to chk if the > > variable is defined there > > any pointer to study material? > > > Best Regards > > Ashish Goel > > "Think positive and find fuel in failure" > > +919985813081 > > +919966006652 > > > On Wed, Jul 14, 2010 at 6:28 AM, Gene <[email protected]> wrote: > > >> On Jul 8, 1:34 pm, gopinath sikha <[email protected]> wrote: > >> > Hi guys, > > >> > How gcc or any c compiler maintains the scope of the variable? > >> > Especially all the static variable are maintained in the data section. > > >> > Can anyone please respond. > > >> As a compiler processes the program, it maintains a table that maps > >> identifier strings to associated attributes like type information, > >> line number of declaration, etc., etc. This is called the symbol > >> table. It's often implemented as a hash table. Self-balancing trees > >> work fine, too. > > >> There are at least a couple of different ways to process scopes. Here > >> is one. > > >> The symbol table must allow duplicate keys for this technique, so it's > >> a multi-map rather than map. > > >> The compiler gives every scope a number. It does this by keeping a > >> counter initialized at 0. Every time it sees a {, it increments the > >> counter and the new value becomes the scope number. It also pushes > >> each new scope number on a stack. At any given time, the stack > >> contains the numbers of open scopes, with the innermost scope at the > >> top. > > >> When the compiler processes a declaration, the declared name is added > >> to the symbol table, and the scope number becomes one of its > >> attributes. If an attempt is made to add a variable twice with the > >> same scope number, a re-declaration error is printed for the user. > > >> When the compiler processes a variable use (as in x = 42), it looks up > >> the variable (x) in the symbol table. If there is more than a single > >> x, it uses the one with the highest scope number because this must be > >> the "inner" scope where there is an x. If the variable (x) is not > >> found at all, then an "undefined identifier" error is printed for the > >> user. > > >> When the compiler processes the end of a scope, a }, it pops the scope > >> stack. Say the closing scope is number N. Then the compiler removes > >> all symbol table entries with scope N as attributes and keeps on > >> processing. > > >> That's about it. There are some details like handling the id's for > >> fields within structs, but I'm sure you can figure out how that works > >> on your own. I.e. when you have struct point { float x, y; } x; x.x > >> = 42; think about what the compiler is doing when it processes the > >> { in the strut declaration, when it sees the x and the y, when it sees > >> the second x and the third. > > >> Cheers. > > >> -- > >> You received this message because you are subscribed to the Google Groups > >> "Algorithm Geeks" group. > >> To post to this group, send email to [email protected]. > >> To unsubscribe from this group, send email to > >> [email protected]<algogeeks%2bunsubscr...@googlegroups > >> .com> > >> . > >> For more options, visit this group at > >>http://groups.google.com/group/algogeeks?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Algorithm Geeks" group. > > To post to this group, send email to [email protected]. > > To unsubscribe from this group, send email to > > [email protected]<algogeeks%2bunsubscr...@googlegroups > > .com> > > . > > For more options, visit this group at > >http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
