Type Checking. Error Checking

Similar documents
Type systems. Static typing

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

Intermediate Code Generation Part II

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

Static Checking and Type Systems

Concepts Introduced in Chapter 6

Principles of Programming Languages

There is a level of correctness that is deeper than grammar. There is a level of correctness that is deeper than grammar

Concepts Introduced in Chapter 6

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

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 309

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Type Analysis. Type Checking vs. Type Inference

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 264

Type Checking. CS308 Compiler Theory 1

Formal Languages and Compilers Lecture X Intermediate Code Generation

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Semantic Analysis and Type Checking

The compilation process is driven by the syntactic structure of the program as discovered by the parser

Type Systems. Seman&cs. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Introduction to Programming Using Java (98-388)

Types. What is a type?

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

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

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

C++ Important Questions with Answers

CSE 431S Type Checking. Washington University Spring 2013

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Expressions and Casting

Symbol Tables. For compile-time efficiency, compilers often use a symbol table: associates lexical names (symbols) with their attributes

Introduction to Computer Science Midterm 3 Fall, Points

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

Types and Type Inference

Type Checking. Chapter 6, Section 6.3, 6.5

1 Lexical Considerations

Lexical Considerations

Lecture #23: Conversion and Type Inference

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Lexical Considerations

Chapter 3 Function Overloading

C++ (Non for C Programmer) (BT307) 40 Hours

Java Primer 1: Types, Classes and Operators

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

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

Types and Type Inference

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

5. Semantic Analysis!

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Lecture 12: Data Types (and Some Leftover ML)

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

22c:111 Programming Language Concepts. Fall Types I

Type Checking and Type Inference

CS558 Programming Languages

Type Conversion. and. Statements

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

intermediate code generator syntax tree token stream intermediate representation parser checker tree

Computational Expression

Chapter 2: Using Data

Intermediate Code Generation

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Types-2. Polymorphism

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

CS /534 Compiler Construction University of Massachusetts Lowell

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Intermediate Code Generation

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Strict Inheritance. Object-Oriented Programming Winter

Compilers CS S-05 Semantic Analysis

CA Compiler Construction

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

5. Syntax-Directed Definitions & Type Analysis

Semantic Processing. Semantic Errors. Semantics - Part 1. Semantics - Part 1

Intermediate Code Generation

Types, Type Inference and Unification

Static Type Checking. Static Type Checking. The Type Checker. Type Annotations. Types Describe Possible Values

Type Systems, Type Inference, and Polymorphism

TYPES, VALUES AND DECLARATIONS

Data Types and Variables in C language

Program construction in C++ for Scientific Computing

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

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

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */

GraphQuil Language Reference Manual COMS W4115

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

Names, Scopes, and Bindings II. Hwansoo Han

Transcription:

Type Checking Error Checking Dynamic checking takes place while program is running Static checking takes place during compilation Type checks Flow-of-control checks Uniqueness checks Name-related checks CS 5300 - SJAllan 2 1

Types Every expression has an associated type Rules exist to assign a type to an expression, e.g.: If you add, subtract, or multiply two integer operands, the result is an integer If you take the address of an operand of type T, the type of the result is pointer to T In Pascal and C, types are either basic or constructed CS 5300 - SJAllan 3 Type Checking Verifying that the type of a construct matches that expect by its context Type information may be needed when code is generated. Why? A symbol that can represent different operations in different contexts is said to be overloaded Overloading may be accompanied by coercion of types CS 5300 - SJAllan 4 2

Polymorphism Different from overloading The body of a polymorphic function can be executed with arguments of several types CS 5300 - SJAllan 5 Type Expressions A "type expression" is the type of a programming language construct Type expressions include basic types, e.g. integer or real A special basic type, type_error, signals an error during type checking Some languages include a void type CS 5300 - SJAllan 6 3

Type Expressions Applying "type constructors" to type expressions forms new type expressions Arrays Records Pointers A function maps elements from one set (domain) to another set (range) mod has type "int x int int" function f(a, b: char) : integer; (f has type "char x char pointer(integer)") CS 5300 - SJAllan 7 Type Systems A type system is a collection of rules for assigning type expressions A type checker implements a type system Different type systems may be used by different compilers of the same language A type checker should adequately handle error recovery CS 5300 - SJAllan 8 4

