Programming Languages and Design Lecture 6 Names, Scopes and Binding Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.

Slides:



Advertisements
Similar presentations
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Advertisements

Programming Languages and Paradigms
Names and Bindings.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Chapter 10 Storage Management Implementation details beyond programmer’s control Storage/CPU time trade-off Binding times to storage.
Run-Time Storage Organization
Run time vs. Compile time
The Concept of Variables
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Names, Bindings, and Scopes
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
COP4020 Programming Languages
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture.
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Binding Time and Storage Allocation (Section ) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
1 Chapter 5 Names Bindings Type Checking Scope. 2 High-Level Programming Languages Two main goals:Two main goals: –Machine independence –Ease of programming.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Compiler Construction
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter 3 :: Names, Scopes, and Bindings Michael L. Scott School of Computer & Information Engineering, Sangji University Kwangman.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
Basic Semantics Associating meaning with language entities.
Runtime Organization.
ISBN Chapter 5 Names, Bindings, and Scopes.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
COMP3190: Principle of Programming Languages
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Robert van Engelen.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Names, Scope, and Bindings Programming Languages and Paradigms.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Chapter 5 Names, Bindings, Type Checking CSCE 343.
Names and Attributes Names are a key programming language feature
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Type Checking, and Scopes
Names, Bindings, and Scopes
Names, Scopes, and Bindings: Scopes
Names, Binding, and Scope
COP4020 Programming Languages
Binding Times Binding is an association between two things Examples:
Programming Languages
Names and Binding In Text: Chapter 5.
Name Binding and Object Lifetimes
Names, Bindings, and Scopes
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Presentation transcript:

Programming Languages and Design Lecture 6 Names, Scopes and Binding Instructor: Li Ma Department of Computer Science Texas Southern University, Houston February, 2008

2 Review of Previous Lectures Introduction to programming languages Syntax specifications of programming languages Semantic specifications of programming languages Functional Programming  Scheme Object-Oriented Programming  C++ & Java

