Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.

Similar presentations


Presentation on theme: "1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010."— Presentation transcript:

1 1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010

2 2 5.1 Introduction Imperative programming languages are abstractions of underlying von Neumann computer architecture Imperative programming languages are abstractions of underlying von Neumann computer architecture Two primary components: Two primary components: Memory (stores instructions and data) Memory (stores instructions and data) Processor (provides operations for modifying the contents of memory) Processor (provides operations for modifying the contents of memory) Abstractions in a language for the memory cells of the machine are variables Abstractions in a language for the memory cells of the machine are variables Characteristics of abstractions are very close to the characteristics of the cells (example: integer variable – represented in one or more bytes of memory) OR Characteristics of abstractions are very close to the characteristics of the cells (example: integer variable – represented in one or more bytes of memory) OR Abstractions are far removed from the organization of the hardware memory (example: 3-D array – requires software mapping function to support abstraction) Abstractions are far removed from the organization of the hardware memory (example: 3-D array – requires software mapping function to support abstraction)

3 3 5.2 Names Name (interchangeable with the term, identifier) Name (interchangeable with the term, identifier) String of characters used to identify some entity in a program String of characters used to identify some entity in a program Design issues Design issues Are names case sensitive? Are names case sensitive? Are the special words of the language reserved words or keywords? Are the special words of the language reserved words or keywords? Name forms Name forms A letter followed by a string of letters, digits, and underscore characters A letter followed by a string of letters, digits, and underscore characters Many languages are case sensitive Many languages are case sensitive Example: ABC != abc != AbC Example: ABC != abc != AbC Problem of writeability rather than readability Problem of writeability rather than readability

4 4 5.2 Names (cont.) Special words Special words Used to make programs more readable by naming actions to be performed Used to make programs more readable by naming actions to be performed Used to separate the syntactic parts of statements and programs Used to separate the syntactic parts of statements and programs Keyword Keyword Word of programming language that is special only in certain contexts Word of programming language that is special only in certain contexts Reserved word Reserved word Special word of a programming language that cannot be used as a name Special word of a programming language that cannot be used as a name

5 5 5.3 Variables A program variable is an abstraction of a computer memory cell (or collection of cells) A program variable is an abstraction of a computer memory cell (or collection of cells) A variable can be characterized as a sextuple of names A variable can be characterized as a sextuple of names 1. Names 2. Address 3. Value 4. Type 5. Lifetime 6. Scope Names (previously discussed) Names (previously discussed) Address Address Machine memory address with which it is associated Machine memory address with which it is associated Sometimes called “l-value” Sometimes called “l-value” Alias is when more than one variable can be used to access the same memory location Alias is when more than one variable can be used to access the same memory location

6 6 5.3 Variables (cont.) Value Value Contents of the memory cell(s) associated with the variable Contents of the memory cell(s) associated with the variable Memory cell refers to abstract memory cell Memory cell refers to abstract memory cell Variable’s value called “r-value” Variable’s value called “r-value” What is required when the variable is used on the right side on an assignment statement What is required when the variable is used on the right side on an assignment statement To access the r-value, the l-value must be determined first To access the r-value, the l-value must be determined first Type Type Determines the range of values that a variable can store and the set of operations that are defined for values of that type Determines the range of values that a variable can store and the set of operations that are defined for values of that type Lifetime Lifetime Discussed in Section 5.4 Discussed in Section 5.4 Scope Scope Discussed in Section 5.5 Discussed in Section 5.5

7 7 5.4 The Concept of Binding Binding Binding An association between an attribute and an entity or between an operation and a symbol An association between an attribute and an entity or between an operation and a symbol Can occur at language design time, language implementation time, compile time, load time, link time, or run time Can occur at language design time, language implementation time, compile time, load time, link time, or run time Binding time Binding time Time at which a binding takes place Time at which a binding takes place Binding of attributes to variables Binding of attributes to variables Static if it first occurs before run time and remains unchanged throughout program execution Static if it first occurs before run time and remains unchanged throughout program execution Dynamic if first occurs during run time or can change in the course of program execution Dynamic if first occurs during run time or can change in the course of program execution Example (Java) count = count + 5; Example (Java) count = count + 5;

