Chapter 5 Semantic Analysis and Intermediate Code Generation





















![5. 3 类型确定与类型检查 n n 4、E → E 1 [ E 2 ] { 5. 3 类型确定与类型检查 n n 4、E → E 1 [ E 2 ] {](https://slidetodoc.com/presentation_image_h2/8dce67562d60b62b8b7c94eb3f7c3e12/image-22.jpg)




- Slides: 26
Chapter 5 Semantic Analysis and Intermediate Code Generation n n 9/6/2021 语义分析概述 语法制导翻译 (Syntax-Directed Translation) 类型确定与类型检查 (Type Checking) 中间代码生成 (Intermediate Code Generation) 1
5. 3 类型确定与类型检查 P 8 1 M D 3 ε id 1 real 0 id 2 pointer(integer 8 ) D 1 : ; 7 D 2 T 1 id 2 : 2 real 6 T 2 5 ↑ T 3 4 integer * 对说明语句的语义处理不产生中间代码 9/6/2021 5
5. 3 类型确定与类型检查 n P 477 (Fig. 8. 13) n n n 9/6/2021 此翻译模式只是一种解决方案 语义过程 mktable,enter,addwidth, enterproc 栈 tblptr,offset 7
5. 3 类型确定与类型检查 n 例子:分析如下程序的说明部分 画出分 析树,详细说明 id 1 : T 1 ; proc id 2 ; id 3 : T 2 ; S 9/6/2021 nil header id 1 real id 2 proc 0 t 1 header id 3 integer 0 8
id 1 : T 1 ; proc id 2 ; id 3 : T 2 ; S P 1 M D D 1 ε id 1 : ; D 2 T 1 proc id 2 ; N ε t 1 0 D 3 id 3 : ; S T 2 1. t 1 : = mktable ( nil ) push ( t 1 , tblptr ) push ( 0, offset ) 9/6/2021 9
id 1 : T 1 ; proc id 2 ; id 3 : T 2 ; S P 1 M D 2 ε id 1 D 1 : ; D 2 T 1 proc id 2 ; N ε t 1 8 D 3 id 3 : S ; T 2 2. enter ( top( tblptr), id 1. name, T 1. type, top(offset) ) top( offset ) : = top(offset) + T 1. width 9/6/2021 10
id 1 : T 1 ; proc id 2 ; id 3 : T 2 ; S P 1 M D 2 ε id 1 t 2 0 t 1 8 D 1 : ; D 2 T 1 proc id 2 ; 3 N ε D 3 id 3 : ; S T 2 3. t 2 : = mktable ( top ( tblptr ) ) push ( t 2 , tblptr ) push ( 0, offset ) 9/6/2021 11
id 1 : T 1 ; proc id 2 ; id 3 : T 2 ; S P 1 M D 2 ε id 1 D 1 : ; D 2 T 1 proc id 2 ; 3 N ε t 2 4 t 1 8 D 3 4 ; id 3 : S T 2 4. enter ( top( tblptr), id 3. name, T 2. type, top(offset) ) top( offset ) : = top(offset) + T 2. width 9/6/2021 12
id 1 : T 1 ; proc id 2 ; id 3 : T 2 ; S P 1 M 2 ε id 1 t 1 D 8 D 1 : ; 5 D 2 T 1 proc id 2 ; 3 N ε D 3 4 ; id 3 : S T 2 5. t : = top ( tblptr ) addwidth ( t, top ( offset ) ) pop ( tblptr ) pop ( offset ) enterproc ( top( tblptr), id 2. name, t ) 9/6/2021 13
id 1 : T 1 ; proc id 2 ; id 3 : T 2 ; S P 7 1 M D 2 ε id 1 D 1 : ; 6 5 D 2 T 1 proc id 2 ; 3 N ε D 3 4 ; id 3 : S T 2 6. 7. addwidth ( top ( tblptr ), top ( offset ) ) pop ( tblptr ) pop ( offset ) 9/6/2021 14
id 1 : T 1 ; proc id 2 ; id 3 : T 2 ; S P 7 1 M D 2 ε id 1 9/6/2021 D 1 6 5 D 2 ; T 1 proc id 2 : nil header id 1 real id 2 proc ; 3 N ε D 3 4 ; id 3 : S T 2 0 t 1 header id 3 integer 0 15
5. 3 类型确定与类型检查 T 1 L record ε id 1 9/6/2021 5 2 D 1 : D 4 end ; 3 D 2 T 1 id 2 : T 2 17
5. 3 类型确定与类型检查 n 表达式的类型检查(Type Checking of Expressions) n 1、E → num { E. type : = integer } n 2、E → id { E. type : = lookup(id. entry) } 9/6/2021 19
5. 3 类型确定与类型检查 n 3、E → E 1 op E 2 { E. type : = if E 1. type=integer and E 2. type=integer then integer else type-error } — 一致性检查 * 假设 op 的运算分量必须是integer,结果 也是integer 9/6/2021 20
5. 3 类型确定与类型检查 { E. type : = if E 1. type=integer and E 2. type=integer then interger 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 } — 相容性检查 必要的话可以使用强制转换函数 inttoreal * 9/6/2021 假设 op 的运算分量可以是 integer 和 real 21
5. 3 类型确定与类型检查 n n 4、E → E 1 [ E 2 ] { E. type : = if E 2. type=integer and E 1. type=array(s, t) then t else type-error } 5、E → E 1↑ { E. type : = if E 1. type=pointer(t) then t else type-error } 9/6/2021 22
5. 3 类型确定与类型检查 n 例: 5 3 E 1 E 2 id 1 9/6/2021 + 1 * E 2 E 3 E 4 4 num id 2 23
5. 3 类型确定与类型检查 n 语句的类型检查(Type Checking of Statements) n n 1、S → id: =E { S. type : = if id. type=E. type then void else type-error } 2、S → if E then S 1 { S. type : = if E. type=boolean then S 1. type else type-error } 9/6/2021 24
5. 3 类型确定与类型检查 n n 3、S → while E do S 1 { S. type : = if E. type=boolean then S 1. type else type-error } 4、S → S 1; S 2 { S. type : = if S 1. type=void and S 2. type=void then void else type-error } 9/6/2021 25
9/6/2021 26