



下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、各種語言成分的語法及其翻譯方案(示例)1. 普通聲明語句的翻譯 下面是聲明語句的文法:Pf prog id (input , output ) D ; S D f D ; D | List : T | proc id D ; S List f Listi, id | idT f integer | real | array C of Ti | Ti | record DC f num C | £聲明語句的翻譯模式:Pfprog id (input , output) offset := 0 D ; SDfD ; DDfid : Tenter (id .name, T.type, of
2、fset); offset:= offset + T.widthTfintegerT.type := integer; T.width :=4Tfreal T.type :=real; T.width :=8Tf array num of Ti T.type := array (num .val, T1.type);T.width := num .val 燈1.widthTf TTi T.type := pointer (T1.type); T.width := 42. 嵌套過程中聲明語句的翻譯 嵌套過程聲明語句的產(chǎn)生式。Pfprog id (input , output ) D ; SDfD
3、 ; D | id: T | proc id ; D ; S(7.1)嵌套過程聲明語句的翻譯模式:Pfprog id (input , output) MD ;S addwidth (top(tblptr),top(offset);pop(tblptr);pop(offset)Mf t := mktable(nil );push(t, tblptr); push(0, offset)DfD1Q2Dfprocid ; N D1 ; St:= top(tblptr); addwidth (t, top(offset); pop(tblptr);pop(offset); enterproc(top(
4、tblptr ), id .name,t)D f id : T enter(top(tblptr), id .name, T.type, top(offset);top(offset) := top(offset) + T.widthN f t:= mktable(top(tblptr); push(t, tblptr); push(0, offset)3. 記錄的翻譯下面是生成記錄類型的產(chǎn)生式:Tfrecord D end生成記錄類型的翻譯模式:T f record L D end T.type := record(top(tblptr);T.width := top(offset);pop
5、(tblptr); pop(offset)L f t:= mktable(nil); push(t, tblptr); push(0, offset)4. 賦值語句的翻譯 下面是典型的賦值語句文法:SfLeft := EE - E1 + E2 | E1 * E2 |- E1 |(E1 )| LeftLeftfElist | id日ist f Elist, E |id E(7.2)賦值語句的翻譯模式:(1) Sf Left:=Eif Left.offset=null then /*Left 是簡單變量 id*/ gencode(Left.addr ':=' E.addr);els
6、egencode(Left.addr '' Left.offset ' ' ':=' E.addr) /* Left 是數(shù)組元素*/ EfEi+E2E.addr:=newtemp;gencode(E.addr ':='Ei.addr'+'E2.addr) E-(Ei) E.addr:= Ei.addr(4) EfLeftif Left.offset=null then /*Left 是簡單 id*/E. addr:= Left.addrelse begin/* Left 是數(shù)組元素*/E. addr:=newtem
7、p;gencode(E.addr ':=' Left.addr ' ' Left.offset '')endLeftfElist Left.addr:=newtemp;/* Left是數(shù)組元素,因此存放基址和位移*/Left.offset:=newtemp;gencode(Left.addr ':=' c(Elist .array);gencode(Left.offset ':=' Elist.addr '*' width(Elist.array )(6) Leftfid Left.addr:=id
8、 .addr; Left.offset=null ElistElisti, E t:=newtemp;m:= Elisti.ndim+1;gencode(t ':='曰isti.addr '*' limit (Elist i.array , m); /* 計算 em-i Xnm */gencode(t ':=' t '+' E. addr);/* 計算 + im */Elist.array:= Elist i.array ;Elist.addr:=t;Elist.ndim:=m Elistid E Elist .array :=i
9、d .addr; Elist.addr:= E.addr;曰ist.ndim :=15.各種控制結(jié)構(gòu)的翻譯5.1 布爾表達式的翻譯布爾表達式的文法為: B-B or M B2 BfB and M B2 Bfnot B1(4) B-(B1) B-E relop E2(6) B-true BffalseMf e布爾表達式的翻譯模式如下所示:B-B1 or m B2 backpatch(B1 .falselist, M.quad);B.truelist := merge(B1.truelist , B2.truelist );B.falselist := B2.falselistB- B1 and
10、M B2 backpatch(B1 .truelist, M.quad);B.truelist := B2.truelist ;B.falselist := merge(B1.falselist, B2.falselist)B-not BB.truelist := B1.falselist; B.falselist := B1 .truelist B-(B1) B.truelist := B1.truelist; B.falselist := B1.falselistB-Erelop E2B.truelist kmakelist(nextquad);B.falselist := makelis
11、t(nextquad+1);gencode('if Ei.addr relop .opEi.addr'goto -); gencode('goto -) Bftrue B.truelist := makelist(nextquad); gencode('goto 力BffalseB.falselist := makelist(nextquad); gencode('goto 力Mf M.quad := nextquad5.2 常用控制流語句的翻譯控制流語句if-then , if-then-else和while-do的文法為: Sf if B then
12、Si S- if B then S else S2 Sf while B do Si Sf begin L endSf A LfLi;S LfS(7.9)if-then , if -then -else 和 while -do 語句的翻譯模式:Sf if B then M1 Si N else M2 S2 backpatch(B.truelist, M.quad);backpatch(B.falselist , M 2.quad);S.nextlist := merge(S1.nextlist, merge(N. nextlist, S2. nextlist)N N.nextlist := m
13、akelist(nextquad); gencode('goto -)Mf M.quad := nextquadSf if B then M Si backpatch(B.truelist, M.quad);S.nextlist := merge(B.falselist , S1. nextlist)Sf while Mi b do M2 Si backpatch(S1.nextlist, M.quad);backpatch(B.truelist ,M2.quad);S.nextlist:= B.falselist; gencode('goto'M1.quad) Sf
14、begin L end S.nextlist:= L.nextlist Sf A S.nextlist := nil L-L1;MSbackpatch(L1.nextlist, M .quad); L.nextlist := S.nextlistLfS( L.nextlist := S.nextlist1.3 for循環(huán)語句的翻譯for 循環(huán)語句的文法如下所示:Sf for id := Ei to E2 step E3 do Sifor 循環(huán)語句的翻譯模式如下所示:Sf for id := Ei to E2 step E3 do M Si backpatch(Si.nextlist, M.ag
15、ain,);gencode( goto-, -, ,M.again); S.nextlist := M.again ;Mf e M.addr := entry(id); gencode( ' :=Ei.addr, -, M.addr); Ti:=newtemp;gencode( :=E2.a,ddr, -, T1); T2:=newtemp; gencode( :=E3.a,ddr, -, T2); q:=nextquad; gencode( goto-, -, ,q+2); M .again :=q+1; gencode( +M .a, ddr, T2, M.addr);M.next
16、list:=nextquad; gencode( 'ifM.'addr >'Ti 'goto-' );1.4 repeat語句的翻譯repeat 語句的文法如下所示:S- repeat Si until BRepeat 語句的翻譯模式如下所示:Sf repeat M Siuntil N B backpatch( B.falselist,M .quad);S.nextlist :=B.truelistMf M.quad := nextquadNf e( backpatch (Si .nextlist, nextquad)6. switch 語句的語法
17、制導(dǎo)翻譯switch 語句的文法為:Sf switch (E ) ClistClistf caseV : S Clist | default : Sswitch 語句的翻譯模式如下所示:Sf switch (E)i:=0; Si.nextlist:=0; push Si.nextlist; push E.addr; push i; q:=0; push qClist pop q;pop i;pop E.addr;pop Si .nextlist; S.nextlist :=merge(Si.nextlist , q); push S.nextlist Clistf caseV :pop q;
18、pop i; i:=i+1; pop E.addr;if nextquad w0 then backpatch(q, nextquad);q:=nextquad;gencode( if' E.addr '卻i 'gotcLi);push E.addr; push i;push qSpop q; pop i; pop E.addr; pop Si-1.nextlist; p:=nextquad;gencode( goto -); gencode(Li:);Si.nextlist:=merge(Si.nextlist, p);Si.nextlist:=merge(Si.ne
19、xtlist, Si-1. nextlist);push Si.nextlist; push E.addr ; push i; push q Clist Clistfdefault :pop q; pop i; i:=i+1; pop E.addr;if nextquad w0 then backpatch(q, nextquad);q:=nextquad;gencode( if' E.addr '卻i ' gotoV+i);push E.addr; push i;push q Spop q; pop i ; pop E.addr; pop Si-1 .nextlist
20、; p:= nextquad;gencode(goto -); gencode(Li:);Si.nextlist :=merge(Si.nextlist, p);Si.nextlist :=merge(Si. nextlist, Si-1 .nextlist);push Si.nextlist; push E.addr; push i; push q7. 過程調(diào)用和返回語句的翻譯 過程調(diào)用和返回語句的文法如下所示:Sf call id(Elist) 日ist f Elist, E | E Sf return E過程調(diào)用語句的翻譯模式如下所示: Sf call id (Elist) n :=0;
21、repeatn:=n+1; 從 queue 的隊首取出一個實參地址p;gencode('param', -, -, p);until queue 為空 ;gencode('call', id .addr, n, -) 日ist f Elist, E將E.addr添加到queue的隊尾日ist f E初始化queue,然后將 E.addr加入到queue的隊尾。 過程返回語句的翻譯模式為:Sf return Eif 需要返回結(jié)果 then gencode( :=', E.addr, -, F); gencode( ret, -, -, -)其中, F 是存放結(jié)果的指定單元,四元式(ret, -, -, -)執(zhí)行如下操作: 恢復(fù)主調(diào)程序的寄存器內(nèi)容; 釋放過程運行時所占用的數(shù)據(jù)區(qū); 按返回地址返回
溫馨提示
- 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 遠程在線安全教育的應(yīng)用與即時反饋分析
- 跨境電商平臺的物流管理與優(yōu)化策略
- 自我提升的行動清單計劃
- 高質(zhì)量生活與血液病的先進診斷技術(shù)
- 2025智能物聯(lián)網(wǎng)計算終端
- 財務(wù)風險管理在教育行業(yè)的應(yīng)用
- 增強前臺文員溝通能力的工作計劃
- 跨領(lǐng)域科技創(chuàng)新的發(fā)展策略研究報告
- 科技類企業(yè)如何通過競品廣告提升品牌形象
- 超聲科操作技巧解析專家級教程
- 2024年9月時事政治試題帶答案
- 《浙江省應(yīng)急管理行政處罰裁量基準適用細則》知識培訓(xùn)
- 2024年全國職業(yè)院校技能大賽高職組(康復(fù)治療技術(shù)賽項)考試題庫(含答案)
- 2025年山東健康集團招聘筆試參考題庫含答案解析
- 《中外廣播電視史》課件
- 微信公眾號運營
- DLT 593-2016 高壓開關(guān)設(shè)備和控制設(shè)備
- 三年級體育下冊全冊教案
- 2024年貴州省高考物理試卷(含答案解析)
- 博物館保安職責(4篇)
- 2024解析:第十章 浮力、阿基米德原理及其應(yīng)用-基礎(chǔ)練(解析版)
評論
0/150
提交評論