Non Local Names

12
 Acce ss T o Non Local Names  Acce ss T o Non Local Names 1 Shashwat Shriparv [email protected] InfinitySoft

Transcript of Non Local Names

8/8/2019 Non Local Names

http://slidepdf.com/reader/full/non-local-names 1/12

 Access To Non Local Names Access To Non Local Names

1

Shashwat [email protected]

InfinitySoft

8/8/2019 Non Local Names

http://slidepdf.com/reader/full/non-local-names 2/12

Scope :Scope :

y Scope is the textual region of a program inwhich a binding is active.

y Programming languages implement

Static Scoping: active bindings are determinedusing the text of the program at compile time.x Most recent scan of the program from top to bottom

x Closest nested subroutine rule

Dy namic Scoping: active bindings aredetermined by the flow of execution at run time.

2

8/8/2019 Non Local Names

http://slidepdf.com/reader/full/non-local-names 3/12

Egs:Egs:

Lexical scope

Pascal

CAda

Dynamic scope

LispSnobol

APL

3

8/8/2019 Non Local Names

http://slidepdf.com/reader/full/non-local-names 4/12

C losely Nested RuleC losely Nested Rule

1.The scope of a declaration in a block B includes B.

2.If a name x is not declared in a block B,then an

occurrence of x in B is in the scope of adeclaration of x in an enclosing block B1 suchthat

i. B1 has a declaration of x

ii. B1 is more closely nested around B than anyother block with a declaration of x.

4

8/8/2019 Non Local Names

http://slidepdf.com/reader/full/non-local-names 5/12

Non Local NamesNon Local Names

In a language with nested

procedures (or block s) and static scope(lexical scope), some names are neitherlocal nor global, they are non-local names.

5

8/8/2019 Non Local Names

http://slidepdf.com/reader/full/non-local-names 6/12

NonNon--local names in PASCALlocal names in PASCAL

procedure A

real a;

procedure B

real b;

reference a; ->non local name

end Bend A;

6

8/8/2019 Non Local Names

http://slidepdf.com/reader/full/non-local-names 7/12

main () {int a = 0, b=0; {

int b = 1; {

B2 int a = 2;print(a,b); }

B0 B1 {B3 int b = 3;

print(a,b); }print(a,b);}print(a,b); }

7

2, 1

0, 3

0, 1

0, 0

8/8/2019 Non Local Names

http://slidepdf.com/reader/full/non-local-names 8/12

Block Block--level and Procedurelevel and Procedure--levellevel

ARsARs1. Each block can be considered as an in-line procedure

without parameters. So we could create a new AR foreach block. This is block-level AR and is very ineff icient.

2. In procedure-level AR method, AR is only used for a

true procedure. The relative location of variables inindividual block s within a procedure can be computedand f ixed at compile time.

Storage for names 

8

a0

b0

b1

a2 , b1

8/8/2019 Non Local Names

http://slidepdf.com/reader/full/non-local-names 9/12

Lexical scope without nested proceduresLexical scope without nested procedures

y (1)int a[11];

y (2)readarray() {«.a«.}

y

(3)int partition(y,z) int y,z; {«.a«.}y (4)quick sort(m,n) int m,n; {«..}

y (5)main() {«.a«.}

9

8/8/2019 Non Local Names

http://slidepdf.com/reader/full/non-local-names 10/12

Lexical scope with nested proceduresLexical scope with nested procedures

y (1) program sort (input , output);

y (2)  var a : array [0..10] of integer;

y (3) x : integer;

y

(4) procedure readarray;y (5)  var I : integer;

y (6) begin «a«end{ readarray };

y (7) procedure exchange ( i , j : integer );

y (8) beginy (9) x := a[ i ] ; a[ i ] := a[ j ]; a[ j ] := x

y (10) end { exchange };

10

8/8/2019 Non Local Names

http://slidepdf.com/reader/full/non-local-names 11/12

y

(11) procedure quick sort( m , n : integer);y (12)  var k , v : integer;

y (13) function partition(y ,z : integer):integer;

y (14)  var i , j : integer;

y (15) begin «a «y (16) «v «

y (17) «exchange( i , j ); «

y (18) end { partition };

y (19) begin « end { quick sort };

y (20) begin « end { sort }

11

8/8/2019 Non Local Names

http://slidepdf.com/reader/full/non-local-names 12/12

12

Shashwat [email protected]