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.

Slides:



Advertisements
Similar presentations
PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04B - Arrays and records Programming Language Design.
Advertisements

Arrays and records Programming Language Design and Implementation
Programming Languages and Paradigms
Names and Bindings.
Chapter 7:: Data Types Programming Language Pragmatics
1 Introduction to Data Types (Section 7.1) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
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,
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Chapter Four Data Types Pratt 2 Data Objects A run-time grouping of one or more pieces of data in a virtual machine a container for data it can be –system.
1 Programming Languages Storage Management Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM.
Chapter 9 Imperative and object-oriented languages 1.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
G. Levine Chapter 6 Chapter 6 Encapsulation –Why do we want encapsulation? Programmer friendliness- programmer need not know about these details –Easier.
Encapsulation by Subprograms and Type Definitions
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Definitions A data type defines a collection of data.
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.
Primitive Data Types: Numbers Strings Ordinal Types Pointers
Structured Data Types and Encapsulation Mechanisms to create new data types: –Structured data Homogeneous: arrays, lists, sets, Non-homogeneous: records.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.
CS 355 – Programming Languages
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.
College of Computer Science and Engineering
1 CS 2104 Prog. Lang. Concepts Lecture 3 Dr. Abhik Roychoudhury School of Computing.
ISBN 0-321— Chapter 6 Data Types. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2007 Addison-Wesley.
1 Programming Languages Implementation of Control Structures Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM.
ISBN 0-321— Chapter 6 sections 1-4, 9 Primitive Data Types Numbers Strings Ordinal Types Pointers.
Names Variables Type Checking Strong Typing Type Compatibility 1.
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 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Elsa Gunter who’s.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
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.
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.
CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann.
Chapter 4 of Programming Languages by Ravi Sethi.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
CS 330 Programming Languages 11 / 01 / 2007 Instructor: Michael Eckmann.
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.
Names, Scope, and Bindings Programming Languages and Paradigms.
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
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
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
Lecture 16: Introduction to Data Types
CS 326 Programming Languages, Concepts and Implementation
Chapter 6 Data Types.
Types and Related Issues
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

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

2 Contents Fundamentals Elementary data types Structured data types Subprograms

3 Fundamentals Data objects Data types Type specification and implementation Declaration Type checking, compatibility, and conversion Assignment and initialisation

4 Data Objects A run-time grouping of one or more pieces of data A container for data values Block of storage Programmer-defined and system-defined Lifetime Variables and constants

5 Fundamentals Data objects Data types Type specification and implementation Declaration Type checking, compatibility, and conversion Assignment and initialisation

6 Data Types A class of objects having the same properties Primitive data types

7 Fundamentals Data objects Data types Type specification and implementation Declaration Type checking, compatibility, and conversion Assignment and initialisation

8 Specification Attributes Values Operations: op_name: arg_type x arg_type x …  result_type x result_type x …

9 Examples: Arrays Attributes:  number of dimensions  subscript range for each dimension  data types of the components Values: valid values for the components Operations: subscripting, creating, accessing attributes

10 Implementation Storage representation Operation definitions:  hardware operations  subprograms  in-line codes

11 Fundamentals Data objects Data types Type specification and implementation Declaration Type checking, compatibility, and conversion Assignment and initialisation

12 Declaration To provide information about data objects:  number and type  name and position  lifetime  constant and initialised value

13 Declaration Data type declaration:  by name var A: integer  by specification var A: array [1..20] of integer

14 Declaration Operation declaration:  argument number, order, data types  result number, order, data types function FOO(X: integer; Y: real) : real; FOO: integer x real  real

15 Declaration Purposes:  choice of storage representations  storage management  generic operations  type checking

16 Fundamentals Data objects Data types Type specification and implementation Declaration Type checking, compatibility, and conversion Assignment and initialisation

17 Type Checking Checking if each operation receives the proper number of arguments of the proper data types Dynamic (run time) vs. static (compile time) type checking

18 Type Checking Dynamic type checking:  no declarations required  types of data objects may change as needed  programmers have no concerns about data types  difficult to debug and remove type errors  extra storage required for keeping type information  reduction of execution speed

