0% found this document useful (0 votes)
21 views29 pages

CSC304 Week 6 Slides B

The document discusses scope and binding in programming languages. It covers static and dynamic scope, blocks, global scope, referencing environments, and named constants. Static scope allows more reliable access to variables than dynamic scope.

Uploaded by

Adebimpe Adetoba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views29 pages

CSC304 Week 6 Slides B

The document discusses scope and binding in programming languages. It covers static and dynamic scope, blocks, global scope, referencing environments, and named constants. Static scope allows more reliable access to variables than dynamic scope.

Uploaded by

Adebimpe Adetoba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

CSC304:

Survey of Programming Languages

NAMES, BINDINGS and SCOPE


Kingsley Chiwuike Ukaoha, PhD
Scope
• The scope of a variable is the range of
statements in which the variable is visible.
– A variable is visible in a statement if it can be
referenced in that statement.

• Local & non local variables

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

• E.g., Ada, JavaScript, Common LISP, Scheme, Fortran


2003+, F#, and Python

– Subroutine cannot be nested


• E.g. , C-based language
4
Static Scope (Cont’d)
• How to find a reference to a variable in static-scoped
language?
– Suppose a reference is made to a variable x in subprogram
sub1.

– The correct declaration is found by first searching the


declarations of subprogram sub1.

– If no declaration is found for the variable there, the search


continues in the declarations of the subprogram that declared
subprogram sub1, which is call its static parent.
5
Static Scope (Cont’d)

• 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.

• Hidden variables can be accessed in some languages


– E.g., Ada

big.x

7
Blocks
• Many languages allow new static scopes to be
defined in the midst of executable code
– Originated from ALGOL 60

– Allows a section of code to have its own local


variables whose scope is minimized
• Defined variables are typically static dynamic

– 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

• However, C99, C++, Java, JavaScript, C##,


allow variable declarations to appear anywhere
– Scoping rules are different

10
Global Scope

• In C, C++, PHP, JavaScript, and Python,


variable definitions can appear outside all the
functions
– Create global variables, which potentially can be
visible to those functions

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

• Dynamic scoping is based on the calling


sequence of subprogram, not on their spatial
relationship to each other.
– The scope can be determined only at run time.

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

– In ability to type check references to nonlocals


directly

– Make programs much more difficult to read

– Slow in referencing nonlocal variables


17
Evaluation of Dynamic Scoping (Cont’d)

• Merit:
– The parameters passed from one
subprogram to another are variables that
are defined in the caller.

– None of these needs to be passed

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

– The reference environment in a dynamically scoped


language is the locally declared variables, plus the
variables of all other subprograms that are currently
active.
21
Named Constants
• A name constant is variable that is bound to a value only
once.
– Useful as aids to readability and program reliability

• E.g.
– In Java,
• final int len=100;

– Ada and C++ allow dynamic binding of values to named


constants, in C++:
• const int result = 2* width +1 ; 22
Summary
• Case sensitivity and the relationship of names to
special words, which are either reserved words or
keywords, are the design issues for names.
• Variables can be characterized by the sextuple of
attributes: name, address, value, type, lifetime,
and scope.
• Aliases are two or more variables bound to the
same storage address.
• They are regarded as detrimental to reliability but
are difficult to eliminate entirely from a language.
23
Summary
• Binding is the association of attributes with program
entities.
• Knowledge of the binding times of attributes to
entities is essential to understanding the semantics of
programming languages.
• Binding can be static or dynamic.
• Declarations, either explicit or implicit, provide a
means of specifying the static binding of variables to
types.
• In general, dynamic binding allows greater flexibility
but at the expense of readability, efficiency, and
24
reliability.
Summary
• Scalar variables can be separated into four categories
by considering their lifetimes: static, stack dynamic,
explicit heap dynamic, and implicit heap dynamic.
• Static scoping is a central feature of ALGOL 60 and
some of its descendants.
• It provides a simple, reliable, and efficient method of
allowing visibility of nonlocal variables in
subprograms.
• Dynamic scoping provides more flexibility than
static scoping but, again, at the expense of
readability, reliability, and efficiency.
25
Summary
• Most functional languages allow the user to
create local scopes with let constructs, which
limit the scope of their defined names.
• The referencing environment of a statement is the
collection of all of the variables that are visible to
that statement.
• Named constants are simply variables that are
bound to values only once.

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

You might also like