concepts of programming languages chapter 5 names, bindings, and scopes lec. 12 lecturer: dr. emad...

34
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Upload: moses-ray

Post on 18-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Concepts of programming languages

Chapter 5Names, Bindings, and Scopes

Lec. 12Lecturer: Dr. Emad Nabil

1-1

Page 2: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-2

Chapter 5 Topics

Introduction Names Variable

s

The Concept of Binding Scope Scope and

Lifetime

Referencing Environment

s

Named Constants

Page 3: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-3

The Concept of Binding A binding is an association between an

entity and an attribute, such as between a variable and its type or value, or between an operation and a symbol

• Binding time is the time at which a binding takes place.

variable valueBinding

symbol operationBinding

Page 4: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-4

Possible Binding Times1. Language design time -- ex: bind operator symbols

to operations

2. Language implementation time– ex: bind floating point type to a representation “how to implement float num.”

3. Compile time – ex: bind a variable to a type in C or Java

4. Load time – ex: bind a C or C++ static variable to a memory cell)

5. Runtime – ex: bind a non-static local variable to a memory cell

Page 5: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Consider the following Java assignment statement:

Int count;count = count + 5;

• The type of count is bound at compile time.

• The set of possible values of count is bound at compiler design time.

• The meaning of the operator symbol + is bound at compile time, when the types of its operands have been determined.

• The internal representation of the literal 5 is bound at compiler design time.

• The value of count is bound at execution time with this statement.

Copyright © 2012 Addison-Wesley. All rights reserved. 1-5

Page 6: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Binding value

type

storage

Copyright © 2012 Addison-Wesley. All rights reserved. 1-6

static

Dynamic

Page 7: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-7

Static and Dynamic Binding

• A binding is static if it first occurs before run time and remains unchanged throughout program execution.

• A binding is dynamic if it first occurs during execution or can change during execution of the program

Page 8: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Static and Dynamic Binding

Static Binding Dynamic Binding

Binding occurs at • design time • implementation time• Compile time • Load time

Binding occurs at Runtime

Copyright © 2012 Addison-Wesley. All rights reserved. 1-8

Page 9: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Binding value

type

storage

Copyright © 2012 Addison-Wesley. All rights reserved. 1-9

static

Dynamic

Page 10: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-10

Type Binding

• How is a type specified? (explicit/implicit declaration)

• When does the binding take place? (static/dynamic)

Type binding

Static

Explicit type

declaration

Implicit type

declaration

dynamic

Page 11: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

1-11

Explicit Declaration

• An explicit declaration is a program statement used for declaring the types of variables, ex: in C++

Int x;x=5;

Type binding

Static

Explicit type

declaration

Implicit type

declaration

dynamic

Page 12: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

1-12

Implicit Declaration

• An implicit declaration is a default mechanism for specifying types of variables through default conventions, rather than declaration statements

• based on the syntactic form of the variable’s name

• Ex: In Fortran , If the identifier begins with one of the letters I, J, K, L, M, or N, or their lowercase versions, it is implicitly declared to be Integer type; otherwise, it is implicitly declared to be Real type.

• Fortran, BASIC, Perl, Ruby, JavaScript, and PHP provide implicit declarations (Fortran has both explicit and implicit)– Advantage: writability – Disadvantage: reliability

Page 13: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Implicit Declaration (continued)

• Another kind of implicit type declarations uses context type inferencing to determine types of variables (context)

– C# - a variab le can be declared w ith var and an in itia l va lue. The in itia l va lue sets the type

– Ex: var x= 3.5

– Visual BASIC 9.0+ , M L, Haskell, F# , and Go use type inferencing. The context of the appearance of a variable determ ines its type

• Another kind of implicit type declarations uses context type inferencing to determine types of variables (context)

– C# - a variable can be declared with var and an initial value. The initial value sets the type

– Ex: var x= 3.5

– Visual BASIC 9.0+, ML, Haskell, F#, and Go use type inferencing. The context of the appearance of a variable determines its type

1-13

F# example:let a = 1let b = 100ulet str = "text"

Type are inferred from value’s from

Page 14: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Type binding

Static

Explicit type

declaration

Implicit type

declaration

dynamic

visual basic 9.0+, ml, haskell, f#, C# and go

Page 15: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Dynamic Binding

• With dynamic type binding, the type of a variable is not specified by a declaration statement, nor can it be determined by the spelling of its name

• Instead, the variable is bound to a type when it is assigned a value in an assignment statement.

Copyright © 2012 Addison-Wesley. All rights reserved. 1-15

Page 16: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

1-16

Dynamic Type Binding

