As Raphael says in his comment, this is a hierarchical scope organization. But this can qualified any kind of tree structured scope, and you state in the title that it is tree structured.
The whole purpose of this hierarchy is to allow reusing the same name in a different scope, for some other naming purpose. So, given a name, you have to decide where to lokk for its meaning, in what scope.
As you suggest, you start for the current scope (to be defined ...) and you buble up to parent scopes until you find a definition for that name (a declaration usually). The first one you find is usually supposed to be the right one.
But there is a major problem remaining: which is the parent scope of any given scope? There are two major policies regarding this: static scoping also called lexical scoping, and dynamic scoping.
Static scoping corresponds roughly to the way scopes are embedded according to the program text. Dynamic scoping corresponds to the way scopes are embedded according to program execution.
So, for example, take a function foo
that is declared in your program. If defines a new scope, with the variables local to foo
. If the function uses a variable x
that it does not declare, the meaning of the variable must be found in the parent scope. For dynamic scoping, the parent scope is that of the scoping structure (function, method, module, and whatever creates new scopes) that called the function foo
. For static scoping the parent scope is the scoping structure in which the function foo
was declared.
Static scoping is usually preferred because the meaning of a programming construct is independent of where it is actually used (possibly many different places), and depends only of where it is defined (only one place). Hence program analysis, understanding, and maintenance is usually much easier with static scoping.
The story goes that dynamic scoping was invented more or less by accident, as it was used in the first Lisp interpreter in the late 1950s. People were not yet aware of the difference at the time. When they found out (see the Funarg controversy), the language was being used and it was to late. Until Scheme came in as a statistically scoped version of Lisp.
But there can be other ways of organizing scopes, of importing one scope into another, and so on. I suggest that you read the wikipedia page on scope, which you should have done before asking.
scope hierarchy
or the like is not clear or definitive enough. $\endgroup$DOM
. other hierarchies we know of in CS arestack
,heap
,AR
/ARI
s, etc.... I am wondering what the name for the hierarchy that deals with scopes as the members in the organization. Simply saying 'I jump up one level of scope' doesn't seem clear enough given that there are different scoping structures. I want to say I moved up three levels on theX
. What isX
? $\endgroup$