Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University.

Slides:



Advertisements
Similar presentations
Semantic Analysis and Symbol Tables
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms
1 Compiler Construction Intermediate Code Generation.
Compiler Construction
ISBN Chapter 10 Implementing Subprograms.
Names, Bindings, Type Checking, and Scopes
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Chapter 9: Subprogram Control
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Scope.
Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Topics Scope Scope and Lifetime Referencing Environments.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
Implementing Subprograms
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Lecture 9 Symbol Table and Attributed Grammars
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
Implementing Subprograms
Context-Sensitive Analysis
Type Checking, and Scopes
CS 326 Programming Languages, Concepts and Implementation
Semantic Analysis with Emphasis on Name Analysis
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Implementing Subprograms
UNIT V Run Time Environments.
Names and Binding In Text: Chapter 5.
Compiler Construction
COMPILERS Semantic Analysis
Implementing Subprograms
Compiler Construction
Implementing Subprograms
Presentation transcript:

Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University

CS 540 Spring 2009 GMU2 Static Analysis Compilers examine code to find semantic problems. –Easy: undeclared variables, tag matching –Difficult: preventing execution errors Essential Issues: –Part I: Type checking –Part II: Scope –Part III: Symbol tables

Part I: Type Checking

CS 540 Spring 2009 GMU4 Type Systems A type is a set of values and associated operations. A type system is a collection of rules for assigning type expressions to various parts of the program. –Impose constraints that help enforce correctness. –Provide a high-level interface for commonly used constructs (for example, arrays, records). –Make it possible to tailor computations to the type, increasing efficiency (for example, integer vs. real arithmetic). –Different languages have different type systems.

CS 540 Spring 2009 GMU5 Inference Rules - Typechecking Static (compile time) and Dynamic (runtime). One responsibility of a compiler is to see that all symbols are used correctly (i.e. consistently with the type system) to prevent execution errors. Strong typing – All expressions are guaranteed to be type consistent although the type itself is not always known (may require additional runtime checking).

CS 540 Spring 2009 GMU6 What are Execution Errors? Trapped errors – errors that cause a computation to stop immediately –Division by 0 –Accessing illegal address Untrapped errors – errors that can go unnoticed for a while and then cause arbitrary behavior –Improperly using legal address (moving past end of array) –Jumping to wrong address (jumping to data location) A program fragment is safe if it does not cause untrapped errors to occur.

CS 540 Spring 2009 GMU7 Typechecking Input: x * y + f(a,b) We need to be able to assign types to all expressions in a program and show that they are all being used correctly. E E + E E * E x y f(a,b) Are x, y and f declared? Can x and y be multiplied together? What is the return type of function f? Does f have the right number and type of parameters? Can f’s return type be added to something?

CS 540 Spring 2009 GMU8 Program Symbols User defines symbols with associated meanings. Must keep information around about these symbols: –Is the symbol declared? –Is the symbol visible at this point? –Is the symbol used correctly with respect to its declaration?

CS 540 Spring 2009 GMU9 Using Syntax Directed Translation to process symbols While parsing input program, need to: Process declarations for given symbols –Scope – what are the visible symbols in the current scope? –Type – what is the declared type of the symbol? Lookup symbols used in program to find current binding Determine the type of the expressions in the program

CS 540 Spring 2009 GMU10 Syntax Directed Type Checking Consider the following simple language P  D S D  id: T ; D |  T  integer | float | array [ num ] of T | ^T S  S ; S | id := E E  int_literal | float_literal | id | E + E | E [ E ] | E^ How can we typecheck strings in this language?

CS 540 Spring 2009 GMU11 Example of language: i: integer; j: integer i := i + 1; j := i + 1 P D ; S D ; D S ; S id : T integer id := E E + E idnum id := E E + E idnum

CS 540 Spring 2009 GMU12 Processing Declarations D  id : T ; D {insert(id.name,T.type);} D   T  integer {T.type = integer;} T  float{T.type = float;} T  array [ num ] of T 1 {T.type = array(T 1.type,num); } T  ^T 1 {T.type = pointer(T 1.type);} Put info into the symbol table Accumulate information about the declared type

