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.

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 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.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Chapter 7:: Data Types Programming Language Pragmatics
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
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.
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.
CH4.1 CSE244 Type Checking Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
Compiler Construction
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 9 Imperative and object-oriented languages 1.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Fall 2003CS416 Compiler Design1 Type Checking A compiler has to do semantic checks in addition to syntactic checks. Semantic Checks –Static – done during.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Encapsulation by Subprograms and Type Definitions
Type Checking in Cool Alex Aiken (Modified by Mooly Sagiv)
Chapter 6 Type Checking Section 0 Overview
Approaches to Typing Programming Languages Robert Dewar.
Type Checking  Legality checks  Operator determination  Overload resolution.
Types Type = Why? a set of values
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Semantic Analysis CS 671 February 5, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens –e.g.: main$
Type Equivalence Rules Ada –Strict name equivalence except for almost everything Unique array constructors give rise to unique types Subtypes can create.
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)
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
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.
Arithmetic Expressions
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.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
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.
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Lecture 9 Symbol Table and Attributed Grammars
Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types
Compiler Design – CSE 504 Type Checking
Semantic Analysis Type Checking
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Context-Sensitive Analysis
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Construction
Java Programming Language
Compiler Construction
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

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 code, with less runtime overhead –to avoid runtime errors –to improve expressiveness (see overloading) How? –Type checking Given an operation and an operand of some type, determine whether the operation is allowed on that operand –Type inference Given the type of operands, determine –the meaning of the operation –the type of the operation OR, without variable declarations, infer type from the way the variable is used

2 Issues in typing Does the language have a type system? –Untyped languages (e.g. assembly) have no type system at all When is typing performed? –Static typing: At compile time –Dynamic typing: At runtime –Which is best? How strictly are the rules enforced? –Strongly typed: No exceptions –Weakly typed: With well-defined exceptions Type equivalence & subtyping –When are two types equivalent? What does "equivalent" mean anyway? –When can one type replace another?

3 Components of a type system Built-in types Rules for constructing new types Rules for determining if two types are equivalent Rules for inferring the types of expressions

4 Component: Built-in types These are the basic types. Usually, integer –usual operations: standard arithmetic floating point –usual operations: standard arithmetic character –character set generally ordered lexicographically –usual operations: (lexicographic) comparisons boolean –usual operations: not, and, or, xor

5 Component: type constructors Arrays –array(I,T) denotes the type of an array with elements of type T, and index set I –multidimensional arrays are just arrays where T is also an array –operations: element access, array assignment, products Strings –bitstrings, character strings –operations: concatenation, lexicographic comparison Products –Groups of multiple objects of different types essentially, Cartesian product of types (useful in functions later) Records (structs) –Groups of multiple objects of different types where the elements are given specific names.

6 Component: type constructors Pointers –addresses –operations: arithmetic, dereferencing, referencing –issue: equivalency Function types –A function such as "int add(real, int)" has type real  int  int

7 Component: type equivalence Name equivalence –Two types are equivalent when they have the same name. –Loose vs. strict Structural equivalence –Two types are equivalent when they have the same structure Should records require member name identity to be equivalent? How about Array(int, 1..10) and Array(int, 1..20). Should we consider the bounds or just the element type? How about recursively defined types? Example –C uses structural equivalence for structs and name equivalence for arrays/pointers. Issue –Type equivalence allows type interchangeability If two equivalent types have different meanings, there will be no error message if we interchange them.

8 Component: type equivalence Subtyping –An operation that expects a type T and receives a type T' which is not equivalent to T, may still go ahead if T is a subtype of T'. –But what exactly is a subtype? A subset of the values and superset of the properties? Is a subclass a subtype?

9 Component: type equivalence Type coercion –If x is float, is x=3 acceptable? Disallow Allow and implicitly convert 3 to float. "Allow" but require programmer to explicitly convert 3 to float How do we convert? –Reinterpret bit sequence –Build new object –What should be allowed? float to int ? int to float ? What if both types are equally general? What if multiple coercions are possible? –Consider 3 + "4" in a language that can convert strings to integers.

10 Overloading Same operation name but different effect on different types For an overloaded function f –resolve arguments –look up f in symbol table to get list of all visible "versions" –ignore those with wrong parameter types.

11 A simple type checker We can use an attribute grammar to implement a simple type checker. E  num{E.type = integer} E  E 1 +E 2 {E.type = (E 1.type == integer && E 2.type == integer)? integer : error} E  E 1 [E 2 ]{E.type = (E 1.type == array(int, T) && E 2.type == integer)? T : error} E  *E 1 {E.type = (E 1.type == pointer(T))? T : error} E  E 1 (E 2 ){E.type = (E 1.type == (S  T) && E 2.type == S)? T : error S  if E then S 1 {S.type = (E.type == boolean)? void : error}

12 Inference rules The formal notation for type checking/inference is rules of inference They have the form: If we can prove the hypotheses are all true in the existing environment, then the conclusion is also true. For example: In English: If expressions E1 and E2 both have type int, then E1+E2 also has type int. Environment Hypotheses Environment Conclusions A E1: int A E2: int A E1 + E2 : int

13 Inference rules The inference rules give us templates that describe how to type various expressions. We can use the templates to prove that an expression has a valid type (and find what that type is) The construct parallels the parse tree. Short example: –Type the expression x+a[i] under the assumption that x is an char, a[i] is an array of chars and i is an int. –The environment is A={x:char, a:array(int, char), i:char} A E1: array(T,S), A E2: T A E1[E2] : S [ARRAY] A E1: char, A E2: char A E1 + E2 : char [ADD]

14 Inference rules [ADD] A |– x :char A |– i : int A |– a : array(int,char) A |– a[i] : char A |– x+a[i] : char