CPSC 3740 Programming Languages University of Lethbridge. Data Types

Similar documents
COSC252: Programming Languages: Basic Semantics: Data Types. Jeremy Bolton, PhD Asst Teaching Professor

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2

Data Types In Text: Ch C apter 6 1

Chapter 6. Data Types ISBN

Organization of Programming Languages CS3200 / 5200N. Lecture 06

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Chapter 6. Data Types ISBN

Chapter 6. Structured Data Types. Topics. Structured Data Types. Vectors and Arrays. Vectors. Vectors: subscripts

Chapter 6. Data Types ISBN

CSE 452: Programming Languages. Where are we? Types: Intuitive Perspective. Data Types. High-level Programming Languages.

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction

Software II: Principles of Programming Languages

CS 230 Programming Languages

Chapter 6. Data Types. *modified by Stephanie Schwartz ISBN

Types II. Hwansoo Han

A data type defines a collection of data objects and a set of predefined operations on those objects

Chapter 6. Data Types

COS 140: Foundations of Computer Science

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

Chapter 8 :: Composite Types

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

Chapter 5. Names, Bindings, and Scopes

COS 140: Foundations of Computer Science

Lecture 13: Complex Types and Garbage Collection

Chapter 5 Names, Binding, Type Checking and Scopes

TYPES, VALUES AND DECLARATIONS

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Programming Languages Third Edition. Chapter 7 Basic Semantics

NOTE: Answer ANY FOUR of the following 6 sections:

Types. What is a type?

C# and Java. C# and Java are both modern object-oriented languages

Chapter 5 Names, Bindings, Type Checking, and Scopes

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Data Types. CSE 307 Principles of Programming Languages Stony Brook University

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Type Bindings. Static Type Binding

Name, Scope, and Binding. Outline [1]

Array Initialization. Rectangular and Jagged Arrays. Arrays Operations. ICOM 4036 Programming Languages. Data Types

G Programming Languages - Fall 2012

CSC 533: Organization of Programming Languages. Spring 2005

Fundamentals of Programming Languages

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Programming Languages

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Programming Languages, Summary CSC419; Odelia Schwartz

The SPL Programming Language Reference Manual

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

Programming Languages

CSCI312 Principles of Programming Languages!

22c:111 Programming Language Concepts. Fall Types I

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

CS558 Programming Languages

CPS 506 Comparative Programming Languages. Programming Language

IA010: Principles of Programming Languages

The PCAT Programming Language Reference Manual

CSE 307: Principles of Programming Languages

Record Types. A record is a possibly heterogeneous aggregate of data elements in which the individual elements are identified by names Design issues:

MIDTERM EXAMINATION - CS130 - Spring 2005

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

Chapter 5. Variables. Topics. Imperative Paradigm. Von Neumann Architecture

CMSC 331 Final Exam Section 0201 December 18, 2000

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Names, Scopes, and Bindings II. Hwansoo Han

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Introduce C# as Object Oriented programming language. Explain, tokens,

Concepts of Programming Languages (750321)

Chapter 3:: Names, Scopes, and Bindings

Imperative Programming

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS321 Languages and Compiler Design I. Fall 2013 Week 8: Types Andrew Tolmach Portland State University

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

Chapter 9 Subprograms

Contents of Lecture 3

