CSC304 Week 6 Slides B
CSC304 Week 6 Slides B
2
Static Scope
• ALGOL 60 introduced the method of binding
names to nonlocal variables call static
scoping
– The scope of a variable can be statically
determined
• Prior to execution
3
Static Scope (Cont’d)
• Two categories of static scoped languages
– Subroutine can be nested
• Nested static scopes
• A JavaScript function
function big() { big()
function sub1(){ var x;
var x=7;
sub2(); }
function sub2() {
sub1() sub2()
var x; var y;
var y=x; }
var x=3;
sub1();
}
6
Static Scope (Cont’d)
• Static ancestry
• Hidden
– The outer x is hidden from sub1.
big.x
7
Blocks
• Many languages allow new static scopes to be
defined in the midst of executable code
– Originated from ALGOL 60
– Called a block
• Origin of the phrase block-structured language 8
Blocks (Cont’d)
• The scopes created by blocks, which could be nested in
larger blocks, are treated exactly like those created by
subprgrams
– legal in C and C++, but not in Java and C# - too error-prone
void sub() {
int count;
while (...) {
int count;
count++;
...
}
…
}
9
Declaration order
• In C89, all data declarations in a function except
those in nested blocks must appear at the
beginning of the function
10
Global Scope
11
Global Scope (Cont’d)
• C, C++ have both declarations and definitions of
global data.
– For a specific global name, a C program can have
any number of compatible declaration, but only a
single definition
– Definition Allocation of memory space
• E.g., a declaration of variable
extern int sum;
12
Global Scope (Cont’d)
• The idea of declaration and definition carries over
to the functions of C and C++.
main(){
int foo(int); A prototype,
… declaration
}
int foo(int x;)
{ A function
… definition
}
13
Evaluation of Static Scope
main()
• Problems of static
scoping A() B()
var x;
– In most cases it allows
more access to both
variables and C() D() E()
subprograms than is var x;
necessary main()
– Software is highly var x;
dynamic – programs
that are used regularly A() B()
continually change. var x;
• E.g., E() wants to
access x in D()
C() D() E()
var x; 14
Dynamic Scope
15
Dynamic Scope (Cont’d)
• Consider the following two calling sequences:
– big calls sub1, sub1 calls sub2
– big calls sub2
function big() {
function sub1(){
var x=7;
sub2(); }
function sub2() {
var y=x;
var z=3; }
var x=3;
sub1();
sub2();
}
16
Evaluation of Dynamic Scoping
• Problems follow directly from dynamic scoping:
– No way to protect local variables from this
accessibility
• Merit:
– The parameters passed from one
subprogram to another are variables that
are defined in the caller.
18
Scope and Lifetime (Cont’d)
• The apparent relationship between scope and
lifetime does not hold in other situation
– Second para.
– E.g.
void printheader () {
… }
void compute () {
int sum;
…
printheader(); }
19
Referencing Environments
• The referencing environment of a statement is
the collection of all variables that are visible in
the statement
– In a static scoped language is the variables declared
in its local scope plus the collection of all variables
of its ancestor scopes
20
Referencing Environments (Cont’d)
• For dynamic scoped language:
– A subprogram is active if its execution has begun but
has not yet terminated
• E.g.
– In Java,
• final int len=100;
26
Class Activities …2
i. Which of the following identifier forms is most readable?
Support your decision.
a. SumOfSales
b. sum_of_sales
c. SUMOFSALES
ii. Some programming languages are typeless. What are the
obvious advantages and disadvantages of having no types in a
language?
iii. Write a C function that includes the following
sequence of statements:
x = 21;
int x;
x = 42;
a. Run the program and explain the results. Rewrite the
27
same code in C++ and Java and compare the results.
Class Activities …2
iv. Consider the following Python program:
x = 1;
y = 3;
z = 5;
def sub1():
a = 7;
y = 9;
z = 11;
...
def sub2():
global x;
a = 13;
x = 15;
w = 17;
28
...
Class Activities …2
def sub3():
nonlocal a;
a = 19;
b = 21;
z = 23;
...
...
a. List all the variables, along with the program units
where they are declared, that are visible in the bodies
of sub1, sub2, and sub3, assuming static scoping is
used.
29