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

Slides:



Advertisements
Similar presentations
Names, Bindings, Type Checking, and Scopes
Advertisements

Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case.
Programming Languages and Paradigms
Names and Bindings.
Chapter 5 Names, Bindings, and Scopes
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Names, Bindings, Type Checking, and Scopes
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
CS 330 Programming Languages 10 / 18 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
Names, Bindings, Type Checking, and Scopes
ISBN Lecture 05 Variable Attributes.
The Concept of Variables
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
Names and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
Names, Bindings, and Scopes
Scope.
Names, Bindings, and Scopes
Software II: Principles of Programming Languages Lecture 5 – Names, Bindings, and Scopes.
Names, Bindings, Type Checking, and Scopes
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 5 © 2002 by Addison Wesley Longman, Inc Names - We discuss all user-defined names here - Design issues for names: - Maximum length? - Are.
COMP4730/2003/lec5/H.Melikian Names, Bindings,Type Checking and Scopes (Chapter 5) - Design issues: - Maximum length? - Are connector characters allowed?
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
1 CS Programming Languages Class 07 September 14, 2000.
Names Variables Type Checking Strong Typing Type Compatibility 1.
Names, Bindings, Type Checking, and Scopes
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Chapter 5 Names, Bindings, and Scopes. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 5 Topics Introduction Names Variables The Concept.
ISBN Chapter 5 Names, Bindings, and Scopes.
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
ISBN Chapter 5 Names, Bindings, and Scopes.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
ICOM 4036: PROGRAMMING LANGUAGES Lecture ? Naming and Scopes.
Names, Bindings, and Scope Session 3 Course : T Programming Language Concept Year : February 2011.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names and Binding In Text: Chapter 4.
ISBN Variables, Names, Scope and Lifetime ICOM 4036 Lecture 9.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Scope, and Bindings Programming Languages and Paradigms.
Names, Bindings, Type Checking and Scopes. Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Equivalence.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
5.2 Names - We discuss all user-defined names here
5.2 Names - We discuss all user-defined names here
Type Checking, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, and Scopes
Names, Bindings, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, Type Checking, and Scopes
Names, Bindings, Type Checking, and Scopes
Names and Binding In Text: Chapter 5.
Names, Bindings, and Scopes
Presentation transcript:

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

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

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)= * 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);

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

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

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

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++;... }... }

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

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

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

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 = Example: Pi = void example ( ) { int[ ] intList = new int[100]; String[ ] strList = new String[100]; for (index = 0; index < 100; index++) {... } Average = sum / 100;

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