Names, Bindings
and Scopes
Chapter 5
(part 1)
Type Checking
 Type Checking - activity to ensure that operands of an
operator are of compatible types.
 Compatible – legal for operator or allowed under language rules to be
converted to legal type
 Conversion:
 Automatic (implicit) → coercion
 Eg. In Java, int + float → int is coerce to float
 By code (explicit) → casting
 Eg. In C, short a = 2000;
int b;
b = (int) a;
Copyright © 2006 Addison-Wesley. All rights reserved.
Type Checking
 Type error – application of an operator to an
operand of an inappropriate type.
 Eg. In C, int pass to a float function → type error
Copyright © 2006 Addison-Wesley. All rights reserved.
Scope
main() { /** C **/
int a = 5;
printf("%dn", x);
{
int x = 10;
int y = 20;
printf("%dn", x);
{
int x = 15;
int z = 20;
printf("%dn", x);
}
}
}
Local variable – visible only to
the block where variable being
declared
Non-local variable – visible to all
segments of the code
Static Scope
Static Scope
 2 categories – nested static scope and cannot be nested
 JavaScript and PHP do not support nested static scopes
 But JavaScript and PHP do support function-level scope
var abc = false;
if ( s == "HELLO" ) {
abc = true;
var abc = 500;
document.write("abc is " + abc)
}
document.write("abc is still " + abc)
Example :
main() { /** C **/
int x = 5;
printf("%dn", x);
{
int y = 20;
printf("%dn", x);
{
int x = 15;
int z = 20;
printf("%dn", x);
}
}
}
Declaration of x will be searched
locally first, then from the calling
procedure
x is declared locally.
Therefore x=5 is hidden.
Blocks
 A method of creating static scopes inside program units --
from ALGOL 60
 allows a section of code to have its own local variables whose scope is
minimized.
 Examples:
C and C++: for (...) {
int index;
...
}
Ada: declare LCL : FLOAT;
begin
...
end
compound statement
– define new scope
declare statement –
specified block
This is called as BLOCK
Example:
Error!!
Evaluation of Static Scoping
 Assume MAIN calls A and B
A calls C and D
B calls A and E
MAIN
MAIN
E
A
C
D
B
A B
C D E
MAIN is Static
parent of B
MAIN and A
are static
ancestry of
C and D
Static Scope Example
MAIN MAIN
A B
C D E
A
C
B
E
D
Possible procedure calls
Desired procedure calls
Static Scope (continued)
 Suppose the spec is changed so that D must now
