Re: How is the concept of scope implemented?

Ray Dillinger <bear@sonic.net>
Fri, 14 Nov 2008 09:03:01 -0800

          From comp.compilers

Related articles
How is the concept of scope implemented? tony@my.net (Tony) (2008-11-14)
Re: How is the concept of scope implemented? bear@sonic.net (Ray Dillinger) (2008-11-14)
Re: How is the concept of scope implemented? m.helvensteijn@gmail.com (2008-11-14)
Re: How is the concept of scope implemented? tony@my.net (Tony) (2008-11-14)
Re: How is the concept of scope implemented? liangkun1983@gmail.com (Alex L.K) (2008-11-15)
Re: How is the concept of scope implemented? lkrupp@pssw.com (Louis Krupp) (2008-11-15)
Re: How is the concept of scope implemented? tony@my.net (Tony) (2008-11-18)
Re: How is the concept of scope implemented? tony@my.net (Tony) (2008-11-18)
[9 later articles]
| List of all articles for this month |

From: Ray Dillinger <bear@sonic.net>
Newsgroups: comp.compilers
Date: Fri, 14 Nov 2008 09:03:01 -0800
Organization: Organized? Seems unlikely.
References: 08-11-054
Keywords: symbols, C++
Posted-Date: 15 Nov 2008 06:14:39 EST

Tony wrote:


> In C++, there is many kinds of scope: global, translation unit, function,
> local (between the curly brackets within a function), class and probably
> more. How is the concept of scope implemented by a compiler for a program?


Scope is a set of rules about how variable references are resolved. When
implementing your symbol table management, you have to encode rules about
where to look to get values of variable names. Those rules are your scope
rules.


A given name in your code may refer to many different variables. When
your compiler finds the variable name, it has to map it to the memory
address of the correct variable.


For example, you have a variable name "foo" in a function, and your
symbol table says that name refers to an automatic variable in the
current invocation of that function, to a file variable, and to a
global variable. You need to resolve the conflict using scope rules -
and in this case, in C or C++ your compiler would map the name to
the automatic variable.


In most languages, scope rules require the variable name, the function
name, and a unique identifier for the activation frame. In C and C++,
however (and a few other languages) you also need the code address of
the reference, because the baroque scoping rules include variables whose
scope is a smaller unit than a full function.


Hope that helps.


                                                                Bear



Post a followup to this message

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