CS 540 Spring 2009 GMU13 Can use Trees (or DAGs) to Represent Types array[25] of array[10] of ^(integer) array[100] of ^(float) array pointer integer pointer float array 100 Build data structures while we parse array 10 25

CS 540 Spring 2009 GMU14 Example I: integer; A: array[20] of integer; B: array[20] of ^integer; I := B[A[2]]^ P D S id : T ; D I integer … Parse Tree integer I

CS 540 Spring 2009 GMU15 Example I: integer; A: array[20] of integer; B: array[20] of ^integer; I := B[A[2]]^ Parse Tree integer array 20 IAIA P D S id : T ; D I integer … id : T ; D A array[20] of T integer

CS 540 Spring 2009 GMU16 Example I: integer; A: array[20] of integer; B: array[20] of ^integer; I := B[A[2]]^ Parse Tree integer pointer array 20 IABIAB P D S id : T ; D I integer … id : T ; D A array[20] of T integer id : T ; D B array[20] of T ^ T  integer

CS 540 Spring 2009 GMU17 Typechecking Expressions E  int_literal { E.type := integer; } E  float_literal { E.type = float; } E  id { E.type := lookup(id.name); } E  E 1 + E 2 { if (E 1.type = integer & E 2.type = integer) then E.type = integer; else if (E 1.type = float & E 2.type = float) then E.type = float; else type_error(); } E  E 1 [ E 2 ] { if (E 1.type = array of T & E 2.type = integer) then E.type = T; else …} E  E 1 ^ { if (E 1.type = ^T) then E.type = T; else …} These rules define a type system for the language

CS 540 Spring 2009 GMU18 Example I: integer; A: array[20] of integer; B: array[20] of ^integer; I := B[A[2]]^ P D S id := E E ^ E [ E ] idnum E [ E ]id integer pointer array 20 IABIAB

CS 540 Spring 2009 GMU19 Example I: integer; A: array[20] of integer; B: array[20] of ^integer; I := B[A[2]]^ P D ; S E [ E ] idnum E [ E ]id integer pointer array 20 IABIAB I B A id := E E ^

CS 540 Spring 2009 GMU20 Example I: integer; A: array[20] of integer; B: array[20] of ^integer; I := A[B[2]]^ P D ; S E [ E ] idnum E [ E ]id integer pointer array 20 IABIAB I A B id := E E ^ Type error!

CS 540 Spring 2009 GMU21 Typechecking Statements S  S 1 ; S 1 {if S 1.type = void & S 1.type = void) then S.type = void; else error(); } S  id := E { if lookup(id.name) = E.type then S.type = void; else error(); } S  if E then S 1 { if E.type = boolean and S 1.type = void then S.type = void; else error();} In this case, we assume that statements do not have types (not always the case).

CS 540 Spring 2009 GMU22 Typechecking Statements What if statements have types? S  S 1 ; S 2 {S.type = S 2.type;} S  id := E { if lookup(id.name) = E.type then S.type = E.type; else error(); } S  if E then S 1 else S 2 { if (E.type = boolean & S 1.type = S 2.type) then S.type = S 1.type; else error(); }

CS 540 Spring 2009 GMU23 Untyped languages Single type that contains all values Ex: Lisp – program and data interchangeable Assembly languages – bit strings Checking typically done at runtime

CS 540 Spring 2009 GMU24 Typed languages Variables have nontrivial types which limit the values that can be held. In most typed languages, new types can be defined using type operators. Much of the checking can be done at compile time! Different languages make different assumptions about type semantics.

CS 540 Spring 2009 GMU25 Components of a Type System Base Types Compound/Constructed Types Type Equivalence Inference Rules (Typechecking) … Different languages make different choices!

CS 540 Spring 2009 GMU26 Base (built-in) types Numbers –Multiple – integer, floating point –precision Characters Booleans

CS 540 Spring 2009 GMU27 Constructed Types Array String Enumerated types Record Pointer Classes (OO) and inheritance relationships Procedure/Functions …

CS 540 Spring 2009 GMU28 Type Equivalence Two types: Structural and Name Type A = Bool Type B = Bool In Structural equilivance: Types A and B match because they are both boolean. In Name equilivance: A and B don’t match because they have different names.

