Type Checking. CS308 Compiler Theory 1

Similar documents
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

Type systems. Static typing

Type Checking. Error Checking

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

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

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

Semantic Analysis and Type Checking

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

Intermediate Code Generation

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

Intermediate Code Generation Part II

5. Semantic Analysis!

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

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

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

CA Compiler Construction

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

Introduction to Programming Using Java (98-388)

Final CSE 131B Spring 2004

Acknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.

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

Static Checking and Type Systems

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

Algorithms & Data Structures

Concepts Introduced in Chapter 6

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

CS558 Programming Languages

Type Checking. Chapter 6, Section 6.3, 6.5

Concepts Introduced in Chapter 6

CS162: Introduction to Computer Science II

Values (a.k.a. data) representation. Advanced Compiler Construction Michel Schinz

Values (a.k.a. data) representation. The problem. Values representation. The problem. Advanced Compiler Construction Michel Schinz

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

The Calculator CS571. Abstract syntax of correct button push sequences. The Button Layout. Notes 16 Denotational Semantics of a Simple Calculator

CSC Java Programming, Fall Java Data Types and Control Constructs

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Winter Compiler Construction Who. Mailing list and forum

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

1 Introduction. 3 Syntax

Java Identifiers, Data Types & Variables

Chapter 6 Intermediate Code Generation

CS558 Programming Languages

(Not Quite) Minijava

CSc 453. Semantic Analysis III. Basic Types... Basic Types. Structured Types: Arrays. Compilers and Systems Software. Christian Collberg

CS 61B Data Structures and Programming Methodology. June David Sun

Lexical Considerations

Computational Expression

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

Operational Semantics of Cool

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

Principles of Programming Languages

Intermediate Code Generation

Motivation was to facilitate development of systems software, especially OS development.

Type Checking and Type Inference

Lexical Considerations

CS4120/4121/5120/5121 Spring 2016 Xi Language Specification Cornell University Version of May 11, 2016

The Warhol Language Reference Manual

Lecture Set 4: More About Methods and More About Operators

Today's Topics. CISC 458 Winter J.R. Cordy

Practice Midterm 1. Problem Points Score TOTAL 50

CS 11 java track: lecture 1

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Syntax-Directed Translation Part II

Intermediate Code Generation

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

CS16 Exam #1 7/17/ Minutes 100 Points total

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Symbol Table Information. Symbol Tables. Symbol table organization. Hash Tables. What kind of information might the compiler need?

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

CS558 Programming Languages

Class API. Class API. Constructors. CS200: Computer Science I. Module 19 More Objects

CS 231 Data Structures and Algorithms, Fall 2016

Type Analysis. Type Checking vs. Type Inference

Array. Array Declaration:

Following is the general form of a typical decision making structure found in most of the programming languages:

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

Type Conversion. and. Statements

CHAD Language Reference Manual

CS 132 Compiler Construction, Fall 2011 Instructor: Jens Palsberg Multiple Choice Exam, Dec 6, 2011

Lecture Set 4: More About Methods and More About Operators

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

Short Notes of CS201

Last class. CS Principles of Programming Languages. Introduction. Outline

Begin at the beginning

Variables and Primitive Types

CS201- Introduction to Programming Current Quizzes

Denotational Semantics. Domain Theory

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

Programming Languages Lecture 14: Sum, Product, Recursive Types

ECS 142 Project: Code generation hints

CS201 - Introduction to Programming Glossary By

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

Transcription:

Type Checking CS308 Compiler Theory 1

Type Checking A compiler has to do semantic checks in addition to syntactic checks. Semantic Checks Static done during compilation Dynamic done during run-time Type checking is one of these static checking operations. we may not do all type checking at compile-time. Some systems also use dynamic type checking too. A type system is a collection of rules for assigning i type expressions to the parts of a program. A type checker implements a type system. A sound type system eliminates run-time type checking for type errors. A programming language is strongly-typed, if every program its compiler accepts will execute without type errors. In practice, some of type checking operations are done at run-time (so, most of the programming languages are not strongly-typed). Ex: int x[100]; x[i] most of the compilers cannot guarantee that i will be between 0 and 99 CS308 Compiler Theory 2