19 Type Compatibility T 1 and T 2 are compatible if data objects of type T 1 can occur in the positions of data objects of type T 2, and vice versa: name or structural equivalence type VECT1: array [1..10] of real; VECT2: array [1..10] of real; var X, Y: VECT1; Z: VECT2; Y := X; Z := Y;

20 Type Compatibility Name equivalence:  no anonymous types allowed  global type definitions used var X: array [1..10] of real;

21 Type Compatibility Structural equivalence:  difficult to define  static type checking compromised  cost to check type METERS = integer; LITERS = integer; var LEN: METERS; VOL: LITERS; LEN + VOL

22 Type Conversion When types are mismatched:  type error  type conversion (coercion) var A: integer;int I; B, C: real;unsigned U; float F; C := A + B I = - 1; U = 1; F = U  I; /* I = - 1  */

23 Fundamentals Data objects Data types Type specification and implementation Declaration Type checking, compatibility, and conversion Assignment and initialisation

24 Assignment Basic operation for changing values of data objects Specification: := : type 1 x type 2  void= : type 1 x type 2  type 3 Z := X + YZ = X + (Y = W  2) A = B = C

25 Initialisation To set a value in the storage of an data object Un-initialised data object Explicit and implicit initialisation

26 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

27 Contents Fundamentals Elementary data types Structured data types Subprograms

28 Elementary Data Types Numeric data types Enumerations Booleans Characters

29 Numeric Data Types Integers Subranges Floating-point real numbers Fixed-point real numbers Complex numbers Rational numbers

30 Integers 2 bytes 4 bytes   65536

31 Subranges var A: Smaller storage requirements  fewer bits than a general integer  software-simulated operations Better type checking

32 Floating-Point Real Numbers exponent mantissa Sign bit for mantissa Sign bit for exponent = = x 2 3 = x

33 Fixed-Point Real Numbers integer partfractional part Sign bit =

34 Complex Numbers real part imaginary part

35 Rational Numbers To avoid roundoff and truncation Pairs of integers of unbounded length Sign bit numerator denominator

36 Elementary Data Types Numeric data types Enumerations Booleans Characters

37 Enumerations type DEGREE = (bachelor, master, doctor) 012

38 Elementary Data Types Numeric data types Enumerations Booleans Characters

39 Booleans A particular bit Zero/non-zero value 1 byte

40 Elementary Data Types Numeric data types Enumerations Booleans Characters

41 Characters 1 byte 2 byte

42 Contents Fundamentals Elementary data types Structured data types Subprograms

43 Structured Data Types Fundamentals Vectors and arrays Records Character strings Pointers Sets Files

44 Fundamentals Specification Implementation Type checking

45 Specification Attributes:  Number of components  Type of each component  Names for selecting components  Maximum number of components  Organization of components

46 Specification Operations:  Component selection (random/sequential)  Whole-data-structure operations  Insertion/deletion  Creation/destruction

47 Implementation Storage representation: Decriptor Component Decriptor Component SequentialLinked

48 Implementation Selection operation:  Sequential  base-address-plus-offset  Linked  pointer following

49 Implementation Storage management: Garbage Dangling References object

50 Type Checking var A: array [1..10] of real; Existence of a selected component: A[I] Type of a selected component: A[2].link .item

51 Structured Data Types Fundamentals Vectors and arrays Records Character strings Pointers Sets Files

52 Vectors Vector LB UB Integer E Base address  Component size A[LB] A[LB + 1] A[UB] var A: array [1..10] of integer; loc A[I] =  + D + (I - LB) x E packed/unpacked storage

53 Matrix var A: array [1..2, -1..1] of real; row-major ordercolumn-major order

54 Multidimensional Arrays var A: array [LB 1..UB 1, LB 2..UB 2, …, LB n..UB n ] of real; Row-major order: var A: array [LB 1..UB 1 ] of array [LB 2..UB 2, …, LB n..UB n ] of real; Column-major order: var A: array [LB n..UB n ] of array [LB 1..UB 1, …, LB n-1..Ub n-1 ] of real;

