Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.

Slides:



Advertisements
Similar presentations
Names and Bindings.
Advertisements

Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
ICE1341 Programming Languages Spring 2005 Lecture #9 Lecture #9 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Names, Bindings, Type Checking, and Scopes
Names, Bindings, Type Checking, and Scopes
ISBN Chapter 6 Data Types: Structured types.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
ISBN Lecture 05 Variable Attributes.
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
Elementary Data Types Scalar Data Types Numerical Data Types Other
CS 355 – Programming Languages
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
ISBN 0-321— Chapter 6 sections 1-4, 9 Primitive Data Types Numbers Strings Ordinal Types Pointers.
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?
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.
1 Data Types In Text: Chapter 5. 2 Chapter 5: Data Types Outline What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
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 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
ISBN Chapter 6 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Chapter 6 Topics Introduction Primitive Data.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
1 CS Programming Languages Class 08 September 19, 2000.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved. 6-2 Chapter 6 Topics Introduction Primitive Data Types.
1 Data Types Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types Union Types Pointer Types.
Chapter 6 © 2002 by Addison Wesley Longman, Inc Introduction - Evolution of Data Types: FORTRAN I (1957) - INTEGER, REAL, arrays … Ada (1983) -
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
ISBN Variables, Names, Scope and Lifetime ICOM 4036 Lecture 9.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.2 Primitive Data Types Almost all programming languages.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 5 Evolution of Data Types: FORTRAN I (1956) - INTEGER, REAL, arrays … Ada (1983) - User can.
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 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.
Chapter 6 – Data Types CSCE 343.
Chapter 6 Data Types.
6.1 Introduction 6.2 Primitive Data Types - Evolution of Data Types:
Type Checking, and Scopes
Chapter 6: Data Types Lectures # 10.
Structure of Programming Languages
Instructor : Ahmed Alalawi Slides from Chung –Ta King
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Concepts of Programming Languages
Chapter 6 Data Types.
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Data Types In Text: Chapter 6.
Arithmetic Expressions
Complex Data Types One very important measure of the “goodness” of a PL is the capability of its data types to model the problem space variables Design.
Names, Bindings, Type Checking, and Scopes
Names, Bindings, Type Checking, and Scopes
Names and Binding In Text: Chapter 5.
Chapter 6 Data Types.
Names, Bindings, Type Checking, and scopes
Types and Related Issues
Chapter 6 Data Types.
Presentation transcript:

Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the operands of an operator are of compatible types 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. This automatic conversion is called coercion A type error is the application of an operator to an operand of an inappropriate type If all type bindings are static, nearly all type checking can be static If type bindings are dynamic, type checking must be dynamic A programming language is strongly typed if type errors are always detected

Strongly Typed Languages? FORTRAN 77 is not strongly typed Parameters, EQUIVALENCE C and C++ are not Parameter type checking can be avoided Unions are not type checked Pascal is not Variant records Modula-2 is not Ada is, almost UNCHECKED CONVERSION is a loophole Coercion rules strongly affect strong typing; they can weaken it considerably (C++ versus Ada) Advantage of strong typing Allows the detection of the misuse of variables that result in type errors

Type Compatibility Type compatibility by name means that the two variables have compatible types if they are in either the same declaration or in declarations that use the same type name Easy to implement but highly restrictive Sub ranges of integer types are not compatible with integer types

Data Types Design Issues What is the syntax of references to variables? What operations are defined and how are they specified?

Primitive Data Types Not defined in terms of other data types Integer Almost always an exact reflection of the hardware, so the mapping is trivial There may be as many as eight different integer types in a language

Floating Point Models real numbers, but only as approximations Languages for scientific use support at least two floating-point types; sometimes more Usually exactly like the hardware, but not always; some languages allow accuracy specs in code e.g. (Ada) type SPEED is digits 7 range 0.0..1000.0; type VOLTAGE is delta 0.1 range -12.0..24.0;

Decimal For business applications (money) Store a fixed number of decimal digits Advantage Accuracy Disadvantages Limited range, wastes memory

Boolean Could be implemented as bits, but often as bytes Advantage Readability

Character String Types Values are sequences of characters Design issues: Is it a primitive type or just a special kind of array? Is the length of objects static or dynamic? Operations: Assignment Comparison (=, >, etc.) Concatenation Substring reference Pattern matching

Examples Pascal Ada, FORTRAN 77, FORTRAN 90 and BASIC C and C++ Not primitive; assignment and comparisons supported only Ada, FORTRAN 77, FORTRAN 90 and BASIC Somewhat primitive Assignment, comparison, catenation Substring reference C and C++ Not primitive Use char arrays and a library of functions that provide these operations SNOBOL4 (a string manipulation language) Primitive Many operations, including elaborate pattern matching Java; string class (not arrays of char)

Ordinal Types (user defined) An ordinal type is one in which the range of possible values can be easily associated with the set of positive integers Enumeration Types - one in which the user enumerates all of the possible values, which are symbolic constants Design Issue Should a symbolic constant be allowed to be in more than one type definition?

Examples Pascal Ada C and C++ Cannot reuse constants They can be used for array subscripts e.g. for variables, case selectors NO input or output Can be compared Ada Constants can be reused (overloaded literals) Disambiguates with context or type_name CAN be input and output C and C++ Like Pascal, except they can be input and output as integers Java does not include an enumeration types C# includes them

Evaluation (of enumeration types) Useful for readability e.g. no need to code a color as a number Useful for reliability e.g. compiler can check operations and ranges of values

Subrange Type An ordered contiguous subsequence of an ordinal type Pascal Subrange types behave as their parent types; can be used as for variables and array indices E.g. type pos = 0 .. MAXINT; Ada Subtypes are not new types, they are just constrained existing types (so they are compatible) Can be used as in Pascal, plus case constants E.g. subtype POS_TYPE is INTEGER range 0 ..INTEGER'LAST;

Evaluation (of enumeration types) Aid to readability Reliability - restricted ranges add error detection

Implementation of user-defined ordinal types Enumeration types are implemented as integers Subrange types are the parent types with code inserted (by the compiler) to restrict assignments to subrange variables

Arrays An array is an aggregate of homogeneous data elements in which an individual element is identified by its position in the aggregate, relative to the first element Design Issues What types are legal for subscripts? Are subscripting expressions in element references range checked? When are subscript ranges bound? When does allocation take place? What is the maximum number of subscripts? Can array objects be initialized?

Subscript Types FORTRAN, C, C++ Pascal Ada Java int only Any ordinal type (int, boolean, char, enum) Ada int or enum (includes boolean and char) Java integer types only

Four Categories of Arrays (based on subscript binding and binding to storage) Static - range of subscripts and storage bindings are static e.g. FORTRAN 77, some arrays in Ada Advantage: execution efficiency (no allocation or de-allocation) Fixed stack dynamic - range of subscripts is statically bound, but storage is bound at elaboration time e.g. C local arrays are not static Advantage: space efficiency

STUFF : array (1..N) of FLOAT; begin ... end; Stack-dynamic - range and storage are dynamic, but fixed from then on for the variable’s lifetime e.g. Ada declare blocks declare STUFF : array (1..N) of FLOAT; begin ... end; Advantage: flexibility - size need not be known until the array is about to be used

4. Heap-dynamic - subscript range and storage bindings are dynamic and not fixed e.g. (FORTRAN 90) - In Java, all arrays and objects are heap-dynamic