3 Today’s Lecture Names, binding, and scopes  Static vs. Dynamic References  “Programming Language Pragmatics”, Michael Scott, Chapter 3 (§3.1 – 3.4)  “Python Reference Manual”, §4.1 (  “Scope (programming) – Wikipedia, the free encyclopedia”, (

4 Name is Abstraction Names enable programmers to refer to variables, constants, operations, and types using identifier names Names are control abstractions and data abstractions for program fragments and data structures  Control abstraction: Subroutines (procedures and functions) allow programmers to focus on manageable subset of program text Subroutine interface hides implementation details  Data abstraction: Object-oriented classes hide data representation details behind a set of operations

5 Binding A binding is an association between a name and an entity/a value Depending on the binding time, there are static binding and dynamic binding Binding time is the time at which an implementation decision is made to create a binding

6 Binding Time Language design time  the design of specific program constructs (syntax), primitive types, and meaning (semantics) Language implementation time  fixation of implementation constants such as numeric precision, run-time memory sizes, max identifier name length, number and types of built-in exceptions, etc. Program writing time  the programmer's choice of algorithms and data structures

7 Binding Time (cont’) Compile time  the time of translation of high-level constructs to machine code and choice of memory layout for data objects Link time  the time at which multiple object codes (machine code files) and libraries are combined into one executable Load time  when the operating system loads the executable in memory Run time  when a program executes

8 Object and Binding Lifetimes Creation of object  Beginning of the object lifetime Creation of binding  Beginning of the binding lifetime Use of bindings  The object is manipulated via its binding Deactivation/reactivation of bindings  Binding may be out-of-scope, so invisible Destruction of bindings  End of the binding lifetime Destruction of objects  End of the object lifetime

9 Object and Binding Lifetimes (cont’) Binding lifetime: time between creation and destruction of binding to object  a pointer variable is set to the address of an object  a formal argument is bound to an actual argument Object lifetime: time between creation and destruction of an object

10 Deallocation Errors in Bindings Bindings are temporarily invisible when code is executed where the binding (name  object) is out of scope Memory leak: object never destroyed (binding to object may have been destroyed, rendering access impossible) Dangling reference: object destroyed before binding is destroyed Garbage collection prevents these allocation/deallocation problems

11 Object Storage Objects (program data and code) have to be stored in memory during their lifetime Static objects have an absolute storage address that is retained throughout the execution of the program  Global variables and data  Subroutine code and class method code Stack objects are allocated in last-in first-out order, usually in conjunction with subroutine calls and returns  Actual arguments passed by value to a subroutine  Local variables of a subroutine Heap objects may be allocated and deallocated at arbitrary times, but require an expensive storage management algorithm  Lisp lists  Java class instances are always stored on the heap

12 Storage Allocation – Static Program code is statically allocated in most implementations of imperative languages Statically allocated variables are history sensitive  Global variables keep state during entire program lifetime  Static local variables in C functions keep state across function invocations  Static data members are “shared” by objects and keep state during program lifetime Advantage of statically allocated object is the fast access due to absolute addressing of the object  Problem: static allocation of local variables cannot be used for recursive subroutines: each new function instantiation needs fresh locals

13 Storage Allocation – Stack Each instance of a subroutine that is active has a subroutine frame (sometimes called activation record) on the run-time stack  Compiler generates subroutine calling sequence to setup frame, call the routine, and to destroy the frame afterwards  Method invocation works the same way, but in addition methods are typically dynamically bound Subroutine frame layouts vary between languages, implementations, and machine platforms

14 Storage Allocation – Heap Implicit heap allocation  Done automatically  Java class instances are placed on the heap  Scripting languages and functional languages make extensive use of the heap for storing objects  Some procedural languages allow array declarations with run- time dependent array size  Resizable character strings Explicit heap allocation  Statements and/or functions for allocation and deallocation  Malloc/free, new/delete

15 Scope Scope is the textual region of a program in which a name-to-object binding is active Statically scoped language: the scope of bindings is determined at compile time  Used by almost all but a few programming languages  More intuitive to user compared to dynamic scoping Dynamically scoped language: the scope of bindings is determined at run time  Used in Lisp (early versions), APL, Snobol, and Perl (selectively)

16 Static vs. Dynamic Scoping (define x 1) (define (bar) x) (define (foo f) (define x 2) (f)) (foo bar) Static scope: x = 1 Dynamic scope: x = 2

17 Static Scoping The bindings between names and objects can be determined by examination of the program text Scope rules of a program language define the scope of variables and subroutines  A variable always refers to its nearest enclosing binding – closest nested scope rule Look for a declaration in the current innermost scope If there is none, look for a declaration in the immediately surrounding scope, etc.

18 Static Scope Implementation with Static Links Scope rules are designed so that we can only refer to variables that are alive: the variable must have been stored in the frame of a subroutine If a variable is not in the local scope, we are sure there is a frame for the surrounding scope somewhere below on the stack:  The current subroutine can only be called when it was visible  The current subroutine is visible only when the surrounding scope is active Each frame on the stack contains a static link pointing to the frame of the static parent

19 Example Static Links Subroutines C and D are declared nested in B  B is static parent of C and D B and E are nested in A  A is static parent of B and E The fp points to the frame at the top of the stack to access locals The static link in the frame points to the frame of the static parent C static link D static link E static link B static link A Stack frames fp A calls B A calls E B calls C B calls D Calls A B E C D

20 Static Chains How do we access non-local objects? The static links form a static chain, which is a linked list of static parent frames When a subroutine at nesting level j has a reference to an object declared in a static parent at the surrounding scope nested at level k, then j-k static links forms a static chain that is traversed to get to the frame containing the object The compiler generates code to make these traversals over frames to reach non-local objects

21 Example Static Chains Subroutine A is at nesting level 1 and C at nesting level 3 When C accesses an object of A, 2 static links are traversed to get to A's frame that contains that object C static link D static link E static link B static link A Stack frames fp A calls B A calls E B calls C B calls D Calls A B E C D

22 Out of Scope Non-local objects can be hidden by local name- to-object bindings and the scope is said to have a hole in which the non-local binding is temporarily inactive but not destroyed Some languages, notably Ada and C++ use qualifiers or scope resolution operators to access non-local objects that are hidden  P1.X in Ada to access variable X of P1 and ::X to access global variable X in C++

23 Out of Scope Example P2 is nested in P1 P1 has a local variable X P2 has a local variable X that hides X in P1 When P2 is called, no extra code is executed to inactivate the binding of X to P1 procedure P1; var X:real; procedure P2; var X:integer begin... (* X of P1 is hidden *) end; begin... end

24 Dynamic Scoping Scope rule: the "current" binding for a given name is the one encountered most recently during execution With dynamic scope:  Name-to-object bindings cannot be determined by a compiler in general  Easy for interpreter to look up name-to-object binding in a stack of declarations Generally considered to be "a bad programming language feature"  Hard to keep track of active bindings when reading a program text  Most languages are now compiled, or a compiler/interpreter mix Sometimes useful:  Unix environment variables have dynamic scope