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

Slides:



Advertisements
Similar presentations
Chapt.2 Machine Architecture Impact of languages –Support – faster, more secure Primitive Operations –e.g. nested subroutine calls »Subroutines implemented.
Advertisements

COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Programming Languages and Paradigms
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Names and Bindings.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
COSC 120 Computer Programming
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Run time vs. Compile time
The Concept of Variables
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.
Names and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
OOP Languages: Java vs C++
Overview. Copyright © 2006 The McGraw-Hill Companies, Inc. Chapter 1 Overview A good programming language is a conceptual universe for thinking about.
Programming Languages and Paradigms Object-Oriented Programming.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
COP4020 Programming Languages
Imperative Programming
Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture.
COP4020 Programming Languages
Hello World 2 What does all that mean?.
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.
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.
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
Basic Semantics Associating meaning with language entities.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Programming Languages and Paradigms Imperative Programming.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
COMP3190: Principle of Programming Languages
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
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.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Robert van Engelen.
Programming Languages and Design Lecture 6 Names, Scopes and Binding Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
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.
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.
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
Programming Languages Salihu Ibrahim Dasuki (PhD) CSC102 INTRODUCTION TO COMPUTER SCIENCE.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
CS 326 Programming Languages, Concepts and Implementation
Names, Binding, and Scope
Binding Times Binding is an association between two things Examples:
Programming Languages
Name Binding and Object Lifetimes
Identifiers & Lifetimes
Presentation transcript:

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

COP4020 Spring /4/2016 Overview Abstractions and names Binding time Object and binding lifetime

COP4020 Spring /4/2016 Name High level languages provide abstraction relative to the assembly languages  Abstraction: high level language features separate from details of computer architecture (machine independence). A name is a mnemonic character string used to represent something else.  Names are essential in high-level languages for supporting abstraction.  Names enable programmers to refer to variables, constants, operations, and types instead of low level concepts such as memory address.

COP4020 Spring /4/2016 Name and abstraction Names enable control abstractions and data abstractions in high level languages  Control abstraction: Subroutines (procedures and functions) allow programmers to focus on manageable subset of program text, subroutine interface hides implementation details Control flow constructs (if-then, while, for, return) hide low-level machine ops  Data abstraction: Object-oriented classes hide data representation details behind a set of operations  Enhances a level of machine-independence

Name related issues Name: a mnemonic character string used to represent something else Related issues:  Binding time: A binding is an association between a name and an entity. When is a name bound to the object it represents? Some binding is done at language design time (design decision).  The lifetime of object and lifetime of binding determine the storage mechanisms for the objectives  Scoping rules: the region that a binding a used.  Alias: multiple names are bound to the same object COP4020 Spring /4/2016

COP4020 Spring /4/2016 Binding Time Binding time is the time at which the binding between a name and an object is created. Depending on the names, binding may happen in many different times. Potential binding time includes:  Language design time: the design of specific language constructs. Example: The primitive data type in C++ include int, char, float, double.  Language implementation time: fixation of implementation constants. Example: C++ float point type uses IEEE 754 standard.  Program writing time: the programmer's choice of algorithms and data structures Example: a routine is called foo().

COP4020 Spring /4/2016 Binding Time Potential binding time includes:  Compile time: the time of translation of high-level constructs to machine code and choice of memory layout for data objects. Example: translate “for(i=0; i<100; i++) a[i] = 1.0;”?  Link time: the time at which multiple object codes (machine code files) and libraries are combined into one executable Which cout routine to use? /usr/lib/libc.a or /usr/lib/libc.so?  Load time: when the operating system loads the executable in memory In an older OS, the binding between a global variable and the physical memory location is determined at load time.  Run time: when a program executes Binding between the value of a variable to the variable.

COP4020 Spring /4/2016 More binding time examples Language design:  Syntax (names  grammar) if (a>0) b:=a; (C syntax style) if a>0 then b:=a end if (Ada syntax style)  Keywords (names  builtins) class (C++ and Java), endif or end if (Fortran, space insignificant)  Reserved words (names  special constructs) main (C), writeln (Pascal)  Meaning of operators (operator  operation) + (add), % (mod), ** (power)  Built-in primitive types (type name  type) float, short, int, long, string Language implementation  Internal representation of types and literals (type  byte encoding) 3.1 (IEEE 754) and "foo bar” (\0 terminated or embedded string length)  Storage allocation method for variables (static/stack/heap)

COP4020 Spring /4/2016 The Effect of Binding Time Early binding times (before run time) are associated with greater efficiency and clarity of program code  Compilers make implementation decisions at compile time (avoiding to generate code that makes the decision at run time)  Syntax and static semantics checking is performed only once at compile time and does not impose any run-time overheads Late binding times (at run time) are associated with greater flexibility (but may leave programmers sometimes guessing what’s going on)  Interpreters allow programs to be extended at run time  Languages such as Smalltalk-80 with polymorphic types allow variable names to refer to objects of multiple types at run time  Method binding in object-oriented languages must be late to support dynamic binding

COP4020 Spring /4/2016 Binding Lifetime versus Object Lifetime Key events in object lifetime:  Object creation  Creation of bindings  The object is manipulated via its binding  Deactivation and reactivation of (temporarily invisible) bindings  Destruction of bindings  Destruction of objects Binding lifetime: time between creation and destruction of binding to object  Example: a pointer variable is set to the address of an object  Example: a formal argument is bound to an actual argument Object lifetime: time between creation and destruction of an object

COP4020 Spring /4/2016 Binding Lifetime versus Object Lifetime (cont’d) Bindings are temporarily invisible when code is executed where the binding (name  object) is out of scope

COP4020 Spring /4/2016 A C++ Example { SomeClass* myobject = new SomeClass;... { OtherClass myobject;... // the myobject name is bound to other object... }... // myobject binding is visible again... myobject->action() // myobject in action(): // the name is not in scope // but object is bound to ‘this’ delete myobject; return; }

COP4020 Spring /4/2016 Problems with object/binding lifetime mismatch Memory leak: the binding is destroyed while the object is not, making it noway to access/delete the object { SomeClass* myobject = new SomeClass; myobject->action(); return; }

Problems with object/binding lifetime mismatch Dangling reference: object destroyed before binding is destroyed … myobject = new SomeClass; foo(myobject); Foo(SomeClass *a) { …… delete (myobject); // my objective is a global variable a->action(); } COP4020 Spring /4/2016

Problems with object/binding lifetime mismatch Dangling reference: object destroyed before binding is destroyed char * ptr; // a global variable Foo() { char buff[1000]; cin >> buff; ptr = strtok(buff, “ “); …… } main() { … foo(); cout << ptr; …… } COP4020 Spring /4/2016

Summary Names are esstential in high level languages to support abstraction. Supporting names involve many issues Binding time: the time when the binding between a name and an objective is created. Potential binding times Lifetime of objects and bindings. COP4020 Spring /4/2016