Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1."— Presentation transcript:

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

2 Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 5 Topics IntroductionNamesVariables The Concept of Binding Scope Scope and Lifetime Referencing Environments Named Constants

3 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 value Binding symbol operation Binding

4 Copyright © 2012 Addison-Wesley. All rights reserved.1-4 Possible Binding Times 1.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

5 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

6 Bindingvalue type storage Copyright © 2012 Addison-Wesley. All rights reserved.1-6 static Dynamic

7 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

8 Static and Dynamic Binding Static BindingDynamic 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

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

10 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

11 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

12 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

13 Implicit Declaration (continued) 1-13 F# example: let a = 1 let b = 100u let str = "text" F# example: let a = 1 let b = 100u let str = "text" Type are inferred from value’s from

14 Type binding Static Explicit type declaration Implicit type declaration dynamic visual basic 9.0+, ml, haskell, f#, C# and go

15 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

16 1-16 Dynamic Type Binding 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 method MyMethod(myDynamic); // Passing as parameter

17 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

18 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.Name2.Address3.Value4.Type 5.Lifetime6.Scope

19 Categories of Variables by Lifetimes Static variables Stack dynamic variables Explicit Heap Dynamic Variables Implicit Heap Dynamic Variables Copyright © 2012 Addison-Wesley. All rights reserved.1-19 Binding to storage

20 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 Static Compilation time Storage binning Static “load time” Stored in Data segment

21 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 Static Compilation time Storage binning When Elaboration “execution reach a stmt” Dynamic Binding Stored in stack Ex: in C++ int x=5;

22 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.

23 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

24 1-24 4. Implicit heap-dynamic variables The 6 attributes including Type, storage, value are bound every time meeting assignment

25 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

26 Value bindingType bindingStorage binding Static variables Stack dynamic variables Explicit heap dynamic variables Implicit heap dynamic variables Copyright © 2012 Addison-Wesley. All rights reserved.1-26

27 Copyright © 2012 Addison-Wesley. All rights reserved.1-27 Chapter 5 Topics IntroductionNamesVariables The Concept of Binding Scope Scope and Lifetime Referencing Environments Named Constants

28 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

29 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)

30 Copyright © 2012 Addison-Wesley. All rights reserved.1-30 x Static parent of scope X

31 Copyright © 2012 Addison-Wesley. All rights reserved.1-31 x Static ancestors of scope x

32 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

33 Ada example procedure P is I : 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

34 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


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

Similar presentations


Ads by Google