slike bài giảng môn chương trình dịch chương 7 symbol tables

14 283 0
slike bài giảng môn chương trình dịch chương 7 symbol tables

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Type Checking Dr. Nguyen Hua Phung Faculty of CSE HCMUT Outline • Type systems – Type expressions – Static and dynamic type checking • Equivalence of type expressions • Type conversions CSE - HCMUT Type Checking 2 Type Expressions • A basic type is a type expression. – boolean, char, integer, float, void, subrange. • A type name is a type expression. • A type constructor applied to type expressions is a type expression. Including: – Arrays: array(I,T) where I: index type, T:element type – Products: T 1 × T 2 – Records: record((name 1 × T 1 ) × (name 2 × T 2 ) ×…) – Pointers: pointer(T) – Functions: T 1 → T 2 • A type variable is a type expression. CSE - HCMUT Type Checking 3 Example • typedef int siso; ⇒ siso • int t[10]; ⇒ array(0 9,int) • int foo(int a,float b) ⇒ (int × float) → int • struct {int a;int b} ⇒ record((a × int) × (b × int)) • int *p ⇒ pointer(int) • template <class T> struct vd {T a; T b[3];} ⇒ record((a × T) × (b × array(0 2,T))) CSE - HCMUT Type Checking 4 Type Checking • Static type checking – is perfomed by the compiler – applied to typed language • Dynamic type checking – is done when the target program runs – target code needs to carry the type of an element CSE - HCMUT Type Checking 5 Equivalence of Type Expressions • Name equivalence • Structural equivalence • Structural equivalence under naming rec1 = record a : integer; b : real; end; rec2 = record c : real; d : integer; end; rec3 = record c : integer; d : real; end; rec4 = record a : integer; b : real; end; rec1 and rec4 are structural equivalence under naming rec1 and rec3 are structural equivalence CSE - HCMUT Type Checking 6 Static Type Checking for Structural Equivalence function sequiv(Type s,Type t):boolean begin if (s and t are the same basic type) then return true; else if (s = array(s1,s2) and t = array(t1,t2) then return sequiv(s1,t1) and sequiv(s2,t2); else if (s = s1 × s2 and t = t1 × t2) then return sequiv(s1,t1) and sequiv(s2,t2); else if (s = pointer(s1) and t = pointer(t1)) then return sequiv(s1,t1); else if (s = s1 → s2 and t = t1 → t2) then return sequiv(s1,t1) ; else return false; CSE - HCMUT Type Checking 7 Object-Oriented Approach Type CSE - HCMUT Type Checking 8 BasicType NumericType BooleanType PointerType ProductType FunctionType ArrayType RangeType array(I,T) IntType FloatType class ArrayType extends Type { RangeType index; Type element; } Object-Oriented Approach (cont’d) abstract class Type { abstract boolean isLeftCompatibleTo(Type t) ; } abstract class BasicType extends Type { boolean isLeftCompatibleTo(Type t) { if (this.getClass().equals(t.getClass())) return true; return false; } } class FloatType extends NumericType { boolean isLeftCompatibleTo(Type t) { if (t instanceof NumericType) return true; return false; } } CSE - HCMUT Type Checking 9 Object-Oriented Approach (cont’d) class ArrayType { RangeType index; Type element; boolean isLeftCompatibleTo(Type t) { if (t instanceof ArrayType) { ArrayType at=(ArrayType) t; return index.isLeftCompatibleTo(at.index) && element.isLeftCompatibleTo(at.element); } else return false; } } CSE - HCMUT Type Checking 10 . → s2 and t = t1 → t2) then return sequiv(s1,t1) ; else return false; CSE - HCMUT Type Checking 7 Object-Oriented Approach Type CSE - HCMUT Type Checking 8 BasicType NumericType BooleanType PointerType ProductType FunctionType ArrayType RangeType array(I,T) IntType

Ngày đăng: 23/10/2014, 17:33