CS 540 Spring 2009 GMU29 Implementing Structural Equivalence To determine whether two types are structurally equilivant, traverse the types: boolean equiv(s,t) { if s and t are same basic type return true if s = array(s1,s2) and t is array(t1,t2) return equiv(s1,t1) & equiv(s2,t2) if s = pointer(s1) and t = pointer(t1) return equiv(s1,t1) … return false; }

CS 540 Spring 2009 GMU30 Other Practical Type System Issues Implicit versus explicit type conversions –Explicit  user indicates (Ada) –Implicit  built-in (C int/char) -- coercions Overloading – meaning based on context –Built-in –Extracting meaning – parameters/context Objects (inheritance) Polymorphism

CS 540 Spring 2009 GMU31 OO Languages Data is organized into classes and sub-classes Top level is class of all objects Objects at any level inherit the attributes (data, functions) of objects higher up in the hierarchy. The subclass has a larger set of properties than the class. Subclasses can override behavior inherited from parent classes. (But cannot revise private data elements from a parent).

CS 540 Spring 2009 GMU32 class A { public: A() {cout << "Creating A\n"; } W() {cout << "W in A\n"; } }; class B: public A { public: B() {cout << "Creating B\n"; } S() {cout << "S in B\n"; } }; class C: public A { public: C() {cout << "Creating C\n"; } Y() {cout << "Y in C\n"; } }; class D: public C { public: D() {cout << "Creating D\n"; } S() {cout << "S in D\n"; } }; Object A (W) B (S) C (Y) D (S)

CS 540 Spring 2009 GMU33 The Code: Output: B b; Creating A Creating B D d; Creating A Creating C Creating D b.W(); W in A b.S(); S in B d.W(); W in A d.Y(); Y in C d.S(); S in D Object A (W) B (S) C (Y) D (S)

CS 540 Spring 2009 GMU34 OO Principle of Substitutability Subclasses possess all data areas associated with parent classes Subclasses implement (through inheritance) at least all functionality defined for the parent class If we have two classes, A and B, such that class B is a subclass of A (perhaps several times removed), it should be possible to substitute instances of class B for instances of class A in any situation with no observable effect.

CS 540 Spring 2009 GMU35 Typechecking OO languages Without inheritance, the task would be relatively simple (similar to records) Difficulties: –Method overriding –When can super/sub types be used? Consider function f: A  B Actual parameter of type A or subtype of A Return type B or supertype of B –Multiple inheritance

CS 540 Spring 2009 GMU36 Function parameters Function parameters make typechecking more difficult procedure mlist(lptr: link; procedure p) while lptr <> nil begin p(lptr); lptr = lptr  next; end

CS 540 Spring 2009 GMU37 Polymorphism Functions – statements in body can be executed on arguments with different type – common in OO languages because of inheritance Ex: Python for determining the length of a list def size (lis): if null(lis): return 0 else: return size(lis[1:]) + 1; size([‘sun’,’mon’,’tue’]) size([10,11,12]) size(A)

CS 540 Spring 2009 GMU38 Type Inferencing def size (lis): if null(lis): return 0 else: return size(lis[1:])+1; Goal: determine a type for size so we can typecheck the calls. Greek symbols are type variables. Expression Type size      lis  Fig 6.30 of your text

CS 540 Spring 2009 GMU39 Type Inferencing def size (lis): if null(lis): return 0 else: return size(lis[1:])+1; Built-in language constructs and functions provide clues. Given what we have in the table, we now know that list(  n ) =  Expression Type size      lis  list(  n ) ) if bool x  i x  i   i null list(  n )  bool null(lis) bool Fig 6.30 of your text

CS 540 Spring 2009 GMU40 Type Inferencing def size (lis): if null(lis): return 0 else: return size(lis[1:])+1;  i = int Expression Type size      lis  list(  n ) ) if bool x  i x  i   i null list(  n )  bool null(lis) bool 0 int + int x int  int lis[1:] list(  n ) Fig 6.30 of your text

