Docsity

Lecture Slides on Static and Dynamics Scope - Programming Languages | CS 403, Study notes of Programming Languages

Toggle sidebar
bg1
1
Spring 2003 CS 403 Class Notes Page 1
Lecture 12 - Tuesday, 2/18/03
• Today
– Static vs. dynamic scope
• Reading Assignments For Next Class
– pp. 107-112
• Assignments
– Assignment #3 due Thursday night
Spring 2003 CS 403 Class Notes Page 2
Scope
• Def: The scope of an identifier is the range of
statements over which it is visible.
• Two major types of identifiers where scope is an
issue:
– Variables
– Function names
• We consider these in order.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Lecture Slides on Static and Dynamics Scope - Programming Languages | CS 403 and more Study notes Programming Languages in PDF only on Docsity!

Spring 2003 CS 403 Class Notes (^) Page 1

Lecture 12 - Tuesday, 2/18/

  • Today
    • Static vs. dynamic scope
  • Reading Assignments For Next Class
    • pp. 107-
  • Assignments
    • Assignment #3 due Thursday night

Scope

  • Def: The scope of an identifier is the range of

statements over which it is visible.

  • Two major types of identifiers where scope is an

issue:

  • Variables
  • Function names
  • We consider these in order.

Spring 2003 CS 403 Class Notes (^) Page 3

Variable Scopes

  • Def: The nonlocal variables of a program unit are those that are visible but not declared there.
  • The scope rules of a language determine how references to names are associated with variables.

Static Scope

Based on program text

  • To connect a name reference to a variable, you (or the compiler) must find the declaration.
  • Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name.
  • Enclosing static scopes (to a specific scope) are called its static ancestors ; the nearest static ancestor is called a static parent.

Spring 2003 CS 403 Class Notes (^) Page 7

Function Nesting in C(++)

  • Traditional C and C++ didn’t do function nesting
  • C99 standard added nested functions
  • Nobody typically really uses them as best I can tell: - Most C++ compilers probably not compliant with

this standard

  • Many C compilers not compliant either

C99 Nested Functions

  • Nesting structure of program: void main(){ void a(){ int x; void b() {x = 3;} void c() {b();} x = 1; c(); cout << x << endl; } a(); }

Main

a

b c

ND = 0

ND = 1

ND = 2

Spring 2003 CS 403 Class Notes (^) Page 9

Static Scope with {}

Blocks - a method of creating static scopes inside program units--from ALGOL 60.

Examples: C and C++: for (...) {

int index;

Scope Nesting With {}

for (int i = 1; i <= 50; i++){ int x; x = 1;

for (int j = 1; j <= 50; j++){ cout << x << endl; //outer x x = j + 3; } }

Spring 2003 CS 403 Class Notes (^) Page 13

Static vs. Dynamic Scope: Behavior Differences

Static scope output: ?? Dynamic scope output: ??

Implementing Static Scope

  • Model discussed previously:
    • Each variable translated into an offset
    • Storing in or retrieving from a variable is in terms

of SP+offset

  • With static scope:
    • Each variable translated into (nesting depth,

offset)

  • A “display stack” is maintained that points to the

last activation record at a particular nesting depth

  • Need function visibility rules to be in place to make

this work

Spring 2003 CS 403 Class Notes (^) Page 15

Example

P

P2 P

P3 F

Nesting Diagram

X dec

X dec

X dec

Use of x

Calling sequence

P

P

P

F

Run-Time Stack

P

P

P

F

Activation Records

Display

Reference to X in F1: (ND=1, offset= i )

Display(ND)+offset will yield address of X