access some data in B
 Solutions:
 Put D in B (but then C can no longer call it and D cannot
access A's variables)
 Move the data from B that D needs to MAIN (but then all
procedures can access them)
 Same problem for procedure access
 Overall: static scoping often encourages many
globals
Example : Static Scope
Answer : main Answer : epsilon
Answer : all procedures
Dynamic Scope
Scope Example
MAIN
- declaration of x
SUB1
- declaration of x -
...
call SUB2
...
SUB2
...
- reference to x -
...
...
call SUB1
…
MAIN calls SUB1
SUB1 calls SUB2
SUB2 uses x
x is referenced from the
calling procedure which is
SUB1
Scope Example
 Static scoping
 Reference to x is to MAIN's x
 Dynamic scoping
 Reference to x is to SUB1's x
 Evaluation of Dynamic Scoping:
 Advantage: convenience
 Disadvantage: poor readability
Example 1: Dynamic Scope
Answer: Kappa
Example 2: Dynamic Scope
Answer: iota & lambda
begin
integer m, n;
procedure hardy;
begin
print("in hardy -- n = ", n);
end;
procedure laurel(n: integer);
begin
print("in laurel -- m = ", m);
print("in laurel -- n = ", n);
hardy;
end;
m := 50;
n := 100;
print("in main program -- n = ", n);
laurel(1);
hardy;
end;
Static scoping:
in main program -- n =
in laurel -- m =
in laurel -- n =
in hardy -- n =
in hardy -- n =
Dynamic scoping:
in main program -- n =
in laurel -- m =
in laurel -- n =
in hardy -- n =
in hardy -- n =
100
50
1
100
100
100
50
1
1
100
#include<stdio.h>
int x = 10;
int f()
{
return x;
}
int g()
{
int x = 20;
return f();
}
int main()
{
printf("%d", g());
return 0;
}
Static scoping:
Value printed in main is
 in static scoping the compiler first searches
in the current block, then in the surrounding
blocks successively and finally in the global
variables
Dynamic scoping:
Value printed in main is
 in dynamic scoping the compiler first
searches the current block and then successively
all the calling functions.
10
20
Scope and Lifetime
 Scope and lifetime are sometimes closely
related, but are different concepts
 Consider a static variable in a C or C++
function
 Statically bound to the scope of the function and
to storage.
 But lifetime extends over the entire execution of
program of which it is a part.
Example
 Scope of variable sum
contained in function
compute only
 Lifetime of variable sum
begins when function
compute executes and
extends until function
printheader
complete

ComputerLangugesProgramingCSECourseCh05-part2.ppt

  • 1.
  • 2.
    Type Checking  TypeChecking - activity to ensure that operands of an operator are of compatible types.  Compatible – legal for operator or allowed under language rules to be converted to legal type  Conversion:  Automatic (implicit) → coercion  Eg. In Java, int + float → int is coerce to float  By code (explicit) → casting  Eg. In C, short a = 2000; int b; b = (int) a; Copyright © 2006 Addison-Wesley. All rights reserved.
  • 3.
    Type Checking  Typeerror – application of an operator to an operand of an inappropriate type.  Eg. In C, int pass to a float function → type error Copyright © 2006 Addison-Wesley. All rights reserved.
  • 4.
  • 5.
    main() { /**C **/ int a = 5; printf("%dn", x); { int x = 10; int y = 20; printf("%dn", x); { int x = 15; int z = 20; printf("%dn", x); } } } Local variable – visible only to the block where variable being declared Non-local variable – visible to all segments of the code
  • 6.
  • 7.
    Static Scope  2categories – nested static scope and cannot be nested  JavaScript and PHP do not support nested static scopes  But JavaScript and PHP do support function-level scope var abc = false; if ( s == "HELLO" ) { abc = true; var abc = 500; document.write("abc is " + abc) } document.write("abc is still " + abc)
  • 8.
    Example : main() {/** C **/ int x = 5; printf("%dn", x); { int y = 20; printf("%dn", x); { int x = 15; int z = 20; printf("%dn", x); } } } Declaration of x will be searched locally first, then from the calling procedure x is declared locally. Therefore x=5 is hidden.
  • 9.
    Blocks  A methodof creating static scopes inside program units -- from ALGOL 60  allows a section of code to have its own local variables whose scope is minimized.  Examples: C and C++: for (...) { int index; ... } Ada: declare LCL : FLOAT; begin ... end compound statement – define new scope declare statement – specified block This is called as BLOCK
  • 10.
  • 11.
    Evaluation of StaticScoping  Assume MAIN calls A and B A calls C and D B calls A and E MAIN MAIN E A C D B A B C D E MAIN is Static parent of B MAIN and A are static ancestry of C and D
  • 12.
    Static Scope Example MAINMAIN A B C D E A C B E D Possible procedure calls Desired procedure calls
  • 13.
    Static Scope (continued) Suppose the spec is changed so that D must now access some data in B  Solutions:  Put D in B (but then C can no longer call it and D cannot access A's variables)  Move the data from B that D needs to MAIN (but then all procedures can access them)  Same problem for procedure access  Overall: static scoping often encourages many globals
  • 14.
    Example : StaticScope Answer : main Answer : epsilon
  • 15.
    Answer : allprocedures
  • 16.
  • 17.
    Scope Example MAIN - declarationof x SUB1 - declaration of x - ... call SUB2 ... SUB2 ... - reference to x - ... ... call SUB1 … MAIN calls SUB1 SUB1 calls SUB2 SUB2 uses x x is referenced from the calling procedure which is SUB1
  • 18.
    Scope Example  Staticscoping  Reference to x is to MAIN's x  Dynamic scoping  Reference to x is to SUB1's x  Evaluation of Dynamic Scoping:  Advantage: convenience  Disadvantage: poor readability
  • 19.
    Example 1: DynamicScope Answer: Kappa
  • 20.
    Example 2: DynamicScope Answer: iota & lambda
  • 21.
    begin integer m, n; procedurehardy; begin print("in hardy -- n = ", n); end; procedure laurel(n: integer); begin print("in laurel -- m = ", m); print("in laurel -- n = ", n); hardy; end; m := 50; n := 100; print("in main program -- n = ", n); laurel(1); hardy; end; Static scoping: in main program -- n = in laurel -- m = in laurel -- n = in hardy -- n = in hardy -- n = Dynamic scoping: in main program -- n = in laurel -- m = in laurel -- n = in hardy -- n = in hardy -- n = 100 50 1 100 100 100 50 1 1 100
  • 22.
    #include<stdio.h> int x =10; int f() { return x; } int g() { int x = 20; return f(); } int main() { printf("%d", g()); return 0; } Static scoping: Value printed in main is  in static scoping the compiler first searches in the current block, then in the surrounding blocks successively and finally in the global variables Dynamic scoping: Value printed in main is  in dynamic scoping the compiler first searches the current block and then successively all the calling functions. 10 20
  • 23.
    Scope and Lifetime Scope and lifetime are sometimes closely related, but are different concepts  Consider a static variable in a C or C++ function  Statically bound to the scope of the function and to storage.  But lifetime extends over the entire execution of program of which it is a part.
  • 24.
    Example  Scope ofvariable sum contained in function compute only  Lifetime of variable sum begins when function compute executes and extends until function printheader complete