CS 540 Spring 2009 GMU41 Type Inferencing def size (lis): if null(lis): return 0 else: return size(lis[1:])+1;  int All of this tells us that size : list(  )  int (in other words, maps from anything with type list to type integer) Expression Type size      lis  list(  n ) ) if bool x  i x  i   i null list(  n )  bool null(lis) bool 0,1 int + int x int  int lis[1:] list(  n ) size(lis[1:])  size(lis[1:]) + 1 int if(…) int Fig 6.30 of your text

CS 540 Spring 2009 GMU42 Formalizing Type Systems Mathematical characterizations of the type system – Type soundness theorems. Requires formalization of language syntax, static scoping rules and semantics. Formalization of type rules

Part II: Scope

CS 540 Spring 2009 GMU44 Scope In most languages, a complete program will contain several different namespaces or scopes. Different languages have different rules for namespace definition

CS 540 Spring 2009 GMU45 Fortran 77 Name Space f1() variables parameters labels f2() variables parameters labels f3() variables parameters labels common block a common block b Global Global scope holds procedure names and common block names. Procedures have local variables parameters, labels and can import common blocks

CS 540 Spring 2009 GMU46 Scheme Name Space All objects (built-in and user-defined) reside in single global namespace ‘let’ expressions create nested lexical scopes Global map 2 cons var f1() f2() let

CS 540 Spring 2009 GMU47 C Name Space Global scope holds variables and functions No function nesting Block level scope introduces variables and labels File level scope with static variables that are not visible outside the file (global otherwise) Global a,b,c,d,... File scope static names x,y,z File scope static names w,x,y f1() f2() f3() variables parameters labels variables variables, param Block Scope variables labels Block scope Block scope

CS 540 Spring 2009 GMU48 Java Name Space Limited global name space with only public classes Fields and methods in a public class can be public  visible to classes in other packages Fields and methods in a class are visible to all classes in the same package unless declared private Class variables visible to all objects of the same class. Public Classes package p1 package p2 package p3 public class c1 class c2 fields: f1,f2 method: m1 locals method: m2 locals fields: f3 method: m3

CS 540 Spring 2009 GMU49 Scope Each scope maps a set of variables to a set of meanings. The scope of a variable declaration is the part of the program where that variable is visible.

CS 540 Spring 2009 GMU50 Referencing Environment The referencing environment at a particular location in source code is the set of variables that are visible at that point. A variable is local to a procedure if the declaration occurs in that procedure. A variable is non-local to a procedure if it is visible inside the procedure but is not declared inside that procedure. A variable is global if it occurs in the outermost scope (special case of non-local).

CS 540 Spring 2009 GMU51 Types of Scoping Static – scope of a variable determined from the source code. –“Most Closely Nested” –Used by most languages Dynamic – current call tree determines the relevant declaration of a variable use.

CS 540 Spring 2009 GMU52 Static: Most Closely Nested Rule The scope of a particular declaration is given by the most closely nested rule The scope of a variable declared in block B, includes B. If x is not declared in block B, then an occurrence of x in B is in the scope of a declaration of x in some enclosing block A, such that A has a declaration of x and A is more closely nested around B than any other block with a declaration of x.

CS 540 Spring 2009 GMU53 Example Program: Static Program main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1 body of main What is visible at this point (globally)?

CS 540 Spring 2009 GMU54 Example Program: Static Program main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1 body of main What is visible at this point (sub1)?

CS 540 Spring 2009 GMU55 Example Program: Static Program main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1 body of main What is visible at this point (sub3)?

CS 540 Spring 2009 GMU56 Example Program: Static Program main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1 body of main What is visible at this point (sub2)?

CS 540 Spring 2009 GMU57 Dynamic Scope Based on calling sequences of program units, not their textual layout (temporal versus spatial) References to variables are connected to declarations by searching the chain of subprogram calls (runtime stack) that forced execution to this point

CS 540 Spring 2009 GMU58 Scope Example MAIN - declaration of x SUB1 - declaration of x -... call SUB2... SUB reference to x -... call SUB1 … MAIN calls SUB1 SUB1 calls SUB2 SUB2 uses x Which x??

