Type Checking, and Scopes

Slides:



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

Names and Bindings.
Chapter 5 Names, Bindings, and Scopes
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.
Names, Bindings, and Scopes
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.
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
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.
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.
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.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 9 Lecturer: Dr. Emad Nabil 1-1.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
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
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.
Structure of Programming Languages
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
Names, Bindings, Type Checking, and scopes
Presentation transcript:

Type Checking, and Scopes Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 5 Outline Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope Scope and Lifetime Referencing Environments Named Constants Type Checking Strong Typing Type Compatibility Scope Static Scope Blocks Evaluation of Static Scoping Dynamic Scope Evaluation of Dynamic Scoping Scope and Lifetime Referencing Environments Named Constants

Names Definition: A name is a string of characters used to identify some entity in a program Design Issues: Maximum Length (Allowed/Significant) Non-Alphanumerics (Alphabet) Case sensitivity (Alphabet) Special words (Use Restriction)

Variables A variable is an abstraction of a memory cell. Practically, it is a sextuple of attributes: name address type value lifetime scope

Variables The sextuple of attributes can be considered to be a triple of pairs: name address type value lifetime scope

Variables The sextuple of attributes can be considered to be a pair of triples: name address type value lifetime scope

Variables The pair of triples is also useful when considering the L-value and R-value : name address = L-value type value = R-value lifetime scope

The Concept of Binding Definition: A binding is an association, such as between an attribute and an entity, or between an operation and a symbol Definition: Binding time is the time at which a binding takes place. Possible binding times: 1. Language design time 2. Language implementation time 3. Compile time 4. Load time 5. Runtime

The Four Categories of Variables (by Lifetimes) Static Bound to memory cells before execution begins and remains bound to the same memory cell throughout execution. Stack- dynamic Storage bindings are created for variables when their declaration statements are elaborated. Explicit heap- Allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution. Implicit Allocation and deallocation caused by assignment statements.

Type Checking Definition: Type checking is the activity of verifying and enforcing data type constraints Operands of an operator must be compatible with the operator and with each other Type checking may occur either at compile-time (static type checking), or at run-time (dynamic type checking). Definition: A type error is a violation of type constraints, frequently the application of an operator to an operand of an inappropriate type

Type Checking If a language enforces type rules strongly (that is, generally allowing only those automatic type conversions which do not lose information), one can refer to the process as strongly typed, if not, as weakly typed. Definition: A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted, by compiler-generated code, to a legal type (coercion)

Strong Typing Definition: A programming language is strongly typed if each name in a program has a single data type associated with it, and that type is known at compile time Advantages: This allows detection of type errors.

Strong Typing Language Examples FORTRAN 95, C, C++ not strongly typed Ada, Java, C# almost strongly typed Pascal most strongly typed Coercion rules greatly affect strong typing: They can weaken it considerably C++ versus Ada Although Java has just half the assignment coercions of C++, its strong typing is still far less effective than that of Ada

Type Compatibility by Name Type Compatibility by Structure There are two different kinds of type compatibility: Type Compatibility by Name Type Compatibility by Structure

The Two Kinds of Type Compatibility Definition: Two variables are type compatible by name if they are either in the same declaration or in declarations that use the same type name - Easy to implement but highly restrictive Two variables are type compatible by structure if their types have identical structures - More flexible, but harder to implement

Type Compatibility by Structure Consider the problem of two structured types: What if they are circularly defined? Are two record types compatible if they are structurally the same but use different field names? Are two array types compatible if they have the same cardinality but the subscripts are different? (e.g. [1..10] and [-5..4]) Are two enumeration types compatible if their components are spelled differently?

Furlongs per fortnight Type Compatibility With structural type compatibility, you cannot differentiate between types of the same structure Example: different units of speed Most speed units are stored as float Miles per hour Feet per second Meters per second Inches per minute Furlongs per fortnight

A,B: array (1..10) of INTEGER; Type Compatibility Language examples C: structure, except for struct Ada: restricted form of name Derived types allow types with the same structure to be different Anonymous types are all unique, even in: A,B: array (1..10) of INTEGER; C++: name for everything except objects Java, C++, C# - object compatibility – Chapter 12

Scope Static Scope Blocks Evaluation of Static Scoping Dynamic Scope Evaluation of Dynamic Scoping

Scope Definitions The scope of a variable is the range of statements over which it is visible The nonlocal variables of a program unit are those that are visible but not declared there

Static Scope Based on program text Created by nested subprograms and/or blocks To connect a name reference to a variable, find the declaration Enclosing static scopes (to a specific scope) are called its static ancestors The nearest static ancestor is the static parent

Scope Hiding Variables can be hidden from a unit by having a "closer" variable with the same name Both C++ and Ada allow access to these "hidden" variables In Ada: unit.name In C++: class_name::name

Blocks A method of creating static scopes inside program units First used in ALGOL 60 Examples: C, C++, Java, C#: for(...){ int index; ... } Ada: declare LCL:FLOAT; begin end

Assume MAIN calls A and B Static Scope Example Assume MAIN calls A and B A calls C and D B calls A and E

Assume MAIN calls A and B Static Scope Example Assume MAIN calls A and B A calls C and D B calls A and E

Static Scope Example

Static Scope Example

Evaluation of Static Scoping Suppose the spec is changed: now D must access some data in B Solutions 1. Put D in B C can no longer call D D cannot access A's variables 2. Move the data from B that D needs to MAIN but then all procedures can access them Same problem for procedure access! Evaluation static scoping often encourages many globals

Dynamic Scope Based on calling sequences of program units, not their textual layout (temporal versus spatial) Example: Ada Big - declaration of x Sub1 ... call Sub2 Sub2 - reference to x call Sub1 Big calls Sub1 Sub1 calls Sub2 Sub2 uses x Evaluation Advantage convenience Disadvantage poor readability

Scope and Lifetime Related or Unrelated? Scope and lifetime are sometimes closely related, but are different concepts!! Consider a static variable in a C or C++ function

Referencing Environments Definition: The referencing environment of a statement is the collection of all names that are visible in the statement In a static-scoped language, it is the local variables, plus all visible variables in all enclosing scopes In a dynamic-scoped language, in all active subprograms A subprogram is active if its execution has begun but has not yet terminated

Named Constants Definition: a variable that is bound to a value only when it is bound to storage Used to parameterize programs The binding of values to named constants can be either static (called manifest constants) or dynamic Advantages: readability and modifiability Examples Pascal literals only FORTRAN 95 constant-valued expressions Ada, C++, Java expressions of any kind

Variable Initialization Definition The binding of a variable to a value at the time it is bound to storage is called initialization Examples Ada: SUM:FLOAT := 0.0; Java: int sum = 0;

Summary Case sensitivity and the relationship of names to special words represent design issues of names Variables are characterized by the sextuples: { name, address, value, type, lifetime, scope } Binding is the association of attributes with program entities Scalar variables are categorized as: 1) static 2) stack dynamic 3) explicit heap dynamic 4) implicit heap dynamic Strong typing means detecting all type errors