• Dynamic Type Binding (JavaScript, Python, Ruby, PHP, and C# (limited))

• Specified through an assignment statement e.g., JavaScript

list = [2, 4.33, 6, 8];list = 17.3;

List type is array only after exe. of assignment

//C# example: This could be the result of call to Python / Ruby etc !dynamic myDynamic = GetDynamic(....);myDynamic.Foo("Hello"); //Call a methodMyMethod(myDynamic); // Passing as parameter

Page 17: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-17

Dynamic Type Binding

– Advantage: flexibility (generic program units)– Disadvantages:

• High cost (dynamic type checking and interpretation)

• Type error detection by the compiler is difficult

Page 18: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-18

Variable Attributes (continued)

• Storage Bindings & Lifetime– Allocation - getting a cell from some pool of

available cells– Deallocation - putting a cell back into the

pool

• The lifetime of a variable is the time during which it is bound to a particular memory cell

1.Name 2.Address 3.Value 4.Type

5.Lifetime 6.Scope

Page 19: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Categories of Variables by Lifetimes

Staticvariables

Stack dynamic variables

Explicit Heap

Dynamic Variable

s

Implicit Heap

Dynamic Variable

s

Copyright © 2012 Addison-Wesley. All rights reserved. 1-19

Binding to storage

Page 20: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-20

1. Static variables• Static variables are bound to memory cells before

execution begins and remains bound to the same memory cell throughout execution, e.g., C and C++ static variables in functions– Advantages: efficiency (direct addressing), history-sensitive

subprogram support– Disadvantage: lack of flexibility (no recursion)

static variables Declaration

Type Binding

StaticCompilation time

Storage binning

Static“load time”

Stored in Data

segment

Page 21: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-21

2. Stack dynamic- variables• Stack-dynamic- variables : are those whose

– storage bindings are created when their declaration statements are elaborated.

– but whose types are statically bound.

Stack dynamic variables Declaration

Type Binding

StaticCompilation time

Storage binning When Elaboration“execution reach

a stmt”

Dynamic

Binding

Stored in

stack

Ex: in C++int x=5;

Page 22: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-22

• Ex: local variables in C subprograms (not declared static) and Java methods are statically

• Advantage: allows recursion; conserves storage• Disadvantages:

– Overhead of allocation and deallocation– Subprograms cannot be history sensitive

2. Stack dynamic- variables cont.

Page 23: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-23

• Explicit heap-dynamic -- Allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution

• Nameless variables, Referenced only through pointers or references, e.g. dynamic objects in C++ (via new and delete), all objects in Java

• Advantage: provides for dynamic storage management• Disadvantage: inefficient and unreliable

Explicit heap-dynamic Declaration

Type Binding

static

Storage binning When creation of object in run time

Dynamic

Stored in heap

3. Explicit heap-dynamic variables

int *intnode; intnode = new int;delete intnode;

Static type binding

Dynamic storage binding in Heap

Page 24: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

1-24

• Implicit heap-dynamic variables

– are bound to heap storage only when they are assigned values

– all their attributes are bound every time they are assigned.

– Ex: in javascript

• highs = [74, 84, 86, 90, 71];– Regardless of whether the variable named highs was

previously used in the program or what it was used for, it is now an array of five numeric values.

– EX: all variables in APL; all strings and arrays in Perl, JavaScript, and PHP

4. Implicit heap-dynamic variables

The 6 attributes including Type, storage, value are bound every time meeting assignment

Page 25: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-25

• Advantage: flexibility (generic code)• Disadvantages:

– Inefficient, because all attributes are defined every assignment

– Loss of error detection

Implicit heap-dynamic-Declaration

Type Binding

Dynamic

Storage binning Every time when

as assignment is

executed

Dynamic

Stored in heap

4. Implicit heap-dynamic variables

Page 26: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Value binding

Type binding Storage binding

Static variables

Stack dynamicvariables

Explicit heap dynamic variables

Implicit heap dynamic variablesCopyright © 2012 Addison-Wesley. All rights reserved. 1-26

Page 27: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-27

Chapter 5 Topics

Introduction Names Variables

The Concept of Binding Scope Scope and

Lifetime

Referencing Environment

s

Named Constants

Page 28: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-28

Variable Attributes: Scope

• The scope of a variable is the range of statements over which it is visible

• The local variables of a program unit are those that are declared in that unit

• The nonlocal variables of a program unit are those that are visible in the unit but not declared there

• Global variables are a special category of nonlocal variables

• The scope rules of a language determine how references to names are associated with variables

Page 29: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-29

Static Scope

• Based on program text• To connect a name reference to a variable, you

(or the compiler) must find the declaration• Search process: search declarations, first locally,

then in increasingly larger enclosing scopes, until one is found for the given name

• Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent

• Some languages allow nested subprogram definitions, which create nested static scopes (e.g., Ada, JavaScript, Common LISP, Scheme, Fortran 2003+, F#, and Python)

Page 30: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-30

x

Static parent of scope X

Page 31: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Copyright © 2012 Addison-Wesley. All rights reserved. 1-31

x

Static ancestors of scope x

Page 32: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

function big() {

function sub1() {var x = 7;sub2();}function sub2() {var y = x;}var x = 3;sub1( );

}

Copyright © 2012 Addison-Wesley. All rights reserved. 1-32

Javascript example

Value of x=3 not 7

Page 33: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

Ada example

procedure P isI : integer;

procedure Q is I : integer; I:= 5;

P.I:=6; end Q

end P;

Copyright © 2012 Addison-Wesley. All rights reserved. 1-33

Access outer hidden I

Page 34: Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1

C++ example

Copyright © 2012 Addison-Wesley. All rights reserved. 1-34

int x = 1;int main(){

int x = 5;while (x > 4)

{x -= ::x;}

return 0;}

Access outer hidden x