Chapter 5 Names, Bindings, Type Checking CSCE 343.

Slides:



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

Names and Bindings.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, Type Checking, and Scopes
Names, Bindings, Type Checking, and Scopes
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?
Names and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
Names, Bindings, and Scopes
Scope.
Software II: Principles of Programming Languages Lecture 5 – 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.
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
중요 용어 설명 ( 추후 설명이 됨 ). 2 명칭과 관련된 용어의 개념  Names  Variables  The Concept of Binding  Type Checking  Strong Typing  Type Compatibility  Scope  Scope.
1 Chapter 5 Names Bindings Type Checking Scope. 2 High-Level Programming Languages Two main goals:Two main goals: –Machine independence –Ease of programming.
1 CS Programming Languages Class 07 September 14, 2000.
Names Variables Type Checking Strong Typing Type Compatibility 1.
Names, Bindings, Type Checking, and Scopes
COME Chapter 5 Chapter 5 Variables: Names, Bindings, Type Checking and Scope.
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.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
ISBN Chapter 5 Names, Bindings, and Scopes.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
ICOM 4036: PROGRAMMING LANGUAGES Lecture ? Naming and Scopes.
CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.
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.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
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.
5.2 Names - We discuss all user-defined names here
5.2 Names - We discuss all user-defined names here
Names, Bindings, Type Checking, and Scopes
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
Structure of Programming Languages
Chapter 5 Names, Bindings, Type Checking, 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
Types and Related Issues
Presentation transcript:

Chapter 5 Names, Bindings, Type Checking CSCE 343

Storage Bindings & Lifetime Allocation: bind variable to memory cell from available pool Deallocation: putting cell back into available pool Lifetime: the time during which it is bound to cell For scalar variables there are four categories –Static –Stack dynamic –Heap dynamic –Implicit heap dynamic

Static Bound to memory cells before execution begins Remains until program ends. –Advantage: efficiency, no run time allocation and deallocation –Allows subprograms to have variables that are historically sensitive (retain value between calls) –Disadvantage: lack of flexibility (no recursion) needs more RAM since subprograms are allocated memory at compile time (e.g., two subprograms that use large local arrays)

Stack-Dynamic Storage is created and allocated when declaration statements are executed If scalar, all attributes except address are statically bound –Local variables in C and Java subprograms Advantage: recursion, conserves storage –Two functions that both have large arrays Disadvantages: –Overhead of allocation/deallocation –Suprograms are not history sensitive

Explicit Heap-Dynamic Allocated (and deallocated) by explicit directives Take effect during execution Referenced only through pointers (or references) –All objects in Java (r = new Random()) –Dynamic objects in C++ Advantage: dynamic storage management Disadvantage: inefficient, complexity of management, difficulty of using pointer/reference variables correctly

C++ //Java Explicit Heap Dynamic C++ int *intPtr; intPtr = new int; delete intPtr; Java Integer intRef; intRef = new Integer(); intRef = ???; C++ does not have automatic garbage collection

Implicit Heap-Dynamic Allocation/Deallocation caused by assignment statements –All variables in APL; strings and arrays in Perl and JavaScript myList = [14, 15, 18, 15] Advantages: flexibility Disadvantages: –Inefficient, keeping track of dynamic attributes –Loss of error detection

Type Checking Type checking: activity of ensuring that the operands of an operator are compatible type –Compatible type: Legal for the operators or Allowed under language rules to be implicitly converted (coersion) Type error: application of an operator to an inappropriate operand type –X * Y –Include assignment: x = expression –Include subprograms: foo(int x, int y) Operator foo, operands x and y

Type Checking Static type bindings: static checking Dynamic type bindings: dynamic checking Strongly typed language: –“All” type errors are detected –Advantage: allows detection of misuse of variables

Strong Typing Strongly typed languages: –C, C++ (almost; they have a union type that is not type checked) –Java (explicit casts are allowed, but caught at run-time) Coercion rules weaken strong typing –Logical errors may be missed widthFactor = (size/numPix) * 1.25 –Java has just half of the coercions of C++

Type Compatibility Type compatibility rules for primitive types are straight forward and include coercion Type compatibility rules for structures and user defined types are not so simple –Use strict compatibility called equivalence Name type equivalence: 2 vars are equivalent if they are in same declaration or use same type name in their declaration –Highly restrictive: subranges are not compatible (double, float) Structure type equivalence: 2 vars are equivalent if types have identical structures –More flexible, but harder to implement

C++ Example typedef map MapType; map myMap1; MapType myMap2; Are myMap1 and myMap2 compatible? Are they name type equivalent? Are they structure type equivalent?

Scope The scope is the range of statements over which a variable is visible (i.e., the variable can be reference). –Scope rules of a language determine how names are associated with variables. –Static scoping and dynamic scoping Non-local variables: those that are visible in a unit, but not declared there.

Static Scope Based on program text, can be determined prior to execution. Searching for variable declaration (attributes): –Start local, then look in larger enclosing scopes –Nearest enclosing scope: static parent –Enclosing scopes: static ancestors Variables hidden by same name, but “closer” –Can sometimes get access through other means: C++: class_name::name Java: this.name

Static Scoping main can call A and B A can call (B?), C and D B can call A and E MAIN E A C D B AB CDE

Static Scoping MAIN AB CDE A C B ED Intended Calls Possible Calls What if we wanted E to have access to D

Static Scoping To give E access to D –move D to same level as A and B –make some of A’s variables global so D has access Disadvantages: –Data access and desired call structure –Overall: encourages many global variables

Dynamic Scope Based on calling sequence (not code layout) and scope determined at run time –dynamic parent: immediate calling procedure –dynamic ancestor: procedure on the runtime stack Search back through chain of calls (stack) A reference to a non-local variable, x, may refer to different variables at different points in time Problems –local variables are visible to all directly or indirectly called procedures –readability –takes more time to determine binding

Scope Example Static scope for variable x in C Dynamic scope for variable x is –A calls B calls C –A calls C procedure A is x:Integer; procdedure B is x:Integer begin end; procdure C is begin … x… end; begin //A end; //A