Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.

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.
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
SYMBOL TABLES &CODE GENERATION FOR EXECUTABLES. SYMBOL TABLES Compilers that produce an executable (or the representation of an executable in object module.
Chapter 6 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.
Structures in C.
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.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
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,
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.
CH4.1 CSE244 Type Checking Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
Compiler Construction
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.
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.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Type Equivalence Rules Ada –Strict name equivalence except for almost everything Unique array constructors give rise to unique types Subtypes can create.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lesson 7 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 October 25, October 25, 2015October 25, 2015October 25, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
Lecture 8 Semantic Analysis.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
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.
Object Oriented Programming Lecture 2: BallWorld.
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.
Arithmetic Instructions. Integer and Float Conversions.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types
Compiler Design – CSE 504 Type Checking
Semantics Analysis.
Principles of programming languages 12: Functional programming
Semantic Analysis Type Checking
CSE 3302 Programming Languages
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 DEFINITION
Compiler Construction
Functions and Recursion
Compiler Construction
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Outline Type systems –Checking for type errors –Type equivalence 2

TYPE SYSTEMS 3

Type systems Specifies how to assign types to computed values Can be expressed with an SDD Example based on this grammar on the next slides → P→ D ; E D→ D ; D | id : T T→ char | integer | array [ num ] of T | ^T | T -> T E→ literal | num | id | E mod E | E [ E ] | E^ | E(E) P→ D ; E D→ D ; D | id : T T→ char | integer | array [ num ] of T | ^T | T -> T E→ literal | num | id | E mod E | E [ E ] | E^ | E(E) 4

SDT for collecting type information from declarations ProductionSemantic action P→ D ; E D→ D ; D | id : T{ addtype(id.name, T.type) } T→ char{ T.type = char } | integer { T.type = integer } | array [ num ] of T 1 { T.type = array(num.val, T 1.type) } | ^T 1 { T.type = pointer(T 1.type) } | T 1 -> T 2 { T.type = T 1.type → T 2.type } 5

SDT for type checking of expressions ProductionSemantic action E→ literal{ E.type = char } | num{ E.type = integer } | id{ E.type = lookup(id.name) } | E 1 mod E 2 { E.type = if E 1.type == E 2.type == integer then integer else type_error } | E 1 [ E 2 ]{ E.type = if E 1.type == array(n, t) and E 2.type == integer then t else type_error } | E 1 ^{ E.type = if E 1.type == pointer(t) then t else type_error } | E 1 (E 2 ){ E.type = if E 1.type == s → t and E 2.type == s then t else type_error } 6

Exercise (1) Assume that lookup(”a”) = array(78, pointer(int)) and lookup(”i”) = integer. Draw the parse tree of the following expression and assign types to all nodes: a[i]^ mod 38 Hint 1: The same expression would look like this in C: *a[i] % 38 Hint 2: Work bottom-up! 7

Type checking of statements Types on statements: –void: No type errors in embedded expressions –type_error: Type errors in expressions We add statements to the grammar: S→ id := E | if E then S | while E do S | S ; S 8

SDT for type checking of statements ProductionSemantic action S→ id := E{ S.type = if lookup(id.name) == E.type then void else type_error } | if E then S 1 { S.type = if E.type == integer then S 1.type else type_error } | while E do S 1 { S.type = if E.type == integer then S 1.type else type_error } | S 1 ; S 2 { S.type = if S 1.type == S 2.type == void then void else type_error } 9

Equivalence of type expressions When are two types equal? Example: Is the following legal? int* arr[72]; … int x = *arr[19]; Two views: Structural equivalence – compare the type expressions recursively Name equivalence – types with different names are always considered non-equivalent 10

Structural type equivalence bool equiv(s, t) if s and t are the same basic type then return true else if s == array(n 1, s') and t == array(n 2, t') then return n 1 == n 2 and equiv(s', t’) else if s == s' × s'' and t == t' × t'' then return equiv(s', t') and equiv(s'', t'') else if s == pointer(s') and t == pointer(t') then return equiv(s', t’) else if s == s' → s'' and t == t' → t'' then return equiv(s', t') and equiv(s'', t'') else return false 11

Name equivalence More strict than structural equivalence User-defined type names are viewed the same way as basic types Example: Here the types of A and B are structurally equivalent, but not name- equivalent: typedef int IntAlias; typedef struct AStruct { IntAlias x; } A; typedef struct BStruct { int y; } B; 12

Type equivalence example: the language C User-defined type names in C: Structs and unions: name equivalence is used Typedefs and enums: structural equivalence is used struct A { int x; }; struct B { int x; }; typedef struct A C; typedef struct A D; typedef D E; void some_func(void) { struct A a; struct B b; C c; D d; E e; a = b;// Error c = d;// OK c = e;// OK } 13

Recursively defined types What is the type of Link in the following C code? struct Link { void* data; struct Link* next; }; 14

Recursively defined types With name equivalenceWith structural equivalence 15 void pointer nextdata × ×× void pointer nextdata × ×× Link Link = record

Implicit type conversions ProductionSemantic action E → E 1 + E 2 { E.type = if E 1.type == E 2.type == int then int else if E 1.type == E 2.type = real then real else if E 1.type == int and E 2.type = real then real else if E 1.type == real and E 2.type == int then real else type_error } 16

Explicit type conversions Appear as operators to the type checker Syntax tree for x + (int) y: 17 + id toint id

Conclusion A type system is a set of rules for assigning types to parts of a program –Can be specified as an SDD A type checker checks that these rules are adhered to –Can be specified as an SDT With name equivalence, named types are considered equivalent only if they have the same name With structural type equivalence, types with different names but equivalent structures are considered equivalent as well 18

Next time More stack machine code –Functions and activation records 19