Names, Bindings, Scopes

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Structuring the Data. Structuring the Data

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Computer Components. Software{ User Programs. Operating System. Hardware

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

CS558 Programming Languages

C-LANGUAGE CURRICULAM

Extending SystemVerilog Data Types to Nets

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Review of the C Programming Language for Principles of Operating Systems

CS 3360 Design and Implementation of Programming Languages. Exam 1

Binding and Variables

Transcription:

Data Types A data type defines a collection of data values and a set of predefined operations on those values Some languages allow user to define additional types Useful for error detection through type checking Also useful as a documentation tool An implementation (compiler or interpreter, plus run-time environment) needs descriptors for data types for type checking, access, allocation, deallocation, etc. Data Types 1 26 Howard Cheng

Primitive Data Types Primitive Data Types: not defined in terms of other types Numeric Types Boolean Types Character Types Data Types 2 26 Howard Cheng

Numeric Types Integer: signed vs unsigned, range/size, arbitrary length support Floating-point: Most use IEEE Floating-Point Standard 754 format: double is 1 sign bit, 11-bit exponent, 53-bit mantissa Floating-point values have both range and precision Binary Coded Decimals (each digit may take one byte or a half byte) Boolean: some languages have an Boolean type, others allow other numeric values (0 is false, nonzero true) Character types: numeric but value indicates character based on some mapping. Most common is 8-bit using ASCII, but wide unicode characters are becoming more common. Data Types 3 26 Howard Cheng

Character Strings Abstraction of sequence of characters Issues: Primitive type or a character array, or supported through external libraries? Static vs. dynamic length? Common operations: assignment individual character access concatenation substring reference comparison pattern matching Data Types 4 26 Howard Cheng

Character Strings C and C++ use character arrays (null-terminated) to store strings: unsafe because there are no bound checks C++ also have a string class Java supports String and StringBuffer (why?). C# and Ruby are similar. Python strings are immutable Pattern matching are often specified by regular expressions Data Types 5 26 Howard Cheng

Character Strings Three different options for length: Static length: length is static and set when the string is created. Suitable for immutable strings, but least flexible. Limited dynamic length: allow dynamic length up to some fixed maximum (e.g. C). Somewhat more flexible. Dynamic length: C++ string class, etc. Maximum flexibility, requires overhead. Data Types 6 26 Howard Cheng

Character Strings Implementation of descriptor: Static: type name, length, and address (compile-time) Limited Dynamic: type name, maximum length, current length, address (run-time) Dynamic: type name, current length, address (run-time) Common dynamic length string implementations: Dynamic arrays Ropes Data Types 7 26 Howard Cheng

Enumeration Types Allows a related set of constants to be grouped into a type e.g. enum in C/C++/C# Allows for type checking Issues: Is it a separate type, or are conversions to/from integers implicit? Can a constant appear in more than one type? Value and type checking Any other types that can be coerced to an enumeration type? Operations (e.g. arithmetic, bitwise?) Data Types 8 26 Howard Cheng

Arrays Homogeneous aggregate of data elements, individually identified by position. Most languages require elements to be of the same type, but can get around with pointers, inheritance, etc. Issues: Subscript types Reference range checking When are subscript ranges bound Allocation time Ragged or rectangular multi-dimensional arrays Initialization? Slices? Data Types 9 26 Howard Cheng

Array Indices/Subscripts A list of subscripts is used to identify a particular elements. Some languages use [], others use (). Possible overlap with function calls (e.g. Ada) Element type and subscript type are separate Some languages allow lower and upper bounds on array subscripts Some languages (e.g. Perl) allow negative subscripts reference from end of array Data Types 10 26 Howard Cheng

Subscript Ranges and Storage Binding The range of subscript values and storage may be determined at compile time or run time. Static: subscript ranges and storage are both bound at compile time. Most efficient, but least flexible. Fixed stack-dynamic: subscript range is statically bound, allocation done at declaration elaboration time (e.g. local fixed-size arrays). Space efficient (can be reused), require some allocation/deallocation time. Fixed heap-dynamic: both are bound at run time, but once the storage is bound the size is fixed. More flexible, but also more overhead Heap-dynamic: both are bound at run time, but they can change. Most flexible but also most overhead. Data Types 11 26 Howard Cheng

Array Operations Some languages allow operations on arrays as a whole (e.g. assignment, concatenation, comparison, etc.) Comparison can be based on object or equivalence Some languages allow slices of arrays which are subsections of the arrays. (e.g. subarray, column, row, every second element of a row). May or may not allocate extra storage. Data Types 12 26 Howard Cheng

Multidimensional Arrays Rectangular: each row must have the same number of columns Jagged: length of each row may be different Jagged arrays are usually implemented as arrays of arrays Data Types 13 26 Howard Cheng

Implementation Descriptor contains type name, element type, index type, lower and upper bounds for indices, and address. Some parts are needed at compile time, some at run time Bounds are needed only for range checks Multidimensional rectangular arrays: usually row major order (notable exception Fortran) Address calculations for multidimensional arrays Data Types 14 26 Howard Cheng

Associative Arrays Unordered collection of data elements indexed by keys In some languages associative arrays are native in the language (e.g. Python, Perl, Ruby) In some languages it is supported by standard libraries (map and unordered map in C++) Implemented by hash tables, binary trees,... Data Types 15 26 Howard Cheng

Record Types An aggregate of data elements, identified by field names Each element is accessed through offsets from the beginning of structure. Issues: how to refer to fields elliptical references? Data Types 16 26 Howard Cheng

Record Types References to individual fields usually require the name of the record and the name of the fields (. notation in C++, OF in COBOL) Nested records usually require a fully qualified reference: a.b.c.d Some languages (e.g. COBOL) allows elliptical references d OF a as long as it is unambiguous. Difficult to read Many of the modern languages are block-structured and allows for new local scopes to be created Descriptor contains type name, starting address, as well as the name, type, and offset for each field (compile time) Memory layout is straightforward Data Types 17 26 Howard Cheng

List Types Functional programming languages often have native support for lists Lists can typically be nested Common list operations: car, cdr, cons, list Data Types 18 26 Howard Cheng

Union Types A variable that may store different type values at different times during run time Allows for same memory location to be reused Free unions: no type checking performed Discriminated union: a tag is added internally to perform type checking (e.g. Ada) Space has to be allocated for the largest variant Data Types 19 26 Howard Cheng

Pointer Types Pointers store memory addresses, with a special nil value which is invalid Allow for indirect addressing and dynamic storage Can be used to access anonymous variables (e.g. those obtained from the heap) Issues: scope and lifetime lifetime of heap-dynamic variable restrictions on what type of value they can point to for dynamic storage, indirect addressing, or both? support pointers at all? Data Types 20 26 Howard Cheng

Pointer Common operations: assignment, dereferencing, allocate/deallocate C/C++: pointer arithmetic, array-pointer equivalence, void * Problems: Dangling pointers Lost heap-dynamic variables (memory leak) Removing explicit deallocation may help Data Types 21 26 Howard Cheng

Reference Types Like a pointer, but a reference type refers to an object or a value in memory No need to dereference, no possibility to change address One use: pass by reference Java does not have pointers, but have references. Need to remember that assignments do not necessarily make a new copy of an object (aliasing) Can be implemented by a pointer Safer than pointers Data Types 22 26 Howard Cheng

Pointer Implementation Represented by whatever the machine uses to address memory To solve dangling pointer problem: Tombstones: each heap-dynamic variable is itself a pointer to the real heap-dynamic variable. Locks-and-keys: each pointer has an address and a key value that is given at time of allocation and copied by assignment operators. Each heap-dynamic variable also has a lock value matching the key. Access checks to see if lock and key match. Data Types 23 26 Howard Cheng

Heap Management If all objects are of the same size, it is easier. Different sizes: may lead to fragmentation When to deallocate (implicit): Reference count: small cost for each access/allocation/deallocation, reclaim immediately. Circular structures? Garbage collection: at certain times, an algorithm marks all accessible memory locations. Inaccessible memory locations are deallocated. Run time can be unpredictable and possibly costly. Data Types 24 26 Howard Cheng

Type Checking Ensures that the operands of an operator/function are of compatible types Compatible: either legal, or can be implicitly converted according to the language specification Automatic conversion: coercion Coercion: convenience vs. error checking? Type error: application of an operator/function to an operand of an incompatible type Dynamic type checking is needed if type binding is done dynamically Difficult in some cases (e.g. union in C/C++) Data Types 25 26 Howard Cheng

Type Equivalence Type equivalend: one type can be substituted for another, without coercion Name type equivalence: only for the same declaration or same type name. Easy to check, more restrictive Structure type equivalence: if two variables have identical structures. Harder to check (e.g. circular references), more flexible (too flexible?) Some languages use a combination, including support for subtypes In C++, struct, enum and union use name type equivalence, but arrays use structural type equivalence (sort of) Data Types 26 26 Howard Cheng