Docsity

UNC Chapel Hill's COMP 524: Programming Language Concepts - Prof. Aaron Block, Study notes of Programming Languages

Partial preview of the text

Download UNC Chapel Hill's COMP 524: Programming Language Concepts - Prof. Aaron Block and more Study notes Programming Languages in PDF only on Docsity!

Lecture 8: Scope, Symbol Table, & Runtime Stack

COMP 524 Programming Language Concepts Aaron Block February 12, 2007 Based on notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

Goal of Lecture

  • Discuss scoping! 2

Scope

  • Scope is the textual region of a program in which

binding is active.

  • Programming languages implement
    • Static Scoping (or lexical): Active bindings are determined using the text of the program at compile time - Most recent scan of the program from top to bottom - Closes nested subroutine rule.
    • Dynamic Scoping: active bindings are determined by the flow of execution at run time
  • Current active binding called Referencing environment. 4

Nest Subroutines

  • Nest subroutines are able to access parameters and

local variables of the surrounding scope

5 procedure P1(A1); var X : real; procedure P2(A2); procedure P3(A3); X = 2; end end end

Dynamic Scope

  • Bindings between name

and objects depend on the

flow of control at run time

  • the current binding is the one found most recently during execution. 7 a:int; procedure first() a:= procedure second() a:int first() a:= if read_int()> second() else first() write_int(a)

Perl and Dynamic Scope

  • Perl allows dynamic scope.
  • If not declared otherwise, variables are dynamically

created , global , and persistent.

  • Dynamic creation: Variables appear when referenced.
  • Global : Variables can be referenced in any and all cod written
  • Persistent : Variables stay around until end of execution. 8

Perl and Dynamic Scope

10 aFunc(); sub bFunc{ $c = $a; #$c = 1 if run in or after aFunc } sub aFunc{ $a = 1; bFunc(); }

Perl

  • “my $abc”
    • Makes variable statically scoped
    • Only available to this subroutine
    • Not available to called subroutines or originating subroutines
    • Destroyed when execution exists the block its in. 11

Perl and Dynamic Scope (my)

13 aFunc(); sub bFunc{ $c = $a; #$c is undefined no matter if it #is run at or in bFunc } sub aFunc{ my($a); $a = 1; bFunc(); }

Perl

  • “local $var”
    • Makes variable dynamically scoped
    • “Temporary global”
    • Available to called subroutines, but not available to originating Subroutines
    • Destroyed when execution exists current block 14

Perl and Dynamic Scope (local)

16 aFunc(); sub bFunc{ $c = $a; #$c is undefined if bFunc is run #after aFunc, but is 1 if run #in $aFunc } sub aFunc{ local($a); $a = 1; bFunc();

Lifetime vs Scope

  • Some objects exist only when scope is active
  • ... however, this is not always the case. 17 class foo{ public static int sum = 0; void vooDo(){ sum ++; } } //Where is sum? Its not active but it exists. g1 = new foo; g1.vooDo();

Nested Subroutines--Determining Scope

19

A
E
B
D
C
A
E
B
D
C

fp

Static

Links

Nesting

SCPs

Symbol Table

  • In statically scoped languages, compilers keep track of

names using a data structure called a symbol table.

  • The symbol table might be retained after compiling and

made available at runtime (e.g., for debugging)

20 0 ... name type Scope Level other curLev