Static vs. Dynamic Checks In principle, any check can be done dynamically if the target code carries type information A sound type system eliminates the need for dynamic checking A language is strongly typed of its compiler only accepts programs that have no type errors In practice, some checks can only be done dynamically (e.g., checking array indices) CS 5300 - SJAllan 9 Simple Type Checker P D ; E D D ; D id : T T char integer array [num] of T T E literal num id E mod E E[E] E The simple language above allows a sequence of declarations followed by a single expression A simple syntax-directed definition can specify a type checker for this language CS 5300 - SJAllan 10 5

Simple Type Checker P D ; E D D ; D D id : T T char T integer T T 1 T array[num] of T 1 addtype(id.entry, T.type) T.type := char T.type := integer T.type := pointer(t 1.type) T.type := array(num, T 1.type) CS 5300 - SJAllan 11 Simple Type Checker E literal E num E id E E 1 mod E 2 E E 1 [E 2 ] E E 1 E.type := char E.type := integer E.type := id.type E.type := if E 1.type = integer and E 2.type = integer then integer else type_error E.type := if E 2.type = integer and E 1.type = array(s,t) then t else type_error E.type := if E 1.type = pointer(t) then t else type_error CS 5300 - SJAllan 12 6

Type Checking of Statements S id := E S if E then S 1 S while E do S 1 S S 1 ; S 2 Statements S.type := if id.type = E.type then void else type_error S.type := if E.type = boolean then S 1.type else type_error S.type := if E.type = boolean then S 1.type else type_error S.type := if S 1.type = void and S 2.type = void then void else type_error CS 5300 - SJAllan 13 Type Checking of Functions E E 1 (E 2 ) subprogam_head function id arguments : standard_type ';' Statements id.type := arguments.type standard_type.type E.type := if E 2.type = s and E 1.type = s t then t else type_error CS 5300 - SJAllan 14 7

Type Systems A collection of rules for assigning type expressions to the various parts of a program A type checker implements a type system CS 5300 - SJAllan 15 Type Checkers Static Checking is done by the compiler Dynamic Checking is done at run-time A sound type system eliminates the need for dynamic checking for type errors because it allows us to determine statically that these errors cannot occur at run-time A langue is strongly typed if its compiler can guarantee that the programs it accepts will execute without errors CS 5300 - SJAllan 16 8

Error Recovery If an error is discovered, the type checker should do something reasonable At the very least, it should report the nature and location of the error It is desirable for the type checker to recover from errors, so it can check the rest of the input CS 5300 - SJAllan 17 A Simple Type Checker P D ; E D D ; D id : T T char integer array[num] of T T E literal num id E mod E E[E] E CS 5300 - SJAllan 18 9

Type Checking Scheme P D ; E D D ; D D id : T T char T integer T T 1 T array[num] of T 1 addtype(id.entry, T.type) T.type = char T.type = integer T.type = pointer(t 1.type) T.type = array(num.val, T 1.type) CS 5300 - SJAllan 19 Type System for Expressions E literal E num E id E E 1 mod E 2 E E 1 [E 2 ] E E 1 E.type = char E.type = integer E.type = lookup(id.entry) E.type = if E 1.type = integer and E 2.type = integer then integer else type_error E.type = if E 2.type = integer and E 1.type = array(s,t) then t else type_error E.type = if E 1.type = pointer(t) then t else type_error CS 5300 - SJAllan 20 10

Type System for Statements S id = E S if E then S 1 S while E do S 1 S S 1 ; S 2 S.type = if id.type = E.type then void else type_error S.type = if E.type = boolean then void else type_error S.type = if E.type = boolean then void else type_error S.type = if S 1.type = void and S 2.type = void then void else type_error CS 5300 - SJAllan 21 Type System for Functions T T 1 T 2 E E 1 (E 2 ) T.type = T 1.type T 2.type E.type = if E 2.type = s and E 1.type = s t then t else type_error CS 5300 - SJAllan 22 11

Equivalence of Types Name equivalence Each type name is viewed as a distinct type Two type expressions are name equivalence if and only if they are identical Structural equivalence Names are replaced by the type expressions they define So two type expressions are structurally equivalence if they represent two structurally equivalence type expressions when all names have been substituted out CS 5300 - SJAllan 23 Implicit (coercion) Type Conversion Conversion from one type to another implicitly done by the compiler Explicit Programming must write something to cause the conversion CS 5300 - SJAllan 24 12

Coercion of Expressions E num E num. num E id E E 1 op E 2 E.type = integer E.type = real E.type = lookup(id.entry) E.type = if E 1.type = integer and E 2.type = integer then integer else if E 1.type = integer and E 2.type = real then real else if E 1.type = real and E 2. type = integer then real else if E 1.type = real and E 2.type = real then real else type_error CS 5300 - SJAllan 25 13