



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A part of programming language course notes, focusing on the concepts of scope and symbol tables. The differences between static and dynamic scoping, the role of symbol tables in keeping track of names and their bindings, and the handling of nested scopes. Dr. Amie souter's announcements include information about term projects, midterms, and homework assignments.
Typology: Assignments
1 / 7
This page cannot be seen from the preview
Don't miss anything!




4/18/2005 1
4/18/2005 3
4
int myattrex(int a, int b, int c){
int x, y, z;
z = a * b + c – d + e; … z = d; }
5
6
z The scope of a declaration is the region of the program to which the bindings established by the declaration apply. (If individual bindings apply over different regions: scope of a binding.) z Scope is typically indicated implicitly by the position of the declaration in the code, though keywords can modify it. z In a block-structured language, the scope is typically the code from the end of the declaration to the end of the "block" (indicated by braces {…} in C and Java) in which the declaration occurs. z Scope can extend backwards to the beginning of the block in certain cases (class declarations in Java and C++, top-level declarations in Scheme).
4/18/2005 7
int x; void p(int a){ int y; char z; z = y + z; { int m; z = m + z; } }
int main(){ int x; char z;
z = x * x; }
14
z Almost all languages use lexical scope: with dynamic scope the meaning of a variable cannot be known until execution time, thus there cannot be any static checking. z In particular, no static type checking. z Originally used in Lisp. Scheme could still use it, but doesn't. Some languages still use it: VBScript, Javascript, Perl (older versions). z Lisp inventor (McCarthy) now calls it a bug. z Still useful as a pedagogical tool to understand the workings of scope. In some ways a lot like dynamic binding of methods.
4/18/2005 15
4/18/2005 16
z A table of little stacks of declarations under each name (using lexical scope):
int local to x main
public static f void method
name bindings
main publicvoid method static
public static int
args (^) parameter of String[] main
public class Scope { public static int x = 2; public static void f() { System.out.println(x); } public static void main(String[] args) { int x = 3; f(); } }
18
int x; void p(int a){ int y; char z; z = y+x; { int y; z = y + 2; // what does the symbol table look like at //this point? } }
int main(){ int x; char z; p(); z = x * x; } (^19)
20
z A dictionary or table is used to maintain the identifier/attribute bindings.
z It can be maintained either during translation or execution or both. (Pre-translation entities are entered into the initial or default table.)
z During translation this table is called the symbol table.
z During execution this table is called the environment.
z If both are maintained, the environment can usually dispense with names, keeping track only of locations (names are maintained implicitly).
21
z Can be constructed entirely statically (Fortran): all vars and functions have fixed locations for the duration of execution. z Can also be entirely dynamic: functional languages like Scheme and ML. z Most language use a mix: C, C++, Java, Ada. z Consists of three components:
26
© 2003 Brooks/Cole - Thomson Learning™ 30
z The lifetime or extent of a program entity is the duration of its allocation in the environment. z Allocation is static when the lifetime is the duration of the entire program execution.
z Lifetime is related to but not identical to scope. With scope holes, lifetime can extend to regions of the program where the program entity is not accessible. z It is also possible for scope to exceed lifetime when a language allows locations to be manipulated directly (as for example manual deallocation). This is of course very dangerous!
31
32
z An alias occurs when the same object is bound to two different names at the same time. This is fairly common with Java objects. z A dangling reference is a location that has been deallocated from the environment, but is still accessible within the program. Dangling references are impossible in a garbage-collected environment with no direct access to addresses. z Garbage is memory that is still allocated in the environment but has become inaccessible to the program. Garbage can be a problem in a non- garbage collected environment, but is much less serious than dangling references.