編譯原理課件CHAPTER 5(Semantic Analysis and Interme.ppt_第1頁
編譯原理課件CHAPTER 5(Semantic Analysis and Interme.ppt_第2頁
編譯原理課件CHAPTER 5(Semantic Analysis and Interme.ppt_第3頁
編譯原理課件CHAPTER 5(Semantic Analysis and Interme.ppt_第4頁
編譯原理課件CHAPTER 5(Semantic Analysis and Interme.ppt_第5頁
已閱讀5頁,還剩21頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、2020/10/11,1,Chapter5Semantic Analysis and Intermediate Code Generation,語義分析概述 語法制導(dǎo)翻譯 (Syntax-Directed Translation) 類型確定與類型檢查 (Type Checking) 中間代碼生成 (Intermediate Code Generation),2020/10/11,2,5.3 類型確定與類型檢查,類型確定:確定標(biāo)識符所代表對象的數(shù)據(jù)類型,要確定類型,基本工作是處理說明部分,把相關(guān)的類型等屬性填入符號表相應(yīng)的條目中,2020/10/11,3,5.3 類型確定與類型檢查,一般的說明語

2、句的處理:P474 (Fig.8.11),注意語義動作 enter,為名字建立表項(xiàng),對第一條產(chǎn)生式及翻譯模式進(jìn)行改寫(見P474),引入標(biāo)記非終結(jié)符號(Marker Nonterminals),改寫后的語法制導(dǎo)定義是 S-屬性定義,適合于自底向上翻譯,2020/10/11,4,5.3 類型確定與類型檢查,例子:處理語句 id1 : real ; id2 : integer,1、自底向上建立分析樹,2、在建立分析樹的同時(shí)執(zhí)行語義規(guī)則完成翻譯,2020/10/11,5,5.3 類型確定與類型檢查,* 對說明語句的語義處理不產(chǎn)生中間代碼,P,D,M,D2,D1,;,T2,T1,:,id2,id1,:

3、,real,T3,integer,1,2,3,4,5,6,7,8,2020/10/11,6,5.3 類型確定與類型檢查,過程說明的處理:P477 (Fig.8.13) 針對允許嵌套過程的語言,如 PASCAL,sort,exchange,quicksort,readarray,partition,簡要說明: P475 Fig.8.12 程序參見 P416 (2)-(19)都是說明部分,2020/10/11,7,5.3 類型確定與類型檢查,P477 (Fig.8.13),此翻譯模式只是一種解決方案,語義過程 mktable,enter,addwidth,enterproc,棧 tblptr,of

4、fset,2020/10/11,8,5.3 類型確定與類型檢查,例子:分析如下程序的說明部分 畫出分析樹,詳細(xì)說明,id1 : T1 ; proc id2 ; id3 : T2 ; S,2020/10/11,9,P,D,M,D2,D1,;,;,T1,id2,proc,id1,:,;,D3,N,S,T2,id3,:,1,1. t1 := mktable ( nil ) push ( t1 , tblptr ) push ( 0, offset ),id1 : T1 ; proc id2 ; id3 : T2 ; S,t1,0,2020/10/11,10,P,D,M,D2,D1,;,;,T1,id

5、2,proc,id1,:,;,D3,N,S,T2,id3,:,1,2,2. enter ( top( tblptr), , T1.type, top(offset) ) top( offset ) := top(offset) + T1.width,id1 : T1 ; proc id2 ; id3 : T2 ; S,t1,8,2020/10/11,11,P,D,M,D2,D1,;,;,T1,id2,proc,id1,:,;,D3,N,S,T2,id3,:,1,2,3,3. t2 := mktable ( top ( tblptr ) ) push ( t2 , tblptr

6、) push ( 0, offset ),id1 : T1 ; proc id2 ; id3 : T2 ; S,t1,8,t2,0,2020/10/11,12,P,D,M,D2,D1,;,;,T1,id2,proc,id1,:,;,D3,N,S,T2,id3,:,1,2,3,4,4. enter ( top( tblptr), , T2.type, top(offset) ) top( offset ) := top(offset) + T2.width,id1 : T1 ; proc id2 ; id3 : T2 ; S,t1,8,t2,4,2020/10/11,13,P,D

7、,M,D2,D1,;,;,T1,id2,proc,id1,:,;,D3,N,S,T2,id3,:,1,2,3,4,5,5. t := top ( tblptr ) addwidth ( t, top ( offset ) ) pop ( tblptr ) pop ( offset ) enterproc ( top( tblptr), , t ),id1 : T1 ; proc id2 ; id3 : T2 ; S,t1,8,2020/10/11,14,P,D,M,D2,D1,;,;,T1,id2,proc,id1,:,;,D3,N,S,T2,id3,:,1,2,3,4,5,6

8、,7,6. 7. addwidth ( top ( tblptr ), top ( offset ) ) pop ( tblptr ) pop ( offset ),id1 : T1 ; proc id2 ; id3 : T2 ; S,2020/10/11,15,P,D,M,D2,D1,;,;,T1,id2,proc,id1,:,;,D3,N,S,T2,id3,:,1,2,3,4,5,6,7,id1 : T1 ; proc id2 ; id3 : T2 ; S,2020/10/11,16,5.3 類型確定與類型檢查,記錄類型的處理:P477 (Fig.8.14),例子:分析句型 record

9、id1 : T ; id2 : T end,2020/10/11,17,5.3 類型確定與類型檢查,T,D,record,D2,L,D1,;,T2,T1,:,id2,id1,:,end,1,2,3,4,5,2020/10/11,18,5.3 類型確定與類型檢查,類型檢查(Type Checking):按照所用語言的語義規(guī)則檢查運(yùn)算的合法性與運(yùn)算分量類型的一致性或相容性,2020/10/11,19,5.3 類型確定與類型檢查,表達(dá)式的類型檢查(Type Checking of Expressions),1、E num E.type := integer ,2、E id E.type := loo

10、kup(id.entry) ,2020/10/11,20,5.3 類型確定與類型檢查,3、E E1 op E2 E.type := if E1.type=integer and E2.type=integer then integer else type-error 一致性檢查 * 假設(shè) op 的運(yùn)算分量必須是integer,結(jié)果也是integer,2020/10/11,21,5.3 類型確定與類型檢查, E.type := if E1.type=integer and E2.type=integer then interger else if E1.type=integer and E2.t

11、ype=real then real else if E1.type=real and E2.type=integer then real else if E1.type=real and E2.type=real then real else type-error 相容性檢查 必要的話可以使用強(qiáng)制轉(zhuǎn)換函數(shù) inttoreal * 假設(shè) op 的運(yùn)算分量可以是 integer 和 real,2020/10/11,22,5.3 類型確定與類型檢查,4、E E1 E2 E.type := if E2.type=integer and E1.type=array(s,t) then t else t

12、ype-error ,5、E E1 E.type := if E1.type=pointer(t) then t else type-error ,2020/10/11,23,5.3 類型確定與類型檢查,例:,E1,E,E4,E2,id1,num,+,*,E3,id2,1,2,3,4,5,2020/10/11,24,5.3 類型確定與類型檢查,語句的類型檢查(Type Checking of Statements),1、S id:=E S.type := if id.type=E.type then void else type-error ,2、S if E then S1 S.type := if E.type=boolean then S1.type else type-error ,2020/10/11,25,5.

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論