CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Data Types-II: Composite Data Types.

Slides:



Advertisements
Similar presentations
Arrays and records Programming Language Design and Implementation
Advertisements

Programming Languages and Paradigms
Names and Bindings.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Chapter 7:: Data Types Programming Language Pragmatics
Copyright © 2005 Elsevier Chapter 7:: Data Types Programming Language Pragmatics Michael L. Scott.
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 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.
Chapter 9 Imperative and object-oriented languages 1.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
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.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
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.
Mark Hennessy CS351 Dept Computer Science NUI Maynooth 1 Types CS351 – Programming Paradigms.
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Elementary Data Types Scalar Data Types Numerical Data Types Other
Structured Data Types and Encapsulation Mechanisms to create new data types: –Structured data Homogeneous: arrays, lists, sets, Non-homogeneous: records.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Names and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
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.
Compiler Construction
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
Basic Semantics Associating meaning with language entities.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Copyright © 2009 Elsevier Chapter 7:: Data Types Programming Language Pragmatics Michael L. Scott.
COMP3190: Principle of Programming Languages
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.
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Arrays and Pointers Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 23.
Chapter 4 of Programming Languages by Ravi Sethi.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
Programming Languages and Design Lecture 6 Names, Scopes and Binding Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
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.
Names, Scope, and Bindings Programming Languages and Paradigms.
Copyright © 2009 Elsevier Chapter 7:: Data Types Programming Language Pragmatics Michael L. Scott.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Programming Language Theory 2011, 1 Chap. 7 :: Data Type Michael L. Scott.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
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 # 13. 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.
Data Types In Text: Chapter 6.
CS 326 Programming Languages, Concepts and Implementation
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
CS 3304 Comparative Languages
CS 326 Programming Languages, Concepts and Implementation
Chapter 7:: Data Types Programming Language Pragmatics
Chap. 7 :: Data Type Michael L. Scott.
Chapters 7 and 8:: Data Types
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Data Types-II: Composite Data Types

Agenda 1.Records and Variant Records 2.Arrays 3.Strings 4.Sets 5.Pointers And Recursive Types 6.Lists

Records Allow related data of heterogeneous types to be stored and manipulated together Usually laid out contiguously Possible holes for alignment reasons Smart compilers may rearrange fields to minimize holes (C compilers promise not to) Different terms in – Algol 68, C, C++, and Common Lisp: struct – Java, C++, C#: class – Pascal: record – ML, Python, Ruby: lists (no keyword for the declaration)

Examples In Pascal: type two_chars = packed array [1..2] of char; type element = record name: two_chars; atomic_number: integer; atomic_weight: real; metallic: Boolean end; In C: struct element { char name[2]; int atomic_number; double atomic_weight; bool metallic; };

Memory Layout of Records Likely layout in memory for objects on a 32-bit machine Alignment restrictions lead to the shaded “holes.”

Packed Records Pascal allows the programmer to specify that a record type (or an array, set, or file type) should be packed: type element = packed record name : two_chars; atomic_number : integer; atomic_weight : real; metallic : Boolean end;

Memory Layout of Packed Records Likely memory layout for packed records. The atomic_number and atomic_weight fields are nonaligned, and can only be read or written via multi- instruction sequences.

Memory Layout of Rearranged Records Rearranging record fields to minimize holes. By sorting fields according to the size of their alignment constraint, a compiler can minimize the space devoted to holes, while keeping the fields aligned.

Variant Records A variant record provides two or more alternative fields or collections of fields, only one of which is valid at any given time. type element = record name : two_chars; atomic_number : integer; atomic_weight : real; metallic : Boolean; case naturally_occurring : Boolean of true : ( source : string_ptr; prevalence : real; ); false : ( lifetime : real; ) end;

Memory Layout of Variants Likely memory layouts for element variants. The value of the naturally occurring field (shown here with a double border) determines which of the interpretations of the remaining space is valid. Type string_ptr is assumed to be represented by a (four-byte) pointer to dynamically allocated storage.

Arrays Arrays are the most common and important composite data types Unlike records, which group related fields of disparate types, arrays are usually homogeneous Semantically, they can be thought of as a mapping from an index type to a component or element type A slice or section is a rectangular portion of an array.

Arrays Array slices(sections) in Fortran90. Much like the values in the header of an enumeration- controlled loop (Section6.5.1), a: b: c in a subscript indicates positions a, a+c, a+2c,...through b. If a or b is omitted, the corresponding bound of the array is assumed. If c is omitted, 1 is assumed. It is even possible to use negative values of c in order to select positions in reverse order. The slashes in the second subscript of the lower right example delimit an explicit list of positions. second subscript of the lower right example delimit an explicit list of positions.

