ComputerLangugesProgramingCSECourseCh05-part2.ppt

ReemAlwani1 10 views 24 slides Sep 06, 2024
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

Computer languages course at CS major.


Slide Content

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("%d\n", x);
{
int x = 10;
int y = 20;
printf("%d\n", x);
{
int x = 15;
int z = 20;
printf("%d\n", 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("%d\n", x);
{
int y = 20;
printf("%d\n", x);
{
int x = 15;
int z = 20;
printf("%d\n", 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
MAINMAIN
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
ED
Possible procedure callsDesired 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
Tags