8 8 Type bindings Type bindings 1. How the type is specified 2. When the binding takes place Static Type Binding Static Type Binding Explicit and implicit declarations create static bindings to types Explicit and implicit declarations create static bindings to types Explicit declaration Explicit declaration Statement in a program that lists variable names and specifies that they are a particular type Statement in a program that lists variable names and specifies that they are a particular type Implicit declaration Implicit declaration Means of associating variables with types through default conventions rather than declaration statements Means of associating variables with types through default conventions rather than declaration statements Advantages: Convenient Advantages: Convenient Disadvantages: Detrimental to readability (prevent compiler from detecting typographical/programmer errors) Disadvantages: Detrimental to readability (prevent compiler from detecting typographical/programmer errors) 5.4 The Concept of Binding (cont.)

9 9 Dynamic type binding Dynamic type binding Type of variable is not specified by a declaration statement (nor can it be determined by the spelling of its name) Type of variable is not specified by a declaration statement (nor can it be determined by the spelling of its name) Variable is bound to a type when assigned a value in the assignment statement Variable is bound to a type when assigned a value in the assignment statement Advantage: Provides more programming flexibility Example (JavaScipt) List = [ 10.2, 3.5]; List = 47; Advantage: Provides more programming flexibility Example (JavaScipt) List = [ 10.2, 3.5]; List = 47; Disadvantage: Less reliable programs, cost of implementing dynamic attribute considerable in execution time, languages that have dynamic type binding for variables are usually implemented using pure interpreters rather than compilers (takes 10x longer) Disadvantage: Less reliable programs, cost of implementing dynamic attribute considerable in execution time, languages that have dynamic type binding for variables are usually implemented using pure interpreters rather than compilers (takes 10x longer)

10 10 5.4 The Concept of Binding (cont.) Type inference Type inference Most types of expressions can be determined without requiring programmer to specify types of variables Most types of expressions can be determined without requiring programmer to specify types of variables General syntax of ML function: Fun fucntion_name(formal parameters) = expression; Fun circum(r)=3.14159 * r * r; fun times10(x) = 10 * x; fun square(x) = x * x; square(2.75); fun square(x) : real = x * x; fun square(x) : real) = x * x; fun square(x) = (x : real) * x; fun square(x) = x * ( real : x);

11 11 5.4 The Concept of Binding (cont.) Storage bindings and lifetimes Storage bindings and lifetimes Allocation Allocation Process of a memory cell (to which a variable is bound) being taken from a pool of available memory Process of a memory cell (to which a variable is bound) being taken from a pool of available memory Deallocation Deallocation Process of placing a memory cell that has been unbound back from a variable into the pool of available memory Process of placing a memory cell that has been unbound back from a variable into the pool of available memory Lifetime Lifetime Time during which the variable is bound to a specific memory location (begins when bound, ends when unbound) Time during which the variable is bound to a specific memory location (begins when bound, ends when unbound) 4 categories, based on lifetime 4 categories, based on lifetime 1. Static 2. Stack-dynamic 3. Explicit heap-dynamic 4. Implicit heap-dynamic

12 12 5.4 The Concept of Binding (cont.) Static variables Static variables Bound to memory cells before program execution begins and remain bound to same memory cells until program execution terminates Bound to memory cells before program execution begins and remain bound to same memory cells until program execution terminates Stack-dynamic variables Stack-dynamic variables Storage bindings are created when their declaration statements elaborated, but whose types are statically bound Storage bindings are created when their declaration statements elaborated, but whose types are statically bound Explicit heap-dynamic variables Explicit heap-dynamic variables Nameless (abstract) memory cells that are allocated and deallocated by explicit run-time instructions specified by programmer. These variables can only be referenced through pointer or reference variables. Nameless (abstract) memory cells that are allocated and deallocated by explicit run-time instructions specified by programmer. These variables can only be referenced through pointer or reference variables. Implicit heap-dynamic variables Implicit heap-dynamic variables Bound to heap storage only when they are assigned values, all of their attributes are bound EVERY time they are assigned Bound to heap storage only when they are assigned values, all of their attributes are bound EVERY time they are assigned

