Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.

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
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
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
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.
Lecture # 20 Type Systems. 2 A type system defines a set of types and rules to assign types to programming language constructs Informal type system rules,
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
Chapter 6 Type Checking Section 0 Overview 1.Static Checking Check that the source program follows both the syntactic and semantic conventions of the source.
Type Checking.
Compiler Construction
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Type Checking- Contd Compiler Design Lecture (03/02/98) Computer Science Rensselaer Polytechnic.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
Encapsulation by Subprograms and Type Definitions
Chapter 6 Type Checking Section 0 Overview
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
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.
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
Type Equivalence Rules Ada –Strict name equivalence except for almost everything Unique array constructors give rise to unique types Subtypes can create.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 5 © 2002 by Addison Wesley Longman, Inc Names - We discuss all user-defined names here - Design issues for names: - Maximum length? - Are.
COMP4730/2003/lec5/H.Melikian Names, Bindings,Type Checking and Scopes (Chapter 5) - Design issues: - Maximum length? - Are connector characters allowed?
1 CS Programming Languages Class 07 September 14, 2000.
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.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.
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.
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.
CS212: Object Oriented Analysis and Design
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Static Checking and Type Systems Chapter 6. 2 Static versus Dynamic Checking Static checking: the compiler enforces programming language’s static semantics.
CS536 Types 1. Roadmap Back from our LR Parsing Detour Name analysis – Static v dynamic – Scope Today – Type checking 2 Scanner Parser Tokens Semantic.
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.
Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
1 February 17, February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Names, Scope, and Bindings Programming Languages and Paradigms.
Data Types (1) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Records type city is record -- Ada Name: String (1..10); Country : String (1..20); Population: integer; Capital : Boolean; end record; struct city { --
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
© 2016 Pearson Education, Ltd. All rights reserved.
Semantic Analysis Type Checking
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.
CS 326 Programming Languages, Concepts and Implementation
Data Types.
Type Systems CSE 340 – Principles of Programming Languages Fall 2016
Semantic Analysis Chapter 6.
Names and Binding In Text: Chapter 5.
Compiler Construction
Compiler Construction
Types and Related Issues
CSE 3302 Programming Languages
Presentation transcript:

Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden

Lecture Outline Type Equivalence Type Checking Type Conversion 2

Aggregate Data Types Arrays Records Unions Pointers 3

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

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 5

Structural Type Equivalence 6 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;

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

Name Equivalence Two name types are equivalent only if they have the same name Name equivalence is available in Ada and C 8 typedef int ar1[10]; typedef ar1 ar2; typedef int age; type ar1 is array (INTEGER range1..10) of INTEGER; type ar2 is new ar1; type age is new INTEGER;

Name equivalence… variable1: ar1; variable2: ar1; variable3: ar2; 9 variable4: array (INTEGER range ) of INTEGER; variable5: array (INTEGER range ) of INTEGER; v6,v7: array (INTEGER range ) of INTEGER;

Declaration Equivalent Lead back to the same original structure declaration via a series of redeclarations 10 type t1 = array [1..10] of integer; t2 = t1; t3 = t2; type t4 = array [1..10] of integer; t5 = array [1..10] of integer;

Type Checking Involves the application of a type equivalence algorithm to expressions and statements to determine if they make sense Any attempt to manipulate a data object with an illegal operation is a type error Program is said to be type safe (or type secure) if guaranteed to have no type errors Static versus dynamic type checking Run time errors 11

Type Checking… Strong typing and type checking ◦ Strong guarantees type safety Statically typed versus dynamically typed ◦ Static (type of every program expression be known at compile time)  All variables are declared with an associated type  All operations are specified by stating the types of the required operands and the type of the result A statically typed language is strongly typed 12

Type Checking …and type inference ◦ Types of expressions are inferred from types of their subexpressions  E1 + E2 ◦ In a function call  Type checking part (match actual and formal parameters  Type inference part (determine the result type of the call) Close interaction with the type equivalence algorithm 13

Type Checking… MODULA2 has declaration equivalence What is wrong with this? PROCEDURE p(ar:ARRAY [1..max] OF INTEGER); 14

Type Checking TYPE artype = ARRAY [1..max] of INTEGER; PROCEDURE p(ar: artype); 15

Type Conversion r is float and j is integer r = j i is integer and j is integer i = j

Type Conversion… Modula2 i := TRUNC (FLOAT(j) ) Explicit type conversion ◦ type conversion functions Implicit conversion ◦ coercion ◦ can weaken type checking 17

Type conversion… Casts ◦ A value or object of one type is preceded by a type name (int) 3.14 Often does not cause a conversion to take place. Internal representation is reinterpreted as a new type CARDINAL(-1)