55 Matrix LB 1 UB 1 LB 2 UB 2 Real E Base address  Component size A[1, -1] A[1, 0] var A: array [1..2, -1..1] of real; loc A[I, J] =  + D + (I - LB 1 ) x S + (J - LB 2 ) x E S = (UB 2 - LB 2 + 1) x E loc A[I, J] =  + K + I x S + J x E K = D - LB 1 x S - LB 2 x E A[1, 1] A[2, -1] A[2, 0] A[2, 1]

56 Structured Data Types Fundamentals Vectors and arrays Records Character strings Pointers Sets Files

57 Records Heterogeneous components Symbolic names for components var EMPLOYEE: record ID: integer; AGE: integer; DEPT: string; SALARY: real; end

58 Records Base address  ID loc R.I =  +  j=1, I-1 (size of R.j) 27 “IT” AGE DEPT SALARY computed during compile time

59 Variant Records type PAY_TYPE = (MONTHLY, HOURLY) var EMPLOYEE: record ID: integer; AGE: integer; DEPT: string; case PAY_CLASS: PAY_TYPE of MONTHLY: (MONTH_RATE: real; START_DATE: integer); HOURLY: (HOUR_RATE: real; REG_HOURS: integer; OVERTIME: integer) end

60 Variant Records ID 27 “IT” AGE DEPT PAY_CLASS HOUR_RATE REG_HOURS OVERTIME ID AGE DEPT PAY_CLASS MONTH_RATE START_DATE Unused

61 Variant Records Dynamic tag checking No checking

62 Structured Data Types Fundamentals Vectors and arrays Records Character strings Pointers Sets Files

63 Character Strings RELA TIVI TY Fixed declared length:

64 Character Strings 128EI NSTE IN Variable length with declared bound: Maximum length Current length

65 Character Strings Unbounded length: Current length 8 IESN ETNI

66 Character Strings Concatenation Relational operations Substring selection (by character-position/pattern matching) Input-output formatting

67 Structured Data Types Fundamentals Vectors and arrays Records Character strings Pointers Sets Files

68 Pointers Referencing data objects of a single type type VECT = array [1..20] of real; var P:  VECT; Referencing data objects of any type

69 Pointers Object d 0 d d 0 d Absolute addressRelative address

70 Pointers Absolute address:  fast access  difficult storage management Relative address:  slower access  simpler storage management

71 Pointers List: type ELEMENT = record HEAD: integer; TAIL:  ELEMENT end var P:  ELEMENT P

72 Structured Data Types Fundamentals Vectors and arrays Records Character strings Pointers Sets Files

73 Sets Membership test Element insertion/deletion Union, intersection, difference

74 Sets var S: set of (mon, tue, wed, thu, fri, sat, sun); Bit-string representation: montuewedthufrisatsun [mon, wed, fri] [mon, tue, wed] [mon, wed]

75 Sets Hash-coded representation:  for large domain size  each set is represented by a storage block  each element has a hash address in that block

76 Structured Data Types Fundamentals Vectors and arrays Records Character strings Pointers Sets Files

77 Files Represented on a secondary storage device (disk/tape) Much larger than data structures of other types Lifetime is longer than that of the program creating it

78 Files Sequential files Text files Direct-access files Indexed sequential files

79 Files Open Read Write End-of-file test

80 Contents Fundamentals Elementary data types Structured data types Subprograms

81 Subprograms Abstract operations:  name  arguments  results  action Data objects

82 Subprograms function FOO(X: real; Y: integer): real; var A: array [1..10] of real; N: integer; begin … N := Y + 1; X := A[N]  2; … end;

83 Subprograms Code segment (static part) Activation record (dynamic part):  parameters  function results  local variables

84 Subprograms Prologue FOO Code segmentActivation record Epilogue Statement executable codes Return point and system data X Y A N

85 Subprograms Code segment Activation record 1 Activation record 2 Activation record N 1st call2nd callN-th call

86 Exercises Consider the following piece of program: program P; var A: 1..10; N: integer; begin … read(N); A := N + 1; … end;  Where is type checking?  Can it be done statically?  Can it be done dynamically and how?

87 Exercises Illustrate the storage representation of A: array [0..1, 1..2] of integer using the column-major order. Give the accessing formula for computing the location of A[I, J], supposing that the size of an integer is 2 bytes.