CS 331, Principles of Programming Languages Chapter 4 Types: Data Representation.

Slides:



Advertisements
Similar presentations
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Advertisements

Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
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.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Data Types-II: Composite Data Types.
Chapter 7:: Data Types Programming Language Pragmatics
Copyright © 2005 Elsevier Chapter 7:: Data Types Programming Language Pragmatics Michael L. Scott.
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.
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.
Type Checking.
Compiler Construction
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;
CS 330 Programming Languages 10 / 16 / 2008 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.
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.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Pointer Data Type and Pointer Variables
Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture.
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.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
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.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 COSC3306: Programming Paradigms Lecture 2: Data Types Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
18. DECLARATIONS.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
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.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Arithmetic Expressions
1 Homework HW5 due today Review a lot of things about allocation of storage that may not have been clear when we covered them in our initial pass Introduction.
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.
Data Types Declarations Expressions Data storage C++ Basics.
Chapter 4 of Programming Languages by Ravi Sethi.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved. 6-2 Chapter 6 Topics Introduction Primitive Data Types.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
Overview: Lecture 6: Dolores Zage. What is a program? n Operations that are to be applied to certain data in a certain sequence (definition holds for.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
Data Types Chapter 6: Data Types Lectures # 12. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
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.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Dynamic Storage Allocation
Data Types In Text: Chapter 6.
Type Checking and Type Inference
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.
Lecture 16: Introduction to Data Types
CS 326 Programming Languages, Concepts and Implementation
Instructor : Ahmed Alalawi Slides from Chung –Ta King
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Introduction to Abstract Data Types
CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported
Compiler Construction
Compiler Construction
Presentation transcript:

CS 331, Principles of Programming Languages Chapter 4 Types: Data Representation

Data Representation Issues Storage Management –automatic, static Scope Internal vs. External

Type Concepts Instances of a type –individual constants, variables, expressions Basic types –built into a language, e.g. float and int in C –instances of basic types are also known as first- class objects User-defined types –defined using type expressions involving existing types

Basic and User-defined Types In most modern languages, basic types include integer, real, character, and Boolean Most PLs have two mechanisms for user- defined types, namely array and record

Internal vs. External Representations Integers in C are not the same as integers in arithmetic –word length introduces issues related to overflow e.g –so some operations don’t act as they should Floats and doubles are not the same as real numbers (or rationals!) –conversion to binary introduces small errors

Storage Classes Typically, automatic variables are associated with a given block –allocated (and initialized) when block is entered –known only within that block –freed when block is exited Compare to static variables, which are associated with a given block, but are allocated and initialized only once

User-directed Allocation In C, we have malloc and free –memory leaks can be a problem –uninitialized or invalid pointers can be problems In C++ we have constructors and destructors In Pascal and Modula, we have new() and dispose() In Lisp and Java, garbage collection is used

Data Aggregates Arrays are homogeneous –all the elements are of the same type Records (known as structs in C) are heterogeneous –components may be of different types Sets are available in languages like Pascal and Modula Type expressions are used to define these

Storage Allocation for Arrays Typically, array elements occupy a contiguous block of memory –hence C’s use of subscript 0 For multi-dimensional arrays, there are two schemes –row-major: the rightmost subscript varies fastest –column-major: the leftmost subscript varies

Records An example in C In Modula, it would be struct date { int day; int month; int year; } DoB, DoD; TYPE date = RECORD day, month, year: CARDINAL END; VAR DoB, DoD: date;

linuxbeta.gl.umbc.edu> cat -n recordeg.mod 1 MODULE recordeg; 2 3 FROM InOut IMPORT WriteLn, WriteCard; 4 5 TYPE date = RECORD 6 day, month, year: CARDINAL 7 END; 8 VAR DoB, DoD: date; 9 10 BEGIN 11 DoB.year := 1808; 12 DoD.year := 1865; 13 WriteCard(DoB.year,10); 14 WriteCard(DoD.year,10); 15 WriteLn; 16 END recordeg. linuxbeta.gl.umbc.edu> source modulasetup linuxbeta.gl.umbc.edu> gpmodula recordeg.mod linuxbeta.gl.umbc.edu> build recordeg linuxbeta.gl.umbc.edu> recordeg linuxbeta.gl.umbc.edu>

Varying Records in C Note that foo.data.I or foo.data.F are defined, but not both Allows data of different types to share storage Polymorphic data types! type enum field_type {integer, real}; struct { ft field_type; union { int I; float F; } data; } foo; if (foo.data.ft == integer) printf(“%d”, foo.data.I); else if (foo.data.ft == real) printf(“%f”, foo.data.F); else printf(“error”);

Varying Records in Modula TYPE field_type = {integer, real}; TYPE foo_type = RECORD CASE ft: field_type OF I: INTEGER; F: REAL; END; (* of CASE *) END; (* of RECORD *) VAR foo: foo_type; IF (foo.ft = integer) WriteInt(foo.I) ELSE IF (foo.ft = real) WriteReal(foo.F) ELSE WriteString(“error”);

Sets as Types There are situations where sets come in handy –when only certain data values are allowed, e.g. program options or file permissions, and no existing subrange type is appropriate –[Mon..Sun] is not a set (Sethi book has a mistake on p. 123)

Example: Sets in Modula or Pascal Operations of membership (IN), union (+), intersection (*), and set difference (-) are common Commonly implemented as bit strings TYPE colors = (white,yellow,blue,green,cyan,black,red); (* note that white < yellow < blue < … < red *) VAR CRT1, CRT2: SET OF colors; VAR testColor: colors; CRT1 := {cyan,yellow,green}; CRT2 := {red,green,blue}; IF (testColor IN CRT1) THEN WriteLn;

Type Coercion PLs differ in their approach to type coercion –coercion refers to automatic conversion from one type to another –if a PL is strongly typed, then coercion is restricted and/or explicit –if a PL is weakly typed, then coercion is taken care of by the compiler, which might cause errors –in recent years, the trend is towards strong typing

Determining an Object’s Type For static or automatic objects it’s easy –static float x; int y; For other objects it can be hard if (x) float *f = new float[250]; else char *f = new char[1000]; … /* other statements */ /* the following statement must have a semantic error, but at compile time C++ can’t tell which one is wrong since it can’t know x in advance */ cout << sqrt(f[0]) << strlen(f);

Static and Dynamic Checking Type checking is needed to make sure operations are well-defined on the objects to which they’re being applied Static type-checking is done once, typically at compile-time Dynamic type-checking is done whenever an operation is applied to an object whose type could not be determined in advance

How is Dynamic Type-Checking Done? When an object is created, a “tag” is attached to that object to indicate its type When that object is involved in some operation, the tag is checked to make sure that the operation is defined on such objects A hassle in terms of storage and execution time Smalltalk and other O-O PLs do this, but not C++