1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Chapter 6 Intermediate Code Generation
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Chapter 6 - Data Types Programming Languages:
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 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,
Type Checking.
Compiler Construction
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Disjoint Unions (quick review)
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.
Composite types Cartesian products –tuples, records, structures disjoint unions –union, discriminated or variant records mappings –arrays, functions recursive.
Mark Hennessy CS351 Dept Computer Science NUI Maynooth 1 Types CS351 – Programming Paradigms.
Types A type consists of –a set of values –a set of operations on those values Types can be –primitive (atomic, non-decomposable) –composite (includes.
1) Enumerated types 2) Records and structures 3) Accessing fields in records/structures 4) Operations on records/structures 5) Variant records 6) Dynamic.
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 Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Type Checking and Data Type Implementation (Sections )
Names Variables Type Checking Strong Typing Type Compatibility 1.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Data Types Lecture 7 – Data Types, Spring CSE3302 Programming Languages, UT-Arlington.
Compiler Principle and Technology Prof. Dongming LU Apr. 4th, 2014.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
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.
Programming Languages Third Edition Chapter 8 Data Types.
Pascal Programming Pointers and Dynamic Variables.
Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
Announcements Quiz this Thursday 1. Multi dimensional arrays A student got a warning when compiling code like: int foo(char **a) { } int main() { char.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Data Types (1) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
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.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Records type city is record -- Ada Name: String (1..10); Country : String (1..20); Population: integer; Capital : Boolean; end record; struct city { --
Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record.
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
CSE 3302 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.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 2: Data Abstraction 2
Records Design Issues: 1. What is the form of references?
Semantic Analysis Chapter 6.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Semantic Analysis Chapter 6.
Compiler Construction
Course Overview PART I: overview material PART II: inside a compiler
Chapter 6 Part 1.
Compiler Construction
CSE 3302 Programming Languages
Presentation transcript:

1 COMP313A Programming Languages Data Types (2)

2 Overview Type Constructors Type Equivalence Type Checking Type Conversion

3 Constructors – Mapping (arrays) The array constructor defines mappings as data aggregates Mapping from an array index to the value stored in that position in the array The domain is the values of the index The range is the values stored in the array

4 Constructors Mapping (arrays) C/C++ typedef int little_people_num[3]; little_people_num gollum_city = {0, 0, 0} gollum_city[3] = 5 typedef int matrix[10][20];

5 Constructors – Mapping (arrays)… Pascal ADA Type intarray = array[2..5] of integer; little_people = (dwarf, elf, goblin); little_people_num = array[little_people] of integer; intmatrix = array[1..10, 1..20] of integer; x:array(INTEGER range 2.6) of INTEGER := (0,2,0,5,-33)

6 Constructors Union Cartesian products – conjunction of fields Union – disjunction of fields Discriminated or undiscriminated

7 Constructors Unions Undiscriminated C/C++ typedef union { short int offset; long unsigned int absolute; } address;

8 Constructors Union… Discriminated typedef struct { address location; descriptor kind; } safe_address; enum descriptor{rel, abs}; safe_address an_address; if (an_address == rel) an_address.location.offset = 255;

9 Pascal Variant Record type address_range = 0..maxint; address_type = (absolute, offset); safe_address = record case kind: address_type of absolute: (abs_addr:address_range); offset: (off_addr: integer); end

10 Constructors Pointer and Recursive Types Recursion used to define aggregates whose size can grow arbitrarily Recursive datatype T is defined as a structure that can contain components of type T Implemented via pointers

11 Constructors Pointer and Recursive Types… Linked list of integers typedef struct { int val; int_list* next; } int_list; int-list* head C/C++Ada type int_list_node; type int_list_ref is access node int_list_node; type int_list_node is record val: integer; next: int_list_ref; end; head: int_list_ref

12 Constructors Pointer and Recursive Types Lacks ……. typedef struct { int val; int_list* next; } int_list; int-list* head

13 Constructors Pointer and Recursive Types Think of the the different possibilities for an int_list as: {emptylist} U int U (int X int) U (int X int X int)…

14 Constructors Pointer and Recursive Types Some languages (Pascal, Ada) require pointers to be typed PL/1 treated pointers as untyped data objects What is the significance of this for a type checker? C pointers are typed but C allows arithmetic operations on them unlike Pascal and Ada

15 Type Equivalence When are two types the same Structural equivalence Declaration equivalence Name equivalence

16 Structural Equivalence Two types are the same if they have the same structure i.e. they are constructed in exactly the same way using the same type constructors from the same simple types

17 Structural Equivalence typedef int A[5] typedef A B; typedef int C[5]; typedef int D[10];

18 Structural Type Equivalence typedef int anarray[10]; typedef struct { anarray x; int y;} struct1; typedef struct { int x[10]; int y; }struct2; typedef int anarray[10]; typedef struct { anarray a; int b; }struct3; typedef int anarray[10]; typedef struct { int b; anarray a; }struct4; (Note we are just using the syntax of C as an example. C does NOT use structural equivalence for structs

19 Structural Equivalence Check representing types as trees –Check equivalence recursively on subtrees Consider… Dynamic arrays Type array1 = array[-1..9] of integer; array2 = array[0..10] of integer; Array (INTEGER range <>) of INTEGER