ISBN 0-321-19362-8 Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.

Slides:



Advertisements
Similar presentations
Names and Bindings.
Advertisements

1 Chapter 6 Data Types What is a data type? A set of values versus A set of values + set of operations on those values.
1 Chapter 6 Data Types What is a data type? A set of values versus A set of values + set of operations on those values.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
ISBN Lecture 06 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Lecture 06 Topics Introduction Primitive Data.
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.
CSE 452: Programming Languages Data Types. 2 Organization of Programming Languages-Cheng (Fall 2004) Where are we? High-level Programming Languages Logic.
CS 330 Programming Languages 11 / 08 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Definitions A data type defines a collection of data.
1 Chapter 6: Data types A data type defines a collection of data objects and a set of predefined operations on those objects Definition: A descriptor is.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
Structured Data Types and Encapsulation Mechanisms to create new data types: –Structured data Homogeneous: arrays, lists, sets, Non-homogeneous: records.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
CS 355 – Programming Languages
ISBN 0-321— Chapter 6 Data Types. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
College of Computer Science and Engineering
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Associative Arrays Record Types Tuple Types List Types Union Types.
ISBN 0-321— Chapter 6 Data Types. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2007 Addison-Wesley.
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.
6-1 Chapter 6: Data Types Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types.
ISBN 0-321— Chapter 6 Structured Data Types Arrays Associated Arrays Records Unions.
COMP4730/2002/lec6/H.Melikian Data Types Primitive Data Types User_Defined Ordinal Types Array Types Record types Union Types Set Types Pointer Types.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN Chapter 6 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Chapter 6 Topics Introduction Primitive Data.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
Structure of Programming Language Data Types. 2 A data type defines a collection of data objects and a set of predefined operations on those objects An.
C H A P T E R S I X Data Types.
CS 363 Comparative Programming Languages Data Types.
1 CS Programming Languages Class 08 September 19, 2000.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
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.
Data Types W E E K F O U R. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character String.
CSE 452: Programming Languages
Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.
Chapter 6 © 2002 by Addison Wesley Longman, Inc Introduction - Evolution of Data Types: FORTRAN I (1957) - INTEGER, REAL, arrays … Ada (1983) -
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.2 Primitive Data Types Almost all programming languages.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 5 Evolution of Data Types: FORTRAN I (1956) - INTEGER, REAL, arrays … Ada (1983) - User can.
Chapter 6 Chapter 6 Data Types. Data Types  A data type defines  a collection of data objects, and  a set of predefined operations on the objects type:
ISBN 0-321— Chapter 6 Data Types. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
Structure of Programming Language Data Types. 2 A data type defines a collection of data objects and a set of predefined operations on those objects An.
Data Types Chapter 6: Data Types Lectures # 12. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record.
6.1 Introduction 6.2 Primitive Data Types - Evolution of Data Types:
Data Types In Text: Chapter 6.
Dr. Vamsi Paruchuri University of Central Arkansas
Chapter 6 – Data Types CSCE 343.
CMP 339/692 Programming Languages Day 14 Tuesday, March 20, 2012
Chapter 6 Data Types.
Concepts of Programming Languages
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
6.1 Introduction 6.2 Primitive Data Types - Evolution of Data Types:
Structure of Programming Language
Records Design Issues: 1. What is the form of references?
Chapter 6 Data Types.
Data Types In Text: Chapter 6.
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.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types Introduction:
Chapter 6 Data Types.
Presentation transcript:

ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Structured data Types A structured data type is defined in terms of other types A structured type is usually composed of multiple elements. –In homogeneous types, all elements have the same type –In heterogeneous types, elements may have different types.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-3 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.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-4 Arrays Design Issues: 1. What types are legal for subscripts? 2. Are subscripting expressions in element references range checked? 3. When are subscript ranges bound? 4. When does allocation take place? 5. What is the maximum number of subscripts? 6. Can array objects be initialized? 7. Are any kind of slices allowed?

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-5 Arrays Indexing is a mapping from indices to elements map(array_name, index_value_list)  an element Index Syntax –FORTRAN, PL/I, Ada use parentheses –Most other languages use brackets

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-6 Arrays Subscript Types: –FORTRAN, C - integer only –Pascal - any ordinal type (integer, boolean, char, enum) –Ada - integer or enum (includes boolean and char) –Java - integer types only

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-7 Array Bindings Categories of arrays (based on subscript binding and binding to storage) 1.Static 2.Fixed stack-dynamic 3.Stack-dynamic 4.Heap-dynamic

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-8 Static Arrays Range of subscripts and storage bindings are static –Both can be done by the compiler –FORTRAN 77, some arrays in Ada, static arrays in C –Advantage: execution efficiency (no allocation or deallocation)

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-9 Fixed stack dynamic Arrays The range of subscripts is statically bound, but storage is bound at elaboration time –e.g. Most Java locals, and C locals that are not static –Advantage: space efficiency –Using stack memory means the space can be reused when array lifetime ends

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-10 Stack-dynamic Arrays 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 Fixed Heap-Dynamic arrays are similar but use heap memory.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-11 Heap-dynamic Arrays Subscript range and storage bindings are dynamic and not fixed –e.g. (FORTRAN 90) INTEGER, ALLOCATABLE, ARRAY (:,:) :: MAT (Declares MAT to be a dynamic 2-dim array) ALLOCATE (MAT (10,NUMBER_OF_COLS)) (Allocates MAT to have 10 rows and NUMBER_OF_COLS columns) DEALLOCATE MAT (Deallocates MAT ’s storage)

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-12 Heap-dynamic Arrays (continued) –In APL, Perl, and JavaScript, arrays grow and shrink as needed –In C and C++, you can create heap-dynamic arrays using pointers. –In Java, all arrays are objects (heap-dynamic) –C# provides both heap-dynamic and fixed-heap dynamic

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-13 Array Attributes Number of subscripts –FORTRAN I allowed up to three –FORTRAN 77 allows up to seven –Others - no limit Array Initialization –Usually just a list of values that are put in the array in the order in which the array elements are stored in memory

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-14 Array Initialization Examples of array initialization: 1. FORTRAN - uses the DATA statement, or put the values in /... / on the declaration 2. Java, C and C++ - put the values in braces; can let the compiler count them e.g. int stuff [] = {2, 4, 6, 8}; 3. For strings (which are treated as arrays in C and C++), an alternate form of initialization is provided. Char* names[] = {"Bob", "Mary", "Joe"};

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-15 Array Initialization Examples of array initialization: 3. Ada - positions for the values can be specified e.g. SCORE : array (1..14, 1..2) := (1 => (24, 10), 2 => (10, 7), 3 =>(12, 30), others => (0, 0)); 4. Pascal does not allow array initialization

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-16 Array Attributes Array Operations 1. APL - many, see book (p ) 2. Ada –Assignment; RHS can be an aggregate constant or an array name –Catenation; for all single-dimensioned arrays –Relational operators (= and /= only) 3. FORTRAN 90 –Intrinsics (subprograms) for a wide variety of array operations (e.g., matrix multiplication, vector dot product) –Elementals (+) act combine corresponding elements from two arrays

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-17 Array Slices A slice is some substructure of an array – Nothing more than a referencing mechanism –A way of designating a part of the array Slices are only useful in languages for operations that can be done on a whole array

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-18 Array Slices 1. FORTRAN 90 INTEGER MAT (1:4, 1:4) MAT(1:4, 1) - the first column MAT(2, 1:4) - the second row

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-19 Example Slices in FORTRAN 90

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-20 Array Slices 2. Ada - single-dimensioned arrays only LIST(4..10) 3. Java has something like slices for multi- dimensioned arrays int [][]array = array[1] gets the second row

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-21 Memory for arrays For 1D arrays, have a contiguous block of memory with equal amount of space for each element Two approaches for multi-dimensional arrays –Single block of contiguous memory for all elements Arrays must be rectangular Address of array is starting memory location –Implement as arrays of arrays (Java) Jagged arrays are possible Array variable is a pointer (reference)

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-22 Contiguous Array Memory Row major (by rows) or column major order (by columns) for 2D array Access function maps subscript expressions to an address in the array

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-23 Locating an Element

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-24 Array Descriptors Single-dimensioned array Multi-dimensional array

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-25 Associative Arrays An associative array is an unordered collection of data elements that are indexed by an equal number of values called keys –A hash table has the same behavior Design Issues: 1. What is the form of references to elements? 2. Is the size static or dynamic?

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-26 Associative Arrays Structure and Operations in Perl –Names begin with % –Literals are delimited by parentheses e.g., %hi_temps = ("Monday" => 77, "Tuesday" => 79,…); –Subscripting is done using braces and keys e.g., $hi_temps{"Wednesday"} = 83; –Elements can be removed with delete e.g., delete $hi_temps{"Tuesday"};

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-27 Records A record is a possibly heterogeneous aggregate of data elements in which the individual elements are identified by names Design Issues: 1. What is the form of references? 2. What unit operations are defined?

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-28 Records Record Definition Syntax –COBOL uses level numbers to show nested records; others use recursive definition Record Field References 1. COBOL field_name OF record_name_1 OF... OF record_name_n 2. Others (dot notation) record_name_1.record_name_2.... record_name_n.field_name

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-29 Records Fully qualified references must include all record names Elliptical references allow leaving out record names as long as the reference is unambiguous (Cobol only) Pascal provides a with clause to abbreviate references

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-30 Record Descriptors A compile-time descriptor for a record

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-31 Record Operations 1. Assignment –Pascal, Ada, and C allow it if the types are identical –In Ada, the RHS can be an aggregate constant 2. Initialization –Allowed in Ada, using an aggregate constant

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-32 Record Operations (continued) 3. Comparison –In Ada, = and /=; one operand can be an aggregate constant 4. MOVE CORRESPONDING –In COBOL - it moves all fields in the source record to fields with the same names in the destination record

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-33 Comparing Records and Arrays 1. Access to array elements is much slower than access to record fields, because subscripts are dynamic (field names are static) 2. Dynamic subscripts could be used with record field access, but it would disallow type checking and it would be much slower

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-34 Unions A union is a type whose variables are allowed to store different type values at different times during execution Design Issues for unions: 1. What kind of type checking, if any, must be done? 2. Should unions be integrated with records?

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-35 Union Examples 1.FORTRAN - with EQUIVALENCE EQUIVALENCE (A, B, C, D), (X(1), Y(1)) –No type checking 2. Pascal - both discriminated and nondiscriminated unions e.g. type intreal = record tagg : Boolean of true : (blint : integer); false : (blreal : real); end; –Problem with Pascal’s design: type checking is ineffective

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-36 Unions A discriminated union of three shape variables

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-37 Pascal Unions Pascal’s unions cannot be type checked effectively: a. User can create inconsistent unions (because the tag can be individually assigned) var blurb : intreal; x : real; blurb.tagg := true; { it is an integer } blurb.blint := 47; { ok } blurb.tagg := false; { it is a real } x := blurb.blreal; { assigns an integer to real }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-38 Pascal Unions b. The tag is optional! –Now, only the declaration and the second and last assignments are required to cause trouble

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-39 Ada Unions Ada has discriminated unions Reasons they are safer than Pascal: a. Tag must be present b. It is impossible for the user to create an inconsistent union (because tag cannot be assigned by itself--All assignments to the union must include the tag value, because they are aggregate values)

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-40 Unions in C, C++ and Java C and C++ have free unions (no tags) –Not part of their records –No type checking of references Java has neither records nor unions

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-41 Evaluation Unions are unsafe in most languages –not Ada Not really necessary in most systems today –Cheap memory –Virtual memory

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-42 Sets A set is a type whose variables can store unordered collections of distinct values from some ordinal type Design Issue: –What is the maximum number of elements in any set base type?

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-43 Other Structured Types Some languages provide lists –LISP Classes are structured types with methods attached to them (more later)