Type Expression The type of a language construct is denoted by a type expression. A type expression can be: A basic type a primitive data type such as integer, real, char, boolean, type-error to signal a type error void : no type A type name a name can be used to denote a type expression. A type constructor applies to other type expressions. arrays: If T is a type expression, then array(i,t) is a type expression where I denotes index range. Ex: array(0..99,int) products: If T 1 and T 2 are type expressions, then their cartesian product T 1 x T 2 is a type expression. Ex: int x int pointers: If T is a type expression, then pointer(t) is a type expression. Ex: pointer(int) functions: We may treat functions in a programming language as mapping from a domain type D to a range type R. So, the type of a function can be denoted by the type expression D R where D are R type expressions. Ex: int int represents the type of a function which takes an int value as parameter, and dits return type is also int. CS308 Compiler Theory 3

A Simple Type Checking System P D;E D D;D D id:t { addtype(id.entry,t.type) } T char { T.type=char } T int { T.type=int } T real { T.type=real } T T 1 { T.type=pointer(T 1.type) } T array[intnum] of T 1 { T.type=array(1..intnum.val,T 1.type) } CS308 Compiler Theory 4

Type Checking of Expressions E id { E.type=lookup(id.entry) } E charliteral { E.type=char } E intliteral { E.type=int } E realliteral l {Et E.type=real l} E E 1 + E 2 { if (E 1.type=int and E 2.type=int) then E.type=int else if (E 1.type=int and E 2.type=real) then E.type=real else if (E 1.type=real and E 2.type=int) then E.type=real else if (E 1.type=real and E 2.type=real) then E.type type=real else E.type=type-error } E E 1 [E 2 ] { if (E 2.type=int and E 1.type=array(s,t)) then E.type=tt else E.type=type-error } E E 1 { if (E 1.type=pointer(t)) p then E.type=t else E.type=type-error } CS308 Compiler Theory 5

Type Checking of Statements S id = E { if (id.type=e.type then S.type=void else S.type=type-error } S if E then S 1 { if (E.type=boolean then S.type=S 1.type else S.type=type-error } S while E do S 1 { if (E.type=boolean then S.type=S 1.type else S.type=type-error } CS308 Compiler Theory 6

Type Checking of Functions E E 1 ( E 2 ) { if (E 2.type=s and E 1.type=s t) then E.type=t else E.type=type-error } Ex: int f(double x, char y) {... } f: double x char int argument types return type CS308 Compiler Theory 7

Structural Equivalence of Type Expressions How do we know that two type expressions are equal? As long as type expressions are built from basic types (no type names), we may use structural equivalence between two type expressions Structural Equivalence Algorithm (sequiv): if (s and t are same basic types) then return true else if (s=array(s 1,s 2 ) and t=array(t 1,t 2 )) then return (sequiv(s 1,t 1 ) and sequiv(s 2,t 2 )) else if( (s = s 1 x s 2 and dt = t 1 x t 2 )th then return (sequiv(s ( 1,t 1 ) and sequiv(s 2,t 2 )) else if (s=pointer(s 1 ) and t=pointer(t 1 )) then return (sequiv(s 1,t 1 )) else if (s = s 1 s 2 and t = t 1 t 2 ) then return (sequiv(s 1,t 1 ) and sequiv(s 2,t 2 )) else return false CS308 Compiler Theory 8

Names for Type Expressions In some programming languages, we give a name to a type expression, and we use that name as a type expression afterwards. type link = cell;? p,q,r,s have same types? var p,q : link; var r,s : cell How do we treat type names? Get equivalent type expression for a type name (then use structural equivalence), or Treat a type name as a basic type. CS308 Compiler Theory 9

Cycles in Type Expressions type link = cell; type cell = record x : int, next : link end; We cannot use structural equivalence if there are cycles in type expressions. We have to treat type names as basic types. but this means that the type expression link is different than the type expression cell. CS308 Compiler Theory 10

Type Conversions x + y? what is the type of this expression (int or double)? What kind of codes we have to produce, if the type of x is double and the type of y is int? inttoreal y,,t1 real+ t1,x,t2 CS308 Compiler Theory 11