Fall 2003CS416 Compiler Design1 Type Checking A compiler has to do semantic checks in addition to syntactic checks. Semantic Checks –Static – done during.

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.
Semantics Static semantics Dynamic semantics attribute grammars
Intermediate Code Generation
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.
Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
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.
Type checking © Marcelo d’Amorim 2010.
Chapter 7:: Data Types Programming Language Pragmatics
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
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.
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.
Type Checking- Contd Compiler Design Lecture (03/02/98) Computer Science Rensselaer Polytechnic.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
CS102 Data Types in Java CS 102 Java’s Central Casting.
Chapter 6 Type Checking Section 0 Overview
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.
PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05B - Type equality Programming Language Design and.
1 Pertemuan 11 Semantic Analysis Matakuliah: T0522 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Denotational Semantics ICS.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP.
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 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Types in programming languages1 What are types, and why do we need them?
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.
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.
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.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Data Types (1) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Chapter 4: Types. Why Have Types? Detect simple kinds of errors: int square(int x) { return x*x; }... int bogus = square(“hello”); String bogus2 = square(3);
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.
Objects Real and Java COMP T1 3
Compiler Design – CSE 504 Type Checking
Semantic Analysis Type Checking
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CS 326 Programming Languages, Concepts and Implementation
C Basics.
Semantic Analysis Chapter 6.
Chapter 4: Types.
Static Checking and Type Systems
Chapter 6 Intermediate-Code Generation
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Semantic Analysis Chapter 6.
Compiler Construction
Java Programming Language
Subtype Substitution Principle
Compiler Construction
Classes and Objects Object Creation
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

Fall 2003CS416 Compiler Design1 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 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

Fall 2003CS416 Compiler Design2 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 its return type is also int.

Fall 2003CS416 Compiler Design3 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) }

Fall 2003CS416 Compiler Design4 Type Checking of Expressions E → id{ E.type=lookup(id.entry) } E → charliteral{ E.type=char } E → intliteral{ E.type=int } E → realliteral{ E.type=real } 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=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=t else E.type=type-error } E → E 1 ↑{ if (E 1.type=pointer(t)) then E.type=t else E.type=type-error }

Fall 2003CS416 Compiler Design5 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 }

Fall 2003CS416 Compiler Design6 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 typesreturn type

Fall 2003CS416 Compiler Design7 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 t = t 1 x t 2 ) 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

Fall 2003CS416 Compiler Design8 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.

Fall 2003CS416 Compiler Design9 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.

Fall 2003CS416 Compiler Design10 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