Type Checking 66.648 Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.

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.
Generation of Intermediate Code Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic.
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.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014.
1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.
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,
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
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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 Pertemuan 18 & 19 Semantic Analyzer (type checking) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Type Checking- Contd Compiler Design Lecture (03/02/98) Computer Science Rensselaer Polytechnic.
Chapter 6 Type Checking Section 0 Overview
Type Checking  Legality checks  Operator determination  Overload resolution.
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.
1 Pertemuan 11 Semantic Analysis Matakuliah: T0522 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Types Type = Why? a set of values
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.
Imperative Programming
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Types and Inheritances Compiler Design Lecture (03/04//98) Computer Science Rensselaer Polytechnic.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Attribute Grammar Examples and Symbol Tables Compiler Design Lecture (02/23/98) Computer Science Rensselaer Polytechnic.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Dale Roberts Object Oriented Programming using Java - Enumerations Dale Roberts, Lecturer Computer Science, IUPUI Department.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
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.
ISBN Chapter 10 Implementing Subprograms.
Lecture 8 Semantic Analysis.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
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.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 9 Ahmed Ezzat Semantic Analysis.
Lecture 9 Symbol Table and Attributed Grammars
Compiler Design – CSE 504 Type Checking
Semantic Analysis Type Checking
Context-Sensitive Analysis
Constructing Precedence Table
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Semantic Analysis Chapter 6.
Static Checking and Type Systems
Chapter 6 Intermediate-Code Generation
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
SYNTAX DIRECTED DEFINITION
Compiler Construction
Compiler Construction
Presentation transcript:

Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic

Lecture Outline Types and type checking Types and type checking Poly-morphic functions Poly-morphic functions Administration Administration

Errors We are in chapter 6 of the text book. Please read that chapter. Compiler must check that the source program follows both the syntactic and semantic conventions of the source language. Static checks - reported during the compilation phase. Dynamic checks - occur during the execution of the program.

Static Checks 1. Type checks: A compiler should report an error if an operator is applied to an incompatible operand. 2. Flow of control checks: Places for transfer of control should be specified. 3. Uniqueness checks: An object must be defined exactly (like the type of an identifier, statements inside a case/switch statement.) 4. Name Related checks: Same name must appear two or more times.

Procedure foo end foo; Type information gathered by a type checker may be needed when code is generated. In general operators could be overloaded. (What is the case in Java?) Example

The design of a type checker for a language is based on the information about the syntactic constructs in the language. Each expression has a type associated with it. Types are either basic or constructed. Basic types are atomic with no internal structure as far as the program is constructed. Pointers, arrays, functions, structures can be treated as constructor types. Type System

Type Expressions A type expression is either a basic type or is formed by applying an operator called type constructor to other expressions. 1. A basic type is a type expression. “void” is also a basic type. 2. A type name is a type expression. 3. A type constructor applied to a type expression is a type expression.

Constructors 1. Array : If T is a type expression, then array(I,T) is a type expression, where I is an index set. 2. Products: If T_1 and T_2 are type expressions, then so is T_1 x T_2 3. Structures: differs from the products as fields have names. The struct type constructor applied to tuple formed from field names and field types. 4. Pointers: If T is a type expression, then pointer(T) is a type expression.

Constructors -Contd 5. Functions: Functions in a programming language map a domain type D to a range type R. Often, for implementation reasons, there are limitations on the types a function can return. 6. Objects: Objects are also type. We will talk about objects in the next lecture. Type Expressions may contain variables whose values are type expressions.

Type Systems A type system is a collection of rules for assigning type expressions to the various parts of a program. A type checker implements a type system. The type of an array includes the index set of an array, so a function with an array argument can only be applied to arrays with that index set.

Static and Dynamic Checking of Types A sound type system eliminates the need for dynamic checking for type errors. A language is strongly typed if its compiler can guarantee that the programs it accepts will execute without type errors. E.g. array bound checking.

Error Recovery Since type checking has the potential for catching errors in programs, it is important to do something when an error is discovered. The inclusion of error handling may result in a type system that goes beyond the one needed to specify correct programs.

Specifications of a simple type checker The type of each identifier must be declared before the identifier is used. The type checker is a translation scheme that synthesizes the type of each expression from the type of its sub- expressions. 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* Base Types: char, integer, type-error

Translation Scheme P --> D; E D--> D;D D--> id :T { addtype(id.entry,T.type);} T--> char {T.type= char;} T--> integer {T.type=integer;} T-->*T_1 {T.type=pointer(T_1.type);} T--> array[num] of T_1 { T.type =array(1..num.val,T_1.type); }

Type Checking of Expressions E--> literal { E.type=char;} E--> num { E.type =integer;} E--> id { E.type =lookup(id.entry);} E--> E_1 mod E_2 { E.type =If (E_1.type ==integer) if (E_2. Type ==integer) integer; else type-error; E--> E_1[E_2] { E.type=if ((E_2.type==integer)&& (E_1.type==array(s,t)) t; else type-error;} E--> *E_1 { E.type = if (E_1.type ==pointer(t)) t else type-error;

Type Checking for Statements S--> id=E { if (id.type==E.type) void; else type- error;} S--> if E then S { if (E.type==boolean) S_1.type; else type-error;} S--> While E do S { if (E.type==boolean) S_1.type; else type-error; S--> S; S; { if (S_1.type==void) if (S_2.type ==void) void; else type-error;}

Type Checking of Functions E--> E(E) T--> T ‘->’ T { T.type = T1.type -> T2.type} E--> E(E) {E.type = I f ((E_2.type ==s) && (E_1.type == s--> t)) t; else type-error;

Comments and Feedback Project 2 is out. Please start working. PLEASE do not wait for the due date to come. We are in chapter 6. We will do the rest of chapter 6 in the next class. Please keep studying this material. It may look difficult.