Cp6_n
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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
wk.baidu.com
Chapter 6 Type Checking Section 4 Type conversion • Coercions – Implicit type conversions • Explicit conversions – Chr(55)
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 syntaxdirected manner 3)Different type systems may be used by different compilers or processors of the same language
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 S1 – {S.type= if (E.type==boolean) S1.type 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 2 Specification of a Simple Type Checker 2. Type Checking of Expressions – E E1[E2] – {E.type= if (E2.type==integer) && (E1.type==array(s,t) t else type_error} – E E1^ – {E.type= if (E1.type==pointer(t)) t else type_error}
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 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 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. – PD;E – DD;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,T1.type)} – T ^T1 {T.type=pointer(T1.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 E1 mod E2 – {E.type= if (E1.type==integer) && (E1.type==integer) integer else type_error}
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 0 overview
2.Position of type checker
Token stream parser Syntax Type tree checker Intermedi Syntax ate code Intermediate tree generator representation
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”
Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker 1. A simple language 1)Grammar for source language – PD;E – DD;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 3. Type Checking of Statements – S while E do S1 – {S.type= if (E.type==boolean) S1.type else type_error} – S S1;S2 – {S.type= if (S1.type==void) && (S2.type== void) void else type_error}
Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker 4. Type Checking of Functions – T T1 „‟ T2 {T.type= T1 .type T2 .type} – E E1 (E2) – {E.type= if (E2.type==s) &&(E1.type==st) t else type_error}
wk.baidu.com
Chapter 6 Type Checking Section 4 Type conversion • Coercions – Implicit type conversions • Explicit conversions – Chr(55)
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 syntaxdirected manner 3)Different type systems may be used by different compilers or processors of the same language
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 S1 – {S.type= if (E.type==boolean) S1.type 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 2 Specification of a Simple Type Checker 2. Type Checking of Expressions – E E1[E2] – {E.type= if (E2.type==integer) && (E1.type==array(s,t) t else type_error} – E E1^ – {E.type= if (E1.type==pointer(t)) t else type_error}
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 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 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. – PD;E – DD;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,T1.type)} – T ^T1 {T.type=pointer(T1.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 E1 mod E2 – {E.type= if (E1.type==integer) && (E1.type==integer) integer else type_error}
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 0 overview
2.Position of type checker
Token stream parser Syntax Type tree checker Intermedi Syntax ate code Intermediate tree generator representation
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”
Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker 1. A simple language 1)Grammar for source language – PD;E – DD;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 3. Type Checking of Statements – S while E do S1 – {S.type= if (E.type==boolean) S1.type else type_error} – S S1;S2 – {S.type= if (S1.type==void) && (S2.type== void) void else type_error}
Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker 4. Type Checking of Functions – T T1 „‟ T2 {T.type= T1 .type T2 .type} – E E1 (E2) – {E.type= if (E2.type==s) &&(E1.type==st) t else type_error}