CS 540 Spring 2009 GMU59 Scope Example MAIN - declaration of x SUB1 - declaration of x -... call SUB2... SUB reference to x -... call SUB1 … MAIN calls SUB1 SUB1 calls SUB2 SUB2 uses x For static scoping, it is main’s x

CS 540 Spring 2009 GMU60 Scope Example In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms. A subprogram is active if its execution has begun but has not yet terminated.

CS 540 Spring 2009 GMU61 Scope Example MAIN - declaration of x SUB1 - declaration of x -... call SUB2... SUB reference to x -... call SUB1 … MAIN calls SUB1 SUB1 calls SUB2 SUB2 uses x For dynamic scoping, it is sub1’s x MAIN (x) SUB1 (x) SUB2

CS 540 Spring 2009 GMU62 Dynamic Scoping Evaluation of Dynamic Scoping: –Advantage: convenience (easy to implement) –Disadvantage: poor readability, unbounded search time

Part III: Symbol Tables

CS 540 Spring 2009 GMU64 Symbol Table Primary data structure inside a compiler. Stores information about the symbols in the input program including: –Type (or class) –Size (if not implied by type) –Scope Scope represented explicitly or implicitly (based on table structure). Classes can also be represented by structure – one difference = information about classes must persist after have left scope. Used in all phases of the compiler.

CS 540 Spring 2009 GMU65 Symbol Table Object Symbol table functions are called during parsing: Insert(x) –A new symbol is defined. Delete(x) –The lifetime of a symbol ends. Lookup(x) –A symbol is used. EnterScope(s) – A new scope is entered. ExitScope(s) – A scope is left.

CS 540 Spring 2009 GMU66 Scope and Parsing func_decl : FUNCTION NAME {EnterScope($2);} parameter decls stmts ; {ExitScope($2); } decl : name ‘:’ type {Insert($1,$3); } … statements:id := expression {lookup($1);} … expression: … id {lookup($1);} Note: This is a greatly simplified grammar including only the symbol table relevant productions.

CS 540 Spring 2009 GMU67 Symbol Table Implementation Variety of choices, including arrays, lists, trees, heaps, hash tables, … Different structures may be used for local tables versus tables representing scope.

CS 540 Spring 2009 GMU68 Example Implementation Local level – within a scope, use a table or linked list. Global – each scope is represented as a structure that points at – –Its local symbols –The scopes that it encloses –Its enclosing scope } A tree?

CS 540 Spring 2009 GMU69 Implementing the table Need variable CS for current scope EnterScope – creates a new record that is a child of the current scope. This scope has new empty local table. Set CS to this record. ExitScope – set CS to parent of current scope. Update tables. Insert – add a new entry to the local table of CS Lookup – Search local table of CS. If not found, check the enclosing scope. Continue checking enclosing scopes until found or until run out of scopes.

CS 540 Spring 2009 GMU70 Example Program Program main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1 body of main Main sub1 sub2sub3 a,b,c, sub1 a,d, sub2, sub3 c,d a

CS 540 Spring 2009 GMU71 Implementing the table We can use a stack instead!!! EnterScope – creates a new record that is a child of the current scope. This scope has new empty local table. Set CS to this record  PUSH ExitScope – set CS to parent of current scope. Update tables  POP Insert – add a new entry to the local table of CS Lookup – Search local table of CS. If not found, check the enclosing scope. Continue checking enclosing scopes until found or until run out of scopes.

CS 540 Spring 2009 GMU72 Example Program – As we compile … Program main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1 body of main Main a,b,c

CS 540 Spring 2009 GMU73 Example Program Program main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1 body of main Main sub1 a,b,c, sub1 a,d

CS 540 Spring 2009 GMU74 Example Program Program main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1 body of main Main sub1 sub2 a,b,c, sub1 a,d sub2 c,d

CS 540 Spring 2009 GMU75 Example Program Program main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1 body of main Main sub1 sub3 a,b,c, sub1 a,d sub2, sub3 a

CS 540 Spring 2009 GMU76 Example Program Program main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1 body of main Main sub1 a,b,c, sub1 a,d, sub2, sub3

CS 540 Spring 2009 GMU77 Example Program Program main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1 body of main Main a,b,c, sub1