1 Pertemuan 18 & 19 Semantic Analyzer (type checking) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.

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.
CPSC 388 – Compiler Design and Construction
Semantic Analysis and Symbol Tables
Chapter 6 Intermediate Code Generation
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.
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.
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
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.
1 Pertemuan 14 Object Query Language (Lanjutan bagian 1) Matakuliah: M0174/OBJECT ORIENTED DATABASE Tahun: 2005 Versi: 1/0.
Chapter 6 Type Checking Section 0 Overview
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
1 Pertemuan 6 The structure part of object data model (Lanjutan) Matakuliah: M0174/OBJECT ORIENTED DATABASE Tahun: 2005 Versi: 1/0.
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
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
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.
Semantic Analysis CS 671 February 5, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens –e.g.: main$
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Names Variables Type Checking Strong Typing Type Compatibility 1.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Semantic Analysis Semantic Analysis v Lexically and syntactically correct programs may still contain other errors v Lexical and syntax analyses.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Static Checking and Type Systems Chapter 6. 2 Static versus Dynamic Checking Static checking: the compiler enforces programming language’s static semantics.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
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.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
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
Type Checking and Type Inference
Semantic Analysis Type Checking
Constructing Precedence Table
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Semantic Analysis Chapter 6.
Chapter 6 Intermediate-Code Generation
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Compiler Construction
Compiler Construction
Presentation transcript:

1 Pertemuan 18 & 19 Semantic Analyzer (type checking) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6

2 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mahasiswa dapat menjelaskan proses type checking dan peranannya dalam compiler (C2) Mahasiswa dapat menunjukkan berbagai metode type checking dalam teknik kompilasi (C3)

3 Outline Materi Type system Spesifikasi type checker sederhana Type expression Type conversion Operator dan fungsi overloading Fungsi-fungsi polymorphic

4 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

5 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.

6 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) }

7 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 }

8 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 }

9 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

10 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

11 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.

12 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.

13 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

14 Static Checking Static checking aims to ensure, at compile time, that syntactic and semantic constraints of the source language are obeyed. E.g.: –Type checks: operators and operands must have compatible types. –Flow-of-control checks: control transfer statements must have legitimate targets (e.g., break/continue statements). –Uniqueness checks: a language may dictate unique occurrences in some situations, e.g., variable declarations, case labels in switch statements. These checks can often be integrated with parsing.

15 Data Types and Type Checking A data type is a set of values together with a set of operations that can be performed on them. Type checking aims to verify that operations in a program code are, in fact, permissible on their operand values. Reasoning about types: –The language provides a set of base types and a set of type constructors; –The compiler uses type expressions to represent types definable by the language.

16 Type Constructors and Type Expressions A type expression denotes (i.e., is a syntactic representation of) the type of a program entity: –A base type is a type expression (e.g., boolean, char, int, float); –A type name is a type expression; –A type constructor applied to type expressions is a type expression, e.g.: arrays: if T is a type expression then so is array(T); records: if T 1, …, T n are type expressions and f 1, …, f n is a list of (unique) identifiers, then record(f 1 :T 1, …, f n :T n ) is a type expression; pointers: if T is a type expression then so is ptr(T); functions: if T, T 1, …, T n are type expressions, then so is (T 1, …, T n )  T.

17 Why use Type Expressions? char *X; char **f() { X = “GOTCHA!” return &X; } main() { printf(“%c\n”, (*f())[2]); /* legal??? */ } Program Code Type Expression Rule f ()  ptr(ptr(char)) symbol table lookup f() ptr(ptr(char)) if e : T 1  T 2 and e 1 : T 1 then e(e 1 ) : T 2 *f() ptr(char) if e : ptr(T) then *e : T *f() array(char) if e : ptr(T) then e : array(T) (*f()) array(char) if e : T then (e) : T 2 int base type (*f())[2] char if e 1 : array(T) and e 2 : int then e 1 [e 2 ] : T What about: qsort((void **)lptr,0,k,(int (*)(void*,void*))(num ? ncmp : strcmp));

18 Notions of Type Equivalence 1.Name equivalence: In some languages (e.g., Pascal), types can be given names. Name equivalence views distinct type names as distinct types: two types are name equivalent if and only if they have the same type name. 2.Structural equivalence: Two type expressions are structurally equivalent if they have the same structure, i.e., if both apply the same type constructor to structurally equivalent type expressions. E.g.: in the Pascal fragment type p =  node; q =  node; var x : p; y : q; x and y are structurally equivalent, but not name-equivalent.

19 Representing Type Expressions Type graphs: A graph-structured representation of type expressions: –Basic types are given predefined “internal values”; –Named types can be represented via pointers into a hash table. –A composite type expression f (T 1,…,T n ) is represented as a node identifying the constructor f and with pointers to the nodes for T 1, …, T n. E.g.: int x[10][20]:

20 Type Checking Expressions ProductionSemantic RuleYacc Code E → idE. type = id. type { $$ = symtab_lookup(id_name); } E → intconE. type = INTEGER{ $$ = INTEGER; } E → E 1 + E 2 E. type = result_type(E 1. type, E 2. type ) { $$ = result_type($1, $3); } /* arithmetic type conversions */ Type result_type(Type t1, Type t2) { if (t1 == error || t2 == error ) return error ; if (t1 == t2) return t1; if (t1 == double || t2 == double ) return double ; if (t1 == float || t2 == float ) return float ; … } Return types: – currently: the type of the expression – down the road: type location code to evaluate the expression

21 Type Checking Expressions: cont’d Arrays: E → id[ E 1 ] { t 1 = id. type ; if ( t 1 == ARRAY  E 1. type == INTEGER) E. type = id. element_type ; else E. type = error ; }

22 Type Checking Expressions: cont’d Function calls: E → id ‘(‘ expr_list ‘)’ { if (id. return_type == VOID) E. type = error ; else if ( chk_arg_types(id, expr_list) ) /* actuals match formals in number, type */ E. type = id. return_type ; else E. type = error ; }

23 Type Checking Statements Different kinds of statements have different type requirements. E.g.: –if, while statements may require boolean conditiona; –LHS of an assignment must be an “l-value”, i.e., something that can be assigned. –LHS and RHS of an assignment must have “compatible” types. If they are of different types, conversion will be necessary.

24 Operator Overloading Overloading refers to the use of the same syntax to refer to different operations, depending on the operand types. E.g.: in Java, ‘+’ can refer to integer addition, floating point addition, or string concatenation. The compiler uses operand type information to resolve the overloading, i.e., figure out which operation is actually referred to. If there is insufficient information to resolve overloading, the compiler may give an error.