PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ04B - Arrays and records Programming Language Design.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Arrays and records Programming Language Design and Implementation
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
Chapter 7:: Data Types Programming Language Pragmatics
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
CS-240 Data Structures in C Arrays Dick Steflik. Abstract Data Type A collection of pairs where index is an ordered set of integers and are values of.
PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10A - Heap storage Programming Language Design and.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
ISBN Chapter 6 Data Types: Structured types.
G. Levine Chapter 6 Chapter 6 Encapsulation –Why do we want encapsulation? Programmer friendliness- programmer need not know about these details –Easier.
PZ02A - Language translation
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
Elementary Data Types Scalar Data Types Numerical Data Types Other
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05B - Type equality Programming Language Design and.
Chapter 8 Arrays and Strings
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
1 Programming Languages Implementation of Data Structures Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM.
1 CS 2104 Prog. Lang. Concepts Lecture 3 Dr. Abhik Roychoudhury School of Computing.
Names Variables Type Checking Strong Typing Type Compatibility 1.
Chapter 8 Arrays and Strings
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06C - Polymorphism Programming Language Design and.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
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.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
INTRODUCTION OF ARRAY. Topics To Be Discussed………………………. Introduction Types of array One Dimensional Array Internal representation of one-dimensional array.
Two dimensional arrays A two dimensional m x n array A is a collection of m. n elements such that each element is specified by a pair of integers (such.
Arrays (Chapter 5)‏ Definition Applications One-Dimensional –Declaration –Initialization –Use Multidimensional.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Chapter 6 – Data Types CSCE 343.
Type Checking, and Scopes
PZ06C - Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3 PZ06C.
PZ09A - Activation records
PZ04A - Scalar and composite data
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Polymorphism Programming Language Design and Implementation
Course Overview PART I: overview material PART II: inside a compiler
Chapter 6 Part 1.
Arrays and records Programming Language Design and Implementation
Arrays and records Programming Language Design and Implementation
Polymorphism Programming Language Design and Implementation
Arrays and records Programming Language Design and Implementation
Type equality Programming Language Design and Implementation
Type equality Programming Language Design and Implementation
Arrays and records Programming Language Design and Implementation
Type equality Programming Language Design and Implementation
Polymorphism Programming Language Design and Implementation
Type equality Programming Language Design and Implementation
Type equality Programming Language Design and Implementation
PZ05B - Type equality Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 6.4 PZ05B.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Polymorphism Programming Language Design and Implementation
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Structures, Unions, and Enumerations
Presentation transcript:

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04B - Arrays and records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 6.1

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Array accessing An array is an ordered sequence of identical objects. The ordering is determined by a scalar data object (usually integer or enumeration data). This value is called the subscript or index, and written as A[I] for array A and subscript I. Multidimensional arrays have more than one subscript. A 2-dimensional array can be modeled as the boxes on a rectangular grid. The L-value for array element A[I,J]is given by the accessing formula on the next slide

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall,

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Array accessing (continued) Rewriting access equation: L-value(A[I,J]) =  - d1*L1 - d2*L2 +I*d1 + J*d2 Set I = 0; J= 0; L-value(A[0,0]) =  - d1*L1 - d2*L2 +0*d1 + 0*d2 L-value(A[0,0]) =  - d1*L1 - d2*L2, which is a constant. Call this constant the virtual origin (VO); It represents the address of the 0th element of the array. L-value(A[I,J]) = VO +I*d1 + J*d2 To access an array element, typically use a dope vector:

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Array accessing summary To create arrays: 1. Allocate total storage beginning at  : (U2-L2+1)*(U1-L1+1)*eltsize 2. d2 = eltsize 3. d1 = (U2-L2+1)*d2 4. VO =  - L1*d1 - L2*d2 5. To access A[I,J]: Lvalue(A[I,J]) = VO + I*d1 + J*d2 This works for 1, 2 or more dimensions. May not require runtime dope vector if all values are known at compile time. (e.g., in Pascal, d1, d2, and VO can be computed by compiler.) Next slide: Storage representation for 2-dimensional array.

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall,

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Array example Given following array: var A: array [7..12, ] of real; Give dope vector if array stored beginning at location 500. d2 = 4 (real data) d1 = ( ) * 4 = 3 * 4 = 12 VO = * * 4 = = 360 L-value(A[I,J]) = * I + 4* J 1. VO can be a positive or negative value, and can have an address that is before, within, or after the actual storage for the array: 2. In C, VO =  since bounds start at 0. Example: char A[25] L-value(A[I]) = VO + (I-L1) * d1 =  + I * 1 =  + I

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Slices

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Array slices Given array : A[L1:U1, L2:U2]: Give d1, d2, and VO for vector: Dope vector A[I,*] = B[L2:U2] VO = L-value(A[I,L2]) - d2*L2 M1 = eltsize = d2 Dope vector A[*,J] = B[L1:U1] VO = L-value(A[L1,J]) - d1*L1 M1 = rowsize = d1 Create new dope vector that accesses original data

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, More on slices Diagonal slices: VO = L-value(A[L1,L2]) - d1*L1 - d2*L2 M1 = d1 + d2 Other possibilities:

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Associative arrays Access information by name without having a predefined ordering or enumeration: Example: Names and grades for students in a class: NAME[I] = name of Ith student GRADE[I] = Grade for Ith student Associative array: Use Name as index: CLASS[name] will be grade. Problem: Do not know enumeration before obtaining data so dope vector method of accessing will not work. Implemented in Perl and in SNOBOL4 (as a table)

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Perl example %ClassList = (“Michelle”, `A', “Doris”, `B', “Michael”, `D'); # % operator makes an associative array $ClassList{‘Michelle’} has the value = %ClassList # Converts ClassList to an enumeration # array with index 0..5 $I= 0 $y[$I] = Doris $I= 1 $y[$I] = B $I= 2 $y[$I] = Michael $I= 3 $y[$I] = D $I= 4 $y[$I] = Michelle $I= 5 $y[$I] = A

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Structs in C Representation: a sequence of objects: record { A: object; B: object; C: object }

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Union types typedef union { int X; float Y; char Z[4];} B; B P; Similar to records, except all have overlapping (same) L-value. But problems can occur. What happens below? P.X = 142; printf(“%O\n”, P.Z[3]) All 3 data objects have same L-value and occupy same storage. No enforcement of type checking.  Poor language design

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Variant records type PayType=(Salaried, Hourly); var Employee:record ID: integer; Dept: array[1..3] of char; Age: integer; case PayClass: PayType of Salaried:(MonthlyRate:real; StartDate:integer); Hourly:(HourRate:real; Reg:integer; Overtime:integer) end

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Variant records (continued) Tagged union type - Pascal variant records type whichtype = (inttype, realtype, chartype); type uniontype = record case V: whichtype of inttype: (X: integer); realtype: (Y: real); chartype: (Z: char4); Assumes string of length 4 end But can still subvert tagging: var P: uniontype P.V = inttype; P.X = 142; P.V = chartype; What is P.V value now?