Arrays Dimensions, Bounds, and Allocation Global lifetime, static shape: allocate space for the array in static global memory Local lifetime, static shape: space can be allocated in the subroutine’s stack frame at run time Local lifetime, shape bound at elaboration time: an extra level of indirection is required to place the space for the array in the stack frame of its subroutine (Ada, C) Arbitrary lifetime, shape bound at elaboration time: at elaboration time either space is allocated or a preexistent reference from another array is assigned (Java, C#) Arbitrary lifetime, dynamic shape: must generally be allocated from the heap. A pointer to the array still resides in the fixed-size portion of the stack frame (if local lifetime).

Memory Layout of Arrays Arrays in most language implementations are stored in contiguous locations in memory Like Records, arrays may contain “holes” due to alignment requirement Some languages (e.g., Pascal) allow the programmer to specify that an array be packed For multidimensional arrays, there are two layouts: row-major order and column-major order – In row-major order, consecutive locations in memory hold elements that differ by one in the final subscript (except at the ends of rows). – In column-major order, consecutive locations hold elements that differ by one in the initial subscript

Row- and Column-major Layout

Strings Strings are really just arrays of characters They are often special-cased, to give them flexibility (like polymorphism or dynamic sizing) that is not available for arrays in general. – It's easier to provide these things for strings than for arrays in general because strings are one-dimensional and (more important) non- circular.

Strings In some languages, strings have special status, with operations that are not available for arrays of other sorts. – It is easier to provide special features for strings than for arrays in general, because strings are one-dimensional. – Manipulation of variable-length strings is fundamental to a huge number of computer applications. Particularly powerful string facilities are found in various scripting languages such as Perl, Python and Ruby. C, Pascal, and Ada require that the length of a string-valued variable be bound no later than elaboration time, allowing the variable to be implemented as a contiguous array of characters in the current stack frame. Lisp, Icon, ML, Java, C# allow the length of a string-valued variable to change over its lifetime, requiring that the variable be implemented as a block or chain of blocks in the heap.

Sets A set is an unordered collection of an arbitrary number of distinct values of a common type. Introduced by Pascal, and are found in many more recent languages as well. Many ways to implement sets, including arrays, hash tables, and various forms of trees. The most common implementation employs a bit vector whose length (in bits) is the number of distinct values of the base type. – Operations on bit-vector sets can make use of fast logical instructions on most machines. – Union is bit-wise or; intersection is bit-wise and; difference is bit-wise not, followed by bit-wise and.

Pointers And Recursive Types A recursive type is one whose objects may contain one or more references to other objects of the type. Pointers serve two purposes: – Efficient (and sometimes intuitive) access to elaborated objects (as in C). – Dynamic creation of linked data structures, in conjunction with a heap storage manager. In languages like C, Pascal, or Ada, which use a value model of variables, recursive types require the notion of a pointer. (Pointers aren't needed with a reference model.) In some languages (e.g., Pascal, Ada 83, and Modula-3), pointers are restricted to point only to objects in the heap.

Pointers (contd.) A dangling reference is a live pointer that no longer points to a valid object. Two sources of dangling pointers: – A pointer in a wider scope still refers to a local object of a subroutine that has returned. – the programmer reclaims an object to which pointers still refer. Two implementation mechanisms to catch dangling pointers: – Tombstones – Locks and Keys

Tombstones Tombstones are a mechanism to detect dangling pointers that can appear in certain computer programming languages, e. g. C, C++ and assembly languages, and to act as a containment to their dangerous effectsdangling pointersprogramming languages CC++assembly languages – The idea is simple: Rather than have a pointer refer to an object directly, introduce an extra level of indirection. – When an object is allocated, the language run-time system allocates a tombstone. – The pointer contains the address of the tombstone; the tombstone contains the address of the object. – When the object is reclaimed, the tombstone is modified to contain a value that cannot be a valid address.

Every pointer is a tuple consisting of an address and a key. – Every object in the heap begins with a lock – A pointer to an object in the heap is valid only if the key in the pointer matches the lock in the object. – When the run-time system allocates a new heap object, it generates a new key value – When an object is reclaimed, its lock is changed to some arbitrary value (e.g., zero) so that the keys in any remaining pointers will not match

Garbage Collection The language implementation notices when objects are no longer useful and reclaim them automatically More or less essential for functional languages – delete is a very imperative sort of operation – The ability to construct and return arbitrary objects from functions requires unlimited extent and hence heap allocation to accommodate it Popular for imperative languages as well; e.g., in Clu, Cedar, Modula-3, Java, C#, and all the major scripting languages. A typical tradeoff between convenience and safety on the one hand and performance on the other.

Lists Defined recursively as either the empty list or a pair consisting of an object (which may be either a list or an atom) and another (shorter) list Ideally suited to programming in functional and logic languages. Several scripting languages, notably Perl and Python, provide extensive list support