Tải bản đầy đủ (.pdf) (16 trang)

nguyên lý ngôn ngữ lập trình nguyễn hứa phùng name, binding and scope sinhvienzone com

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (101.71 KB, 16 trang )

C
o
e.

on

nZ

Dr. Nguyen Hua Phung


09, 2013

hV

ie

HCMC University of Technology, Viet Nam

in

m

Name, Binding and Scope

/>
Dr. Nguyen Hua Phung

Name, Binding and Scope



C
o
e.
on

Name - character string used to represent something else.

nZ

identifiers,
operators (+, &, *).

Use symbol instead of address to refer an entity.

hV

ie

Abstraction

in

m

Name

/>
Dr. Nguyen Hua Phung

Name, Binding and Scope



C
o
e.

on

Definition
Binding - the operation of associating two things.
Binding time - the moment when the binding is performed.

nZ

Some issues
Early binding vs. Late binding

ie

Static binding vs. Dynamic binding
Polymorphism - A name is bound to more than one entity.

hV

Alias - Many names are bound to one entity.

in

m


Binding

/>
Dr. Nguyen Hua Phung

Name, Binding and Scope


C
o
e.

on

Language design time

Language implementation time
Programming time
Linking time

ie

Load time

nZ

Compilation time

hV


Runtime

in

m

Binding Time

/>
Dr. Nguyen Hua Phung

Name, Binding and Scope


C
o

e.

Object - any entity in the program.

Binding lifetime

ie

nZ

Dangling reference
p = new int;
q = p;

delete p;
*q;

on

Object lifetime - the period between the object creation and
destruction.
p

q

hV

Leak memory - Garbage
p = new int;
p = null;

in

m

Object Lifetime

/>
Dr. Nguyen Hua Phung

Name, Binding and Scope


C

o
e.

Stack Dynamic

on

Static

nZ

Explicit Heap Dynamic

Static
Stack

hV

ie

Implicit Heap Dynamic

in

m

Object Allocation

Heap


/>
Dr. Nguyen Hua Phung

Name, Binding and Scope


C
o
e.

on

Definition
A block is a textual region, which can contain declarations to
that region

nZ

Example,

hV

ie

procedure f o o ( )
var x : i n t e g e r ;
begin
x := 1;
end ;


in

m

Blocks

{
int x ;
x = 1;
}

/>
Dr. Nguyen Hua Phung

Name, Binding and Scope


C
o

Static vs. Dynamic

on

e.

Definition
Scope of a binding is the textual region of the program in which
the binding is effective.


nZ

Static scope, or lexical scope, is determined during
compilation

ie

Current binding - in the block most closely surround
Global scope
Local static scope

hV

Dynamic scope is determined at runtime.
Current binding - the most recently execution but not
destroyed

in

m

Scope

/>
Dr. Nguyen Hua Phung

Name, Binding and Scope


C

o
e.

on

A reference to an identifier is always bound to its most
local declaration

nZ

A declaration is invisible outside the block in which it
appears
Declarations in enclosing blocks are visible in inner
blocks, unless they have been re-declared

hV

ie

Blocks may be named and its name declaration is
considered as a local declaration of outer block.

in

m

Staic Scope Rules for Blocks

/>
Dr. Nguyen Hua Phung


Name, Binding and Scope


C
o
e.

nZ

on

var A, B, C: real; //1
procedure Sub1 (A: real); //2
var D: real;
procedure Sub2 (C: real);
var D: real;
begin
. . . C:= C+B; . . .
end;
begin
. . . Sub2(B); . . .
end;
begin
. . . Sub1(A); . . .
end.

Variable
A:real //1
B:real //1


ie

C:real //1
A:real //2
...

hV

in

m

Example on Static scope

/>
Dr. Nguyen Hua Phung

Name, Binding and Scope

Scope
Main
Main, Sub1,
Sub2
Main, Sub1
Sub1, Sub2


C
o

e.

on

X in Sub2 ?
Calling chain:
Big → Sub1 → Sub2
X ⇒ X:Integer in Sub1
Calling chain:
Big → Sub2
X ⇒ X:Real in Big

hV

ie

nZ

procedure Big is
X : Real;
procedure Sub1 is
X : Integer;
begin – of Sub1
...
end; – of Sub1
procedure Sub2 is
begin – of Sub2
...X ...
end; – of Sub2
begin – of Big

...
end; – of Big

in

m

Example on Dynamic Scope

/>
Dr. Nguyen Hua Phung

Name, Binding and Scope


C
o
e.

on

The referencing environment of a statement is the
collection of all names that are visible to the statement

nZ

In a static-scoped language, it is the local names plus all of
the visible names in all of the enclosing scopes

hV


ie

In a dynamic-scoped language, the referencing
environment is the local bindings plus all visible bindings in
all active subprograms

in

m

Referencing Environment

/>
Dr. Nguyen Hua Phung

Name, Binding and Scope


C
o

e.

nZ

on

var A, B, C: real; //1
procedure Sub1 (A: real); //2

var D: real;
procedure Sub2 (C: real);
var D: real;
begin
. . . C:= C+B; . . .
end;
begin
. . . Sub2(B); . . .
end;
begin
. . . Sub1(A); . . .
end.

Function

Main
Sub1

ie

Sub2

hV

in

m

Example on Static-scoped Language


/>
Dr. Nguyen Hua Phung

Name, Binding and Scope

Referencing
Environment
A, B, C, Sub1
A, B, C, D,
Sub1, Sub2
A, B, C, D,
Sub1, Sub2


o3
o4

C
o

b
c

on

o1
o2

hV


ie

nZ

c
void sub1() {
int a, b;
d
...
} /* end of sub1 */
void sub2() {
int b, c;
...
sub1;
} /* end of sub2 */
void main() {
int c, d;
...
sub2();
} /* end of main */

→ sub2

→ sub2

b
c

o5
o6


e.

main

in

m

Example on Dynamic-scoped Language

Frame

main
sub2
sub2
sub1

a
b

o7
o8

Referencing
Environment
c → o1, d → o2
b → o3, c → o4,
d → o2
b → o5, c → o6,

d → o2
a → o7, b → o8,
c → o6, d → o2

/>
Dr. Nguyen Hua Phung

→ sub1

Name, Binding and Scope


C
o
e.
on

Name

nZ

Binding
Scope

hV

ie

Referencing Environment


in

m

Summary [1]

/>
Dr. Nguyen Hua Phung

Name, Binding and Scope


C
o
e.
on

hV

ie

nZ

, Maurizio Gabbrielli and Simone Martini, Programming Languages:
Principles and Paradigms, Chapter 4, Springer, 2010.

in

m


References I

/>
Dr. Nguyen Hua Phung

Name, Binding and Scope



×