Presentation is loading. Please wait.

Presentation is loading. Please wait.

类型表达式  类型的等价性 按名等价: type tp=array[1..10]of integer; var a,b:tp; 则称 a,b 是相同类型的变量 结构等价: type tp1=array[1..10]of integer type tp2=array[1..10]of integer;

Similar presentations


Presentation on theme: "类型表达式  类型的等价性 按名等价: type tp=array[1..10]of integer; var a,b:tp; 则称 a,b 是相同类型的变量 结构等价: type tp1=array[1..10]of integer type tp2=array[1..10]of integer;"— Presentation transcript:

1 类型表达式  类型的等价性 按名等价: type tp=array[1..10]of integer; var a,b:tp; 则称 a,b 是相同类型的变量 结构等价: type tp1=array[1..10]of integer type tp2=array[1..10]of integer; var a:tp1; b:tp2; 则称 a,b 是相同类型的变量  类型的相容性

2 类型相容性  运算分量类型的相容性;  赋值语句左右类型的相容性;  形参和实参类型的相容性;  类型相容:  类型等价则相容  整型或整型子界类型与整型、实型相容  两个子界类型定义的范围相包含则相容  对结构类型,如果同一结构,且子类型 相容则相容

3 类型的分析  作用:构造类型的内部表示,类型检查。  分析过程:读 Token 序列,识别出各种类型, 类型检查,返回类型内部表示的地址。 array [ 1.. 10 ] of integer arrKind … low=1 tp1=intPtr … up=10 tp2=intPtr IndexPtr= (1,subTy, intPtr,1,10) … ElemPtr=intPtr size=(up-low+1) * sizeof (int) aPtr := ( size, arrTy, IndexPtr, ElemPtr )

4  类型出现的位置: 类型定义 TYPE id = t; 变量声明 VAR id : t; 过 / 函声明 Proce/Func P(A 1 :t 1, … )(:t)  类型的种类: name,subrange,enum,array,record,set, file,pointer ……  类型分析模块: 类型检查,返回类型的内部表示地址 Ptr 和 Forward

5 NameType  形式: id ( 类型标识符 )  处理思想:  查符号表  无声明错  typekind ?  TypePtr 为 Ptr 的值  Forward:=0

6 EnumType  形式:( a 1, …,a n )  处理思想:  生成 a 1, … a n 的符号表 EntryList: (a i , Ptr , consKind , i) , Ptr 需回填  生成内部表示 : Ptr:= ★ (enumSize , enumTy,EntryList,n)  回填 EntryList 中的 Ptr 值  Forward:=0

7 SubRangeType  形式: C 1..C 2  处理思想:  从 C 1 求出其内部类型地址 Ptr 1 和值 N 1 ;  从 C 2 求出其内部类型地址 Ptr 2 和值 N 2 ;  检查 Ptr 1 = Ptr 2 , N 1  N 2 ;  Ptr:= ★ (subSize,subTy,Ptr 1,N 1,N 2 )  Forward := 0

8 ArrayType  形式: array T 0 of T 1  处理思想:  返回 T 0,T 1 的内部表示地址 IndexPtr,ElemPtr  判定 IndexPtr 是否为下标类型  计算 size = (IndexPtr ↑. Up- IndexPtr ↑. Low +1)  (ElemPtr ↑. Size);  Ptr:= ★ (size,arrayTy,IndexPtr,ElemPtr)

9 SetType  形式: set of T  处理思想:  返回 T 的内部表示地址 BasePtr;  检查 BasePtr 是否为有序类型 ;  Ptr:= ★ (setSize,setTy,BasePtr)

10 FileType  形式: file of T  处理思想:  返回 T 的内部表示地址 compPtr ;  Ptr:= ★ (fileSize,fileTy,CompPtr) 返回地址 Ptr 。

11 PointerType  形式: ↑Q  处理思想:  由 Q 查表,若找到并且 Q 为类型标识符,其内 部表示指针返回给 QEntry ,并且令 Forward := 0 ; 否则 QEntry 等待回填,令 Forward:=QEntry ;  Ptr:= ★ (PoinSize,PoinTy,QEntry ) ;

12 RecordType  形式: record 不变体;变体 end  处理思想:  构造不变体部分内部表示返回地址 fixBodyPtr  构造变体部分内部表示返回地址 variBodyPtr  计算记录类型长度 RecordSize  Ptr:= ★ (Size,recordTy,fixBodyPtr,variBodyPtr)

13 Offset 原理  初始: off := 0;  不变体: id:T Offset(id):= off; off:= off + size(T) ;  变体: case u:T u of C i : RecBody i ; Offset(u):= off; off:=off + size(T u ) ; Offset(RecBody i )=off; off i :=off + sizeof(RecBody i ) off := Max(off i )  RecordSize := off;

14 FixBody 形式: FixUnit {; FixBody} FixUnit = id : Type 处理思想: 构造 FixUnit 的内部表示结点 fixU : fixU := ★ (name,tp,off,nil) 如果 FixBody 部分为空,则 fixB := fixU; 否则 重复上述过程构造 FixBody 部分的内部表 示 fixB 。 fixB := Link(fixU,fixB) ;

15 VariBody 形式: CaseUnit VariUnits 处理思想: 构造 CaseUnit 的内部表示 caseU : caseU := ★ (name, tp, off); 构造 VariUnits 的内部表示 variUS: variU := ★ (c,fixB,variB,nil); variUS:= ★ Link(variU,variUS) 变体部分的内部表示 variB : variB := ★ (caseU, variUS)


Download ppt "类型表达式  类型的等价性 按名等价: type tp=array[1..10]of integer; var a,b:tp; 则称 a,b 是相同类型的变量 结构等价: type tp1=array[1..10]of integer type tp2=array[1..10]of integer;"

Similar presentations


Ads by Google