13 13 5.5 Scope Definition: Scope of a variable is the range of statements in which the variable is visible. A variable is visible in a statement if it can be referenced in that statement. Different Types of Scoping Static Scoping Dynamic Scoping Global Scoping

14 14 5.5 Scope (cont.) Scope rules determine how references to variables declared outside the currently executing subprogram or blocks are associated with their declarations. void sub( ) { int count;... while (…) { int count; count++;... }... }

15 15 5.5 Scope (cont.) Static Scoping Static Scoping The scope of a variable is determined prior to execution of the program allowing the reader and compiler to determine the type of every variable. The scope of a variable is determined prior to execution of the program allowing the reader and compiler to determine the type of every variable. Dynamic Scoping Dynamic Scoping The scope of a variable can only be determined at run time since this scoping type is based on the calling sequence of subprograms. The scope of a variable can only be determined at run time since this scoping type is based on the calling sequence of subprograms. Analyze subprograms to know the reference of a variable by determining the static parent and ancestors of the program unit.

16 16 Ada Code example to illustrate Static and Dynamic Scoping Procedure Big is nesting two procedures Sub1 and Sub2 Procedure Big is nesting two procedures Sub1 and Sub2 procedure Big is X : Integer; procedure Sub1 is X : Integer; begin ---- of Sub1... end; of ---- of Sub1 procedure Sub2 is begin ---- of Sub2... X... end; ---- of Sub2 begin ---- of Big... end; ---- of Big

17 17 5.5 Scope (cont.) Global scoping Global scoping The variable definition can occur outside the function. These definitions are global variables and can potentially be visible in a given function. The variable definition can occur outside the function. These definitions are global variables and can potentially be visible in a given function. Memory allocation differences between Definitions and Declarations of variables Memory allocation differences between Definitions and Declarations of variables Declarations Variable types and attributes but NO allocation of storage Definitions Variable types and attributes AND cause allocation of storage

18 18 PHP code example to illustrate Global Scoping $day = “Monday”; $month = “January”; Function calendar( ) { $day = “Tuesday”; global $month; print “local day is $day ”; $gday = $GLOBALS[‘day’]; print “global day is $gday ”; print “global month is $month ”; } Calendar( ); local day is Tuesday global day is Monday Global month is January

19 19 5.7 Referencing Environments Referencing environment Referencing environment Collection of all variables declared in its local scope plus the collection of variables of its ancestor scopes that are visible. Collection of all variables declared in its local scope plus the collection of variables of its ancestor scopes that are visible. procedure Example is A, B : Integer;... procedure Sub1 is X, Y : Integer; begin ---- of Sub1... < ------ X and Y of Sub1, A and B of Example end; ---- of Sub1 procedure Sub2 is X : Integer;... procedure Sub3 is X : Integer begin ---- of Sub3... < ------ X of Sub3, (X of Sub2 is hidden), A and B of Example end; ---- of Sub3

20 20 5.8 Named Constants Named constants Named constants A variable that is bound to a value only once A variable that is bound to a value only once Example: Pi = 3.14159 Example: Pi = 3.14159 void example ( ) { int[ ] intList = new int[100]; String[ ] strList = new String[100]; for (index = 0; index < 100; index++) {... } Average = sum / 100;

21 21 5.8 Named Constants (cont.) void example ( ) { final int len = 100; int[ ] intList = new int[len]; String[ ] strList = new String[100]; for (index = 0; index < len; index++) {... } Average = sum / len;

22 22 Questions What is the difference between static type binding and dynamic type binding? Provide an application for each. What is the difference between static type binding and dynamic type binding? Provide an application for each. Give an example of a potential type inference error in ML and a possible "legal" definition alternative solution. Give an example of a potential type inference error in ML and a possible "legal" definition alternative solution. Define a static variable and provide an advantage and disadvantage to using it in a program. Define a static variable and provide an advantage and disadvantage to using it in a program. Give an example of a named constant and explain how it aids in program readability and reliability. Give an example of a named constant and explain how it aids in program readability and reliability.


Download ppt "1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010."

Similar presentations


Ads by Google