CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi – 110 025

Slides:



Advertisements
Similar presentations
CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Advertisements

Chapter 2-2 A Simple One-Pass Compiler
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CPSC 388 – Compiler Design and Construction
Semantic Analysis and Symbol Tables
Symbol Table.
C Language.
Chapter 6 Intermediate Code Generation
Semantics Static semantics Dynamic semantics attribute grammars
Modules Program is built out of components. Each component defines a set of logically related entities (strong internal coupling) A component has a public.
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.
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
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.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
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,
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
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
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;
Type Checking- Contd Compiler Design Lecture (03/02/98) Computer Science Rensselaer Polytechnic.
Chapter 6 Type Checking Section 0 Overview
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 6: Type Systems Fall 2009.
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.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Denotational Semantics ICS.
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.
Imperative Programming
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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.
1 Semantic Analysis  Semantic analysis includes  Dynamic Checking (Those checks for which to perform, compiler doesn’t have sufficient information) 
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.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
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.
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
Context-Sensitive Analysis
Constructing Precedence Table
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Semantic Analysis Chapter 6.
Chapter 6 Intermediate-Code Generation
Designing a Predictive Parser
Semantic Analysis Semantic analysis includes
Semantic Analysis Chapter 6.
SYNTAX DIRECTED DEFINITION
Compiler Construction
Compiler Construction
Presentation transcript:

CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –

CH4.2 Static Checking Some syntactic/semantic properties of the source language are not appropriate for incorporation into the grammar of the language. Some syntactic/semantic properties of the source language are not appropriate for incorporation into the grammar of the language. Content sensitive elements. They will be checked separately. They will be checked separately. Static Checking/ Dynamic Checking. Static Checking/ Dynamic Checking. Static checking examples: Static checking examples: Type checks. Flow-of-control checks check whether a e.g. break statement has somewhere to return control. Uniqueness checks, Name-related checks. In some languages names must be unique or they must appear twice. Etc.

CH4.3 Type Expressions The type of a language construct will be denoted by a type- expression. The type of a language construct will be denoted by a type- expression. Type-expressions are either basic types or they are constructed from basic types using type constructors. Type-expressions are either basic types or they are constructed from basic types using type constructors. Basic types: boolean, char, integer, real, type_error, void array(I,T) where T is a type-expression and I is an integer-range. E.g. int A[10] has the type expression array({0,..,9},integer) We can take cartesian products of type- expressions. E.g. struct entry {char letter; int value; }; is of type ( letter x char) x ( value x integer)

CH4.4 Type Expressions,II Pointers. int* aa aa is of type pointer(integer). Functions: int divide(int i, int j) is of type integer x integer integer Representing type expressions as trees e.g. char x char pointer(integer) char char integer pointer x

CH4.5 Type Systems A Type-system: collection of rules for assigning type-expressions to the variable parts of a program. A Type-system: collection of rules for assigning type-expressions to the variable parts of a program. A type-checker implements a type-system. A type-checker implements a type-system. It is most convenient to implement a type-checker within the semantic rules of a syntax-directed definition (and thus it will be implemented during translation). It is most convenient to implement a type-checker within the semantic rules of a syntax-directed definition (and thus it will be implemented during translation). Many checks can be done statically (at compilation). Many checks can be done statically (at compilation). Not all checks can be done statically. E.g. int A[10]; int i; … … ; printf(%d,A[i]); Not all checks can be done statically. E.g. int A[10]; int i; … … ; printf(%d,A[i]); Recent security concerns about type-checking (the buffer overflow vulnerability). Recent security concerns about type-checking (the buffer overflow vulnerability).

CH4.6 A simple type-checker. Use a synthesized attribute called type to carry the type expression of the corresponding language construct. Use a synthesized attribute called type to carry the type expression of the corresponding language construct. We will use the grammar: We will use the grammar: PRODUCTION 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^Examples CODESome TypesExpressions key:integer;array[256] of chartable[i] key mod 1999^integerppt^

CH4.7 A simple type-checker, II Dealing with declarations (no type-checking yet) Necessary bookkeeping for symbol-table construction + type checking PRODUCTIONSemantic Rule 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.val,T.type)} T ^T {T.type = pointer(T.type) }

CH4.8 A simple type-checker, III continuation… PRODUCTIONSemantic Rule 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) and (E 2.type == integer) then integer else type_error } E E 1 [E 2 ] {E.type = if (E 2.type == integer) and (E 1.type == array(s,t)) then t else type_error } E E 1 ^{E.type = if (E 1.type == pointer(t)) then t else type_error } In a similar fashion we can add boolean types.

CH4.9 Extension to Type-Checking of Statements PRODUCTIONSemantic Rule S id := E{S.type = if (lookup(id.entry)==E.type) then void else type_error } S if E then S 1 {S.type = if (E.type == boolean) then S 1.type else type_error } S while E do S 1 {S.type = if (E.type == boolean) then S 1.type else type_error } S S 1 ; S 2 {S.type = if (S 1.type == void) and (S 2.type == void) then void else type_error } Tough!

CH4.10 Structural Equivalence of Types Recursive procedure: function sequiv(s,t):boolean; begin if s and t are the same basic type 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 ) 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 end

CH4.11 Type Checking of Functions Additional type production: 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) and (E 1.type==s t) then t else type_error }

CH4.12 Type Coercions Tolerate type mismatch in expressions if no information gets lost. Tolerate type mismatch in expressions if no information gets lost. PRODUCTIONSemantic Rule E num {E.type = integer } E num.num{E.type = real } E id {E.type = lookup(id.entry)} E E 1 op E 2 {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 }

CH4.13Overloading Expressions might have a multitude of types. Expressions might have a multitude of types. Example with functions: Example with functions: E id {E.types = lookup(id.entry)} E E 1 (E 2 ) {E.types = { t | there exists a s in E 2.types such that s t belongs to E 1.types } Type error will occur when E.types becomes empty for some expression.