




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
4.4申明語句翻譯1.變量定義與申明2.過程定義與申明1第1頁第1頁24.4申明語句翻譯作用域信息保留過程作用域與程序塊類似,在允許嵌套定義過程程序設計語言中,相同名字能夠同時出現(xiàn)在不同作用域中,因此有必要討論怎樣設計符號表來存放它們。此處討論過程作用域,一樣恪守靜態(tài)作用域和最近嵌套標準。第2頁第2頁34.4申明語句翻譯定義4.2(嵌套深度)設主程序(最外層過程)嵌套深度dmain=1,則若過程A直接嵌套定義過程B,則dB=dA+1;變量申明時所在過程嵌套深度,被認為是該變量嵌套深度。與程序塊相比,有兩點不同,使得過程申明處理復雜:過程頭是一個名字,可象引用變量一樣被調(diào)用程序塊執(zhí)行與靜態(tài)一致,而過程不一致。1voidmain()2{inta=0,b=0;//B03{intb=1; //B14{inta=2,c=4,d=5; }//B27{intb=3;}//B311}12}voidswap(int&x,int&y){inttemp;temp=x;x=y;y=temp;}第3頁第3頁4例4.14快排序Pascal程序:programsort(input,output);
vara:array[0..10]ofinteger; x:integer; procedurereadarray;
vari:integer; beginfori:=1to9doread(a[i])end{readarray}; procedureexchange(i,j:integer); beginx:=a[i];a[i]:=a[j];a[j]:=x;end{exchange}; procedurequicksort(m,n:integer);
vari,v:integer; functionpartition(y,z:integer):integer; vari,j:integer; begin...a...;...v...;...exchange(i,j);...end{partition}; beginif(n>m)then begini:=partition(m,n);quicksort(m,i-1);quicksort(i+1,n)end; end{quicksort};begina[0]:=-9999;a[10]:=9999;readarray;quicksort(1,9)end{sort}.過程變量深度sorta,x1readarrayi2exchange2quicksorti,v2partitioni,j3第4頁第4頁54.4申明語句翻譯嵌套關系樹形表示過程定義為節(jié)點節(jié)點a是節(jié)點b父親,當且僅當過程b直接嵌套在過程a中過程變量深度sorta,x1readarrayi2exchange2quicksorti,v2partitioni,j3第5頁第5頁64.4申明語句翻譯符號表中作用域信息過程定義文法(忽略了參數(shù)) P→D (1) D→D;D (2) |id:T (3) |procid;D;S (4)過程是一個申明語句能夠申明若干個過程(變量)變量申明,T是type過程定義,S是可執(zhí)行語句T是類型(整型,實型等)細節(jié)忽略S是一個可執(zhí)行語句(賦值、控制語句等)細節(jié)忽略第6頁第6頁74.4申明語句翻譯嵌套過程中名字作用域信息,使用有嵌套結構符號表保留。每個過程被認為是一個子符號表,或者是符號表中一個節(jié)點。嵌套節(jié)點之間用雙向鏈連接,正向鏈批示過程嵌套關系,逆向鏈實現(xiàn)按作用域對名字進行訪問??焖倥判蜓刂嫦蜴溤L問不到名字,作用域不相交第7頁第7頁84.4申明語句翻譯在過程申明時要做工作之一是在(語法)分析過程中逐步生成符號表,并將正確內(nèi)容填寫進符號表相應欄目舊文法:
P→D (1) D→D;D (2) |id:T (3) |procid;D;S (4)修改文法,在定義D之前生成符號表(LR分析) P→MD (1) D→D;D (2) |id:T (3) |procid;ND;S (4) M→ε (5) N→ε (6)第8頁第8頁94.4申明語句翻譯全程量、屬性與語義函數(shù)全程量:有序對棧(tblptr,offset) (符號表節(jié)點指針,符號表節(jié)點所需寬度)棧上操作:push(t,o)、pop、top(stack)語義函數(shù)與過程:函數(shù)mktable(previous):建立一個新符號表,返回指向符號表指針。previous是逆向鏈,指向前一符號表(外層)。過程enter(table,name,type,offset):在table指向節(jié)點中為名字name建立新條目,包括名字類型和存儲位置等。過程addwidth(table,width):計算table節(jié)點中所有條目的累加寬度,并統(tǒng)計在table頭部信息中。過程enterproc(table,name,newtable):為過程name在table指向節(jié)點中建立一個新條目。參數(shù)newtable是正向鏈,指向name過程本身符號表節(jié)點。第9頁第9頁104.4申明語句翻譯語義規(guī)則(1)P→MD {addwidth(top(tblptr),top(offset));pop;}(2)M→ε {t:=mktable(null);push(t,0,);}(3)D→D;D(4)D→id:T {enter(top(tblptr),,T.type,top(offset)); top(offset):=top(offset)+T.width;}(5)D→procid;ND1;S {t:=top(tblptr); addwidth(t,top(offset)); pop;
enterproc(top(tblptr),,t);}
(6)N→ε {t:=mktable(top(tblptr));push(t,0);}第10頁第10頁114.4申明語句翻譯語法制導翻譯過程procsort;a:array[10]ofint;x:int;procreadarry;i:int;read(a);readarrayP→MD (1) D→D;D (2) |id:T (3) |procid;ND;S (4) M→ε (5) N→ε (6)t1t2t3第11頁第11頁12(1)M1→ε t1:=mktable(null);push(t1,0);nullt10t1第12頁第12頁13M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);nullt10t20t1t1t2第13頁第13頁14M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);T1→int T1.type=integer,T1.width=4nullt10t20t1t1t2第14頁第14頁15M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);T1→int T1.type=integer,T1.width=4T2→array[10]ofT1 T2.type=array(10,int),T2.width=40nullt10t20t1t1t2第15頁第15頁16M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);T1→int T1.type=integer,T1.width=4T2→array[10]ofT1 T2.type=array(10,int),T2.width=40D1→a:T2 (a,arr,0)填進t2,top(offset):=40nullt10t20aarr,0t1t1t2t240第16頁第16頁17M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);T1→int T1.type=integer,T1.width=4T2→array[10]ofT1 T2.type=array(10,int),T2.width=40D1→a:T2 (a,arr,0)填進t2所指節(jié)點,top(offset):=40T3→int T2.type=integer,T2.width=4nullt10t240aarr,0t1t1t2第17頁第17頁18M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);T1→int T1.type=integer,T1.width=4T2→array[10]ofT1 T2.type=array(10,int),T2.width=40D1→a:T2 (a,arr,0)填進t2所指節(jié)點,top(offset):=40T3→int T2.type=integer,T2.width=4D2→x:T3 (x,int,40)填進t2所指節(jié)點,top(offset):=44nullt10t240aarr,0xInt,40t1t1t2t244第18頁第18頁19M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);T1→int T1.type=integer,T1.width=4T2→array[10]ofT1 T2.type=array(10,int),T2.width=40D1→a:T2 (a,arr,0)填進t2所指節(jié)點,top(offset):=40T3→int T2.type=integer,T2.width=4D2→x:T3 (x,int,40)填進t2所指節(jié)點,top(offset):=44N2→ε t3:=mktable(top(tblptr));push(t3,0);nullt10t244t1t2t3t30aarr,0xInt,40t1t2第19頁第19頁M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);T1→int T1.type=integer,T1.width=4T2→array[10]ofT1 T2.type=array(10,int),T2.width=40D1→a:T2 (a,arr,0)填進t2所指節(jié)點,top(offset):=40T3→int T2.type=integer,T2.width=4D2→x:T3 (x,int,40)填進t2所指節(jié)點,top(offset):=44N2→ε t3:=mktable(top(tblptr));push(t3,0);T4→int T4.type=integer,T4.width=420nullt10t244t1t2t3t30aarr,0xInt,40t1t2第20頁第20頁21M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);T1→int T1.type=integer,T1.width=4T2→array[10]ofT1 T2.type=array(10,int),T2.width=40D1→a:T2 (a,arr,0)填進t2所指節(jié)點,top(offset):=40T3→int T2.type=integer,T2.width=4D2→x:T3 (x,int,40)填進t2所指節(jié)點,top(offset):=44N2→ε t3:=mktable(top(tblptr));push(t3,0);T4→int T4.type=integer,T4.width=4D3→i:T4 (i,int,0)填進t3所指節(jié)點,top(offset):=4nullt10t244aarr,0xInt,40t1t1t2iint,0t2t3t34t30第21頁第21頁22M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);T1→int T1.type=integer,T1.width=4T2→array[10]ofT1 T2.type=array(10,int),T2.width=40D1→a:T2 (a,arr,0)填進t2所指節(jié)點,top(offset):=40T3→int T2.type=integer,T2.width=4D2→x:T3 (x,int,40)填進t2所指節(jié)點,top(offset):=44N2→ε t3:=mktable(top(tblptr));push(t3,0);T4→int T4.type=integer,T4.width=4D3→i:T4 (i,int,0)填進t3所指節(jié)點,top(offset):=4(11)D4→procreadarrayN2D3;S
t:=top(tblptr);addwidth(t,top(offset)); pop;enterproc(top(tblptr),readarray,t);nullt10t244aarr,0xInt,40t1t1t24iint,0t2t3t34第22頁第22頁23M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);T1→int T1.type=integer,T1.width=4T2→array[10]ofT1 T2.type=array(10,int),T2.width=40D1→a:T2 (a,arr,0)填進t2所指節(jié)點,top(offset):=40T3→int T2.type=integer,T2.width=4D2→x:T3 (x,int,40)填進t2所指節(jié)點,top(offset):=44N2→ε t3:=mktable(top(tblptr));push(t3,0);T4→int T4.type=integer,T4.width=4D3→i:T4 (i,int,0)填進t3所指節(jié)點,top(offset):=4(11)D4→procreadarrayN2D3;S t:=top(tblptr);addwidth(t,top(offset));
pop;enterproc(top(tblptr),readarray,t);nullt10t244aarr,0xInt,40t1t1t24iint,0t2t3第23頁第23頁24M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);T1→int T1.type=integer,T1.width=4T2→array[10]ofT1 T2.type=array(10,int),T2.width=40D1→a:T2 (a,arr,0)填進t2所指節(jié)點,top(offset):=40T3→int T2.type=integer,T2.width=4D2→x:T3 (x,int,40)填進t2所指節(jié)點,top(offset):=44N2→ε t3:=mktable(top(tblptr));push(t3,0);T4→int T4.type=integer,T4.width=4D3→i:T4 (i,int,0)填進t3所指節(jié)點,top(offset):=4(11)D4→procreadarrayN2D3;S t:=top(tblptr);addwidth(t,top(offset)); pop;enterproc(top(tblptr),readarray,t);nullt10t244aarr,0xInt,40readarrayt3t1t1t24iint,0t2t3第24頁第24頁25M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);T1→int T1.type=integer,T1.width=4T2→array[10]ofT1 T2.type=array(10,int),T2.width=40D1→a:T2 (a,arr,0)填進t2所指節(jié)點,top(offset):=40T3→int T2.type=integer,T2.width=4D2→x:T3 (x,int,40)填進t2所指節(jié)點,top(offset):=44N2→ε t3:=mktable(top(tblptr));push(t3,0);T4→int T4.type=integer,T4.width=4D3→i:T4 (i,int,0)填進t3所指節(jié)點,top(offset):=4(11)D4→procreadarrayN2D3;S t:=top(tblptr);addwidth(t,top(offset)); pop;enterproc(top(tblptr),readarray,t);(14)D7→procsortN1D6;S
t:=top(tblptr);addwidth(t,top(offset)); pop;enterproc(top(tblptr),sort,t);nullt10t24444aarr,0xInt,40readarrayt3t1t1t24iint,0t2t3第25頁第25頁26M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);T1→int T1.type=integer,T1.width=4T2→array[10]ofT1 T2.type=array(10,int),T2.width=40D1→a:T2 (a,arr,0)填進t2所指節(jié)點,top(offset):=40T3→int T2.type=integer,T2.width=4D2→x:T3 (x,int,40)填進t2所指節(jié)點,top(offset):=44N2→ε t3:=mktable(top(tblptr));push(t3,0);T4→int T4.type=integer,T4.width=4D3→i:T4 (i,int,0)填進t3所指節(jié)點,top(offset):=4(11)D4→procreadarrayN2D3;S t:=top(tblptr);addwidth(t,top(offset)); pop;enterproc(top(tblptr),readarray,t);(14)D7→procsortN1D6;S
t:=top(tblptr);addwidth(t,top(offset));
pop;enterproc(top(tblptr),sort,t);nullt1044aarr,0xInt,40readarrayt3t1t1t24iint,0t2t3第26頁第26頁27M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);T1→int T1.type=integer,T1.width=4T2→array[10]ofT1 T2.type=array(10,int),T2.width=40D1→a:T2 (a,arr,0)填進t2所指節(jié)點,top(offset):=40T3→int T2.type=integer,T2.width=4D2→x:T3 (x,int,40)填進t2所指節(jié)點,top(offset):=44N2→ε t3:=mktable(top(tblptr));push(t3,0);T4→int T4.type=integer,T4.width=4D3→i:T4 (i,int,0)填進t3所指節(jié)點,top(offset):=4(11)D4→procreadarrayN2D3;S t:=top(tblptr);addwidth(t,top(offset)); pop;enterproc(top(tblptr),readarray,t);(14)D7→procsortN1D6;S
t:=top(tblptr);addwidth(t,top(offset));
pop;enterproc(top(tblptr),sort,t);sortt2nullt10t14iint,0t2t344aarr,0xInt,40readarrayt3t1t2第27頁第27頁28M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);T1→int T1.type=integer,T1.width=4T2→array[10]ofT1 T2.type=array(10,int),T2.width=40D1→a:T2 (a,arr,0)填進t2所指節(jié)點,top(offset):=40T3→int T2.type=integer,T2.width=4D2→x:T3 (x,int,40)填進t2所指節(jié)點,top(offset):=44N2→ε t3:=mktable(top(tblptr));push(t3,0);T4→int T4.type=integer,T4.width=4D3→i:T4 (i,int,0)填進t3所指節(jié)點,top(offset):=4(11)D4→procreadarrayN2D3;S t:=top(tblptr);addwidth(t,top(offset)); pop;enterproc(top(tblptr),readarray,t);(14)D7→procsortN1D6;S
t:=top(tblptr);addwidth(t,top(offset));
pop;enterproc(top(tblptr),sort,t);(15)P→M1D7 addwidth(top(tblptr),top(offset));pop;0sortt2nullt10t14iint,0t2t344aarr,0xInt,40readarrayt3t1t2第28頁第28頁29M1→ε t1:=mktable(null);push(t1,0);N1→ε t2:=mktable(top(tblptr));push(t2,0);T1→int T1.type=integer,T1.width=4T2→array[10]ofT1 T2.type=array(10,int),T2.width=40D1→a:T2 (a,arr,0)填進t2所指節(jié)點,top(offset):=40T3→int T2.type=integer,T2.width=4D2→x:T3 (x,int,40)填進t2所指節(jié)點,top(offset):=44N2→ε t3:=mktable(top(tblptr));push(t3,0);T4→int T4.type=integer,T4.width=4D3→i:T4 (i,int,0)填進t3所指節(jié)點,top(offset):=4(11)D4→procreadarrayN2D3;S t:=top(tblptr);addwidth(t,top(offset)); pop;enterproc(top(tblptr),readarray,t);(14)D7→procsortN1D6;S
t:=top(tblptr);addwidth(t,top(offset));
pop;enterproc(top(tblptr),sort,t);(15)P→M1D7 addwidth(top(tblptr),top(offset));pop;0sortt2nullt14iint,0t2t344aarr,0xInt,40readarrayt3t1t2第29頁第29頁304.5簡樸算術表示式與賦值句討論所基于文法:A→id:=E E→E+E|E*E|-E|(E)|id簡樸變量語法制導翻譯屬性.place:存儲E變量名地址(符號表中地址或暫時變量)過程emit():生成result:=arg1oparg2三地址碼。(1)A→id:=E {emit(entry()':='E.place)}(2)E→E1+E2 {E.place:=newtemp;
emit(E.place':='E1.place'+'E2.place)}
(3)E→E1*E2 {E.place:=newtemp;
emit(E.place':='E1.place'*'E2.place)}(4)E→-E1 {E.place:=newtemp;
emit(E.place':=''-'E1.place)}
(5)E→(E1) {E.place:=E1.plac
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 辦公大樓保潔承包合同
- 技術開發(fā)合同模板簡明
- 院企合作科研合同標準模板
- 工業(yè)品交易合同模板轉讓合作協(xié)議
- 銀行軟件服務合同
- 小學生冬季滑冰知識
- 藥理學第二十章 抗心絞痛藥課件
- 微特電機在無人機飛行控制系統(tǒng)的應用考核試卷
- 搪瓷材料在實驗室環(huán)境的應用考核試卷
- 地下綜合管廊工程光纜敷設技術考核試卷
- 新入職消防安全培訓
- 醫(yī)保信息系統(tǒng)數(shù)據(jù)安全管理制度
- 第18課排序計算有方法(教案)四年級全一冊信息技術人教版
- 統(tǒng)編版五年級語文下冊1古詩三首《四時田園雜興(其三十一)》課件
- 酒店2024年保安部工作計劃024酒店工作計劃
- 規(guī)?;i場生物安全
- 2025年春節(jié)后復產(chǎn)復工方案及安全技術措施
- 維修基金使用合同范例
- c語言課件教學下載
- 2024購房合同購房定金合同
- 2024年全國中學生生物學聯(lián)賽試題含答案
評論
0/150
提交評論