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.

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 2-2 A Simple One-Pass Compiler
CPSC 388 – Compiler Design and Construction
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.
8 Intermediate code generation
1 Compiler Construction Intermediate Code Generation.
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.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
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,
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
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.
Fall 2003CS416 Compiler Design1 Type Checking A compiler has to do semantic checks in addition to syntactic checks. Semantic Checks –Static – done during.
1 Pertemuan 18 & 19 Semantic Analyzer (type checking) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Chapter 6 Type Checking Section 0 Overview
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
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.
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.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
COP4020 Programming Languages
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
The TINY sample language and it’s compiler
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY Week 7 and 8(10.14 and 10.21): The lexical of TINY + Implement.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
Static Checking and Type Systems Chapter 6. 2 Static versus Dynamic Checking Static checking: the compiler enforces programming language’s static semantics.
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.
1 February 17, February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
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
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Semantic Analysis Chapter 6.
Static Checking and Type Systems
Chapter 6 Intermediate-Code Generation
Semantic Analysis Chapter 6.
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Compiler Construction
Syntax-Directed Translation Part I
Compiler Construction
Presentation transcript:

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 language Examples of static check –Type checks (incompatible operand) –Flow-of-control checks (break statement) –Uniqueness checks (uniquely declared identifier) –Name-related checks

Chapter 6 Type Checking Section 0 overview 2.Position of type checker Notes: 1)A type checker verifies that the type of a construct matches that expected by its context. 2)Type information gathered by a type checker may be needed when code is generated. 3)A symbol that can represent different operations in different context is said to be “overloaded” parser Token stream Type checker Syntax tree Intermedi ate code generator Syntax tree Intermediate representation

Chapter 6 Type Checking Section 1 Type Systems 1. Type Expressions –Denote the type of a language construct 1) A basic type is a type expression. 2) A type name is a type expression. 3) A type constructor applied to type expression is a type expression. Constructors include: –Arrays, products, records,pointers, functions 4) Type expressions may contain variables whose values are type expressions. Notes: A convenient way to represent a type expression is to use a graph

Chapter 6 Type Checking Section 1 Type Systems 2. Type Systems –A type system is a collection of rules for assigning type expressions to the various parts of a program. Note: 1)A type checker implements a type system 2)The type systems are specified in a syntax- directed manner 3)Different type systems may be used by different compilers or processors of the same language

Chapter 6 Type Checking Section 1 Type Systems 3. Static and Dynamic Checking of Types –Static Checking (Done by a compiler) –Dynamic Checking (Checking after run) –Strongly typed language (if its compiler can guarantee that the programs it accepts will execute without type error) 4. Error Recovery

Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker 1. A simple language 1)Grammar for source language –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^

Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker 1. A simple language 2)The part of a translation scheme that saves the type of an identifier. –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  array [num] of T {T.type=array(1..num,T 1.type)} –T  ^T 1 {T.type=pointer(T 1.type)}

Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker 2. 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) && (E 1.type==integer) integer else type_error}

Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker 2. Type Checking of Expressions –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}

Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker 3. Type Checking of Statements –S  id:=E –{S.type= if (id.type==E.type) void else type_error} –S  if E then S 1 –{S.type= if (E.type==boolean) S 1.type else type_error}

Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker 3. Type Checking of Statements –S  while E do S 1 –{S.type= if (E.type==boolean) S 1.type else type_error} –S  S 1 ;S 2 –{S.type= if (S 1.type==void) && (S 2.type== void) void else type_error}

Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker 4. Type Checking of Functions –T  T 1 ‘  ’ T 2 {T.type= T 1.type  T 2. type} –E  E 1 (E 2 ) –{E.type= if (E 2.type==s) &&(E 1.type==s  t) t else type_error}

Chapter 6 Type Checking Section 3 Equivalence of type expressions 1.Structural Equivalence of type expression –The same basic type –Formed by applying the same constructor to structurally equivalent types

Chapter 6 Type Checking Section 3 Equivalence of type expressions 2.Names for type expression type link=^cell 3.Cycles in representations of types type link=^cell; cell=record info:integer; next :link end;

Chapter 6 Type Checking Section 4 Type conversion Coercions –Implicit type conversions Explicit conversions –Chr(55)