




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、編譯原理,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,2,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,2,第 1 章 編譯簡介,1.1 編譯器 1.2 源程序的結(jié)構(gòu) 1.3 編譯器的實例 1.4 與編譯相關(guān)的數(shù)據(jù)結(jié)構(gòu) 1.5 編譯器各階段的分組,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,3,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,3,1.1 編譯器,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,4,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,4,The phase of a compiler 編
2、譯程序的結(jié)構(gòu),詞法分析Scanner,語法分析Parser,語義分析Semantic Analyzer,代碼優(yōu)化Code Optimizer,中間代碼生成 Intermediate Code Generator,The phase of a compiler 編譯程序的結(jié)構(gòu),目標(biāo)代碼生成 Target Code Generator,源程序 Source Code,目標(biāo)程序 Target Code,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,5,5,編譯器的分析/綜合模式,編譯器基礎(chǔ)架構(gòu) (Infrastructure,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,
3、6,Q and A,Java程序編譯執(zhí)行過程,Java 語言能夠跨平臺執(zhí)行,在不同平臺上有不同的 Java 虛擬機(jī),可以使 Java 的執(zhí)行代碼即統(tǒng)一的中間代碼與平臺無關(guān),2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,7,7,例1. Pascal源程序語句如下: var x, y, z : real; x := y + z * 60,記號流)(id,1),(:=),(id,2),(+),(id,3),(*) ,(60,依據(jù)源程序的語法規(guī)則把源程序的單詞序列組成語法短語 (表示成語法樹,1.3 編譯器的實例,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,8,2021/
4、2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,8,賦值語句,標(biāo)識符,表達(dá)式,表達(dá)式,表達(dá)式,表達(dá)式,標(biāo)識符,整數(shù),標(biāo)識符,表達(dá)式,x := y + z * 60,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,9,9,中間代碼的形式與作用: (序號) (op, arg1, arg2, result) result := arg1 op arg2,1) (itr, 60, , T1) (2) (*, id3, T1, T2) (3) (+, id2, T2, T3) (4) (:=, T3, , id1,x := y + z * 60,2021/2/23,北京化工大學(xué)信息科學(xué)與技
5、術(shù)學(xué)院計算機(jī)系,10,10,1) (itr, 60, , T1) (2) (*, id3, T1, T2) (3) (+, id2, T2, T3) (4) (:=, T3, , id1,1) (*, id3, 60, T1) (2) (+, id2, T1, id1,MOVF R2, id3 MULF R2, #60 MOVF R1, id2 ADDF R1, R2 MOVF id1, R1,x := y + z * 60,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,11,11,編譯器各階段工作的歸納,詞法分析:識別單詞,至少分以下幾大類:關(guān)鍵字(保留字)、標(biāo)識符、特殊符號;
6、 語法分析:得到語言結(jié)構(gòu)并以樹的形式表示; 語義分析:考察結(jié)構(gòu)正確的句子是否語義合法,可修改樹結(jié)構(gòu); 中間代碼生成(可選):生成一種既接近目標(biāo)語言,又與具體機(jī)器無關(guān)的表示,便于優(yōu)化與代碼生成,中間代碼優(yōu)化(可選):局部優(yōu)化、循環(huán)優(yōu)化、全局優(yōu)化等;優(yōu)化實際上是一個等價變換,變換前后的指令序列完成同樣的功能,但是,在占用的空間上和程序執(zhí)行的時間上都更省、更有效。 目標(biāo)代碼生成:不同形式的目標(biāo)代碼匯編、可重定位、內(nèi)存形式; 符號表管理:合理組織符號,便于各階段查找、填寫等; 出錯處理:錯誤的種類詞法錯、語法錯、靜態(tài)語義錯、動態(tài)語義錯,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,12,
7、第2章 一個微小編譯器,2.1 Micro語言描述 2.2 Micro語言的詞法分析 2.3 Micro語言的語法分析 2.4 Micro語言的語義分析 2.5 Micro語言的目標(biāo)代碼,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,13,2.1 Micro語言描述,Micro語言(稱Micro, Pascal語言的子集,begin var x1:real; var z1:real; x1:=0.5; z1:=x1+56; write(z1+2.3); read(x1) end,Main() float x1; float z1; x1=0.5; z1=x1+56; printf
8、(“%f”,z1+2.3); scanf(“%f”,SL end. VDL VD | VD ; VDL VD var id : T SL S | S ; SL S id:= E | write(E) | read(id) T int | real E id | num | E + E | E * E |(E,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,15,2.1 Micro語言描述,Micro語言(稱Micro,Pascal語言的子集,begin var x1:real; var z1:real; x1:=0.5; z1:=x1+56; write(z1+2.3); read
9、(x1) end,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,16,Micro語言中單詞的種類,2.2 Micro語言的詞法分析,特點: 不依賴于語言的語法定義 ,只依賴于單詞的文法定義,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,17,輸出,輸入,輸出,程序的字符串序列 Token序列 (單詞的內(nèi)部表示,Token定義: 二元組(單詞種類,單詞自身值) 標(biāo)識符: ($id,標(biāo)識符)如($id,x) 整常數(shù): ($intC,整常數(shù))如($intC,5) 實常數(shù): ($realC,實常數(shù))如($realC,0.5) 保留字: ($begin,-), ($end,-
10、), ($var,-) 符號詞: ($plus,-), ($mult,-), ($LParen,-), ($Rparen, -), ($colon,-),($assig,-),($semi,-) 換行符: ($line,-), ($stop,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,18,Micro的詞法分析,圖 源程序在文件中的表示,begin var x1:real; var z1:real; x1:=0.5; z1:=x1+56; write(z1+2.3); read(x1) end,空格,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,19,Micro的
11、詞法分析,begin var x1:real; var z1:real; x1:=0.5; z1:=x1+56; write(z1+2.3); read(x1) end,圖 詞法分析后的TOKEN表示,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,20,Micro的詞法分析,讀當(dāng)前字符流,直至文件結(jié)束。 如果是: (1)標(biāo)識符時判斷是保留字還是變量標(biāo)識 符,構(gòu)造Token表示; (2)數(shù)字時判斷是整型還是實型,構(gòu)造 Token表示; (3)構(gòu)造其它合法的符號的Token表示; (4)遇到非法符號則報錯,2.2 Micro語言的詞法分析,3. 詞法分析過程,2021/2/23,北京
12、化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,21,Micro語言的詞法分析程序,Procedure scanner(); begin while Eof do Noblank(ch); case ch of A.Z|a.z Identifier(name); case name of “begin” GenToken($begin); “end” GenToken($end); “var” GenToken($var); “int” GenToken($int); “real” GenToken($real); “read” GenToken($read); “write” GenToken($wri
13、te); other GenToken($id,name); end,例: begin var x1:real; var z1:real; x1:=0.5; z1:=x1+56; write(z1+2.3); read(x1) end,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,22,Micro語言的詞法分析程序,Procedure scanner(); begin while Eof do Noblank(ch); case ch of A.Z|a.z Identifier(name); case name of “begin” GenToken($begin); “end”
14、 GenToken($end); “var” GenToken($var); “int” GenToken($int); “real” GenToken($real); “read” GenToken($read); “write” GenToken($write); other GenToken($id,name); end,GenToken(token)將token送入TOKEN區(qū),例: begin var x1:real; var z1:real; x1:=0.5; z1:=x1+56; write(z1+2.3); read(x1) end,2021/2/23,北京化工大學(xué)信息科學(xué)與技
15、術(shù)學(xué)院計算機(jī)系,23,Procedure scanner(); begin while Eof do Noblank(ch); case ch of A.Z|a.z Identifier(name); 0.9 Constant(class,C); GenToken(Class,C); ( GenToken($Lparen); Read(ch); ) GenToken($Rparen); Read(ch); + GenToken($plus); Read(ch); * GenToken($mult); Read(ch); ; GenToken($semi); Read(ch); : Read(c
16、h); if ch= then GenToken($assig); Read(ch) else GenToken($colon) . GenToken($stop); Read(ch); GenToken($line); Read(ch); other LexicalError(ch) end end,Constant(class,C):從輸入流讀到當(dāng)前常數(shù),并在class中給出常數(shù)的類型標(biāo)志,C中給出常數(shù)的二進(jìn)制數(shù)值。調(diào)用時當(dāng)前字符定是數(shù)字(在ch中,詞法錯誤,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,24,Procedure scanner(); begin while
17、Eof do Noblank(ch); case ch of A.Z|a.z Identifier(name,Procedure Identifier(name:string); Begin name:=“ ”; Append(name,ch); Read(ch); while isLetter(ch) or isDigit(ch) do Append(name,ch); Read(ch); End,Micro語言的詞法分析程序的相關(guān)子程序(1,Identifier(name): 從輸入流把當(dāng)前標(biāo)識符名讀到name中。在調(diào)用時,當(dāng)前字符一定是字母,且已被讀到ch中,讀標(biāo)識符,保留字(關(guān)鍵字,2
18、021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,25,Procedure Constant(class:classType, C:Const); Begin IntConst(N1,L1); if ch=. then Read(ch); if (IsDigit(ch) LexicalError(ch); IntConst(N2,L2); class:=$realC; C:=N1+N2*(1.0/10)L2 Else class:=$intC; C:=N1 End,Micro語言的詞法分析程序的相關(guān)子程序(2,Constant(class,C):從輸入流讀到當(dāng)前常數(shù),并在class中給
19、出常數(shù)的類型標(biāo)志,C中給出常數(shù)的二進(jìn)制數(shù)值。調(diào)用時當(dāng)前字符定是數(shù)字(在ch中,整數(shù)(小數(shù)點左部,整數(shù)(小數(shù) 點右部,讀整/實常數(shù),2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,26,Micro語言的詞法分析程序的相關(guān)子程序(3,IntConst(N,L):從輸入流讀當(dāng)前整常數(shù),并在N中給出所讀常數(shù)的二(十)進(jìn)制值,在L中給出整常數(shù)的位數(shù)。調(diào)用時當(dāng)前字符應(yīng)是數(shù)字,Procedure IntConst(N,L); Begin N:=Num(ch); L:=1; Read(ch); while isDigit(ch) do N:=N*10+Num(ch); L:=L+1; Read(c
20、h) End,讀整常數(shù),2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,27,Procedure scanner(); begin while Eof do Noblank(ch); case ch of A.Z|a.z Identifier(name); case name of “begin” GenToken($begin); “end” GenToken($end); “var” GenToken($var); “int” GenToken($int); “real” GenToken($real); “read” GenToken($read); “write” GenT
21、oken($write); other GenToken($id,name); end ; 0.9 Constant(class,C); GenToken(Class,C); ( GenToken($Lparen); Read(ch); ) GenToken($Rparen); Read(ch); + GenToken($plus); Read(ch); * GenToken($mult); Read(ch); ; GenToken($semi); Read(ch); : Read(ch); if ch= then GenToken($assig); Read(ch) else GenToke
22、n($colon) . GenToken($stop); Read(ch); GenToken($line); Read(ch); other LexicalError(ch) end end,例: begin var x1:real; var z1:real; x1:=0.5; z1:=x1+56; write(z1+2.3); read(x1) end,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,28,輸入,輸出,程序的字符串序列 Token序列 (單詞的內(nèi)部表示,2.2 Micro語言的詞法分析,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,29,1. 任務(wù)
23、: 檢查程序是否有語法(語法結(jié)構(gòu))上的錯誤。 2. 輸入:詞法分析后得到的Token表 輸出:具體語法錯誤提示和語法全部正確提示,2. Micro語言的語法分析,說明:語法分析只用到單詞的Token表示, 并不改變單詞的Token表示,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,30,聲明檢查:begin var id: real; (聲明的位置) 賦值語句 標(biāo)識符 語句 輸入語句 表達(dá)式 整/實常數(shù) 語句檢查: 輸出語句 左括號 后繼符(語句末符號)(; , end,2. Micro語言的語法分析,3. 語法分析程序思路,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算
24、機(jī)系,31,Procedure Parser(); begin Match($begin,1); Match($var,2); LD: Match($id,3); Match($colon,4); Match($intC/$reaC,5); Match($semi,6); ReadToken(token); if token=$line then ReadToken(token); if token=$var then goto LD,Match(kind,n):讀當(dāng)前Token,并檢查Token.LH=kind? 若不等,則打出錯誤編號n。 Token.LH:Token的左半部。 Token
25、.RH:Token的右半部,ReadToken(token): 把當(dāng)前Token讀到token中,Micro語言的語法分析程序,聲明列處理,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,32,LS: case token of ($write,-)Match($Lparen,7); Expr(); Match($Rparen,8); ($read,-) Match($Lparen,9); Match($id,10); Match($Rparen,11); ($id,-) Match($assig,12); Expr(); other error(13) end; ReadToke
26、n(token); /讀語句的后繼符,單詞,后繼符,Expr():表達(dá)式的語法分析程序,Procedure Parser(); begin Match($begin,1); Match($var,2); LD: Match($id,3); Match($colon,4); Match($intC/$reaC,5); Match($semi,6); ReadToken(token); if token=$line then ReadToken(token); if token=$var then goto LD,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,33,case toke
27、n of ($semi,-) ReadToken(token); if token=$line then ReadToken(token); goto LS; ($line,-) Match($end); ($end,-) ReadToken(token); if token=$stop then STOP else Error(14) other Error(15) end end,ReadToken(token); /讀語句的后繼符,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,34,Micro語言的語法分析子程序表達(dá)式的語法檢查,Procedure Expr(); begi
28、n LF: ReadToken(token); case token of ($id,-) skip; ($intC,-) skip; ($reaC,-) skip; $Lparen begin Expr(); Match($Rparen,16) end; other error(17) end; ReadToken(token,case token of (接左) ($plus,-) goto LF; ($mult,-) goto LF; Other BackToken End end,Token指針回溯一步,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,35,Error 1
29、程序頭不是begin Error 2 變量聲明頭不是var Error 3 var后不是標(biāo)識符 Error 4 “var id”后不是“:” Error 5 “var id:”后不是類型符 Error 6 變量聲明后不是“;” Error 7 write后不是“(” Error 8 “write(E”后不是“)” Error 9 read后不是“(” Error 10 “read (”后不是”id,Micro語言的具體語法錯誤,Error 11 “read (id”后不是”)” Error 12 賦值語句左部 不是“:=” Error 13 語句頭單詞錯 Error 14 程序結(jié)束符錯 Err
30、or 15 語句后繼符錯 Error 16 缺“(E)”中的閉括號 Error 17 運(yùn)算分量的后繼符錯,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,36,Procedure Parser(); begin Match($begin,1); Match($var,2); LD: Match($id,3); Match($colon,4); Match($intC/$reaC,5); Match($semi,6); ReadToken(token); if token=$line then ReadToken(token); if token=$var then goto LD;
31、 LS: case token of ($write,-)Match($Lparen,7); Expr(); Match($Rparen,8); ($read,-) Match($Lparen,9); Match($id,10); Match($Rparen,11); ($id,-) Match($assig,12); Expr(); other error(13) end; ReadToken(token); case token of ($semi,-) ReadToken(token); if token=$line then ReadToken(token); goto LS; ($l
32、ine,-) Match($end); ($end,-) ReadToken(token); if token=$stop then STOP else Error(14) other Error(15) end end,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,37,2.4 Micro語言的語義分析,1.任務(wù): 語法分析:只檢查語法結(jié)構(gòu)的正確性 不檢查他本身(語義)的正確性,語義分析: 檢查語義錯誤:沒有聲明;重復(fù)聲明;類型不相容 將Token序列中的標(biāo)識符轉(zhuǎn)換為($id, entry) (entry表示標(biāo)識符在符號表中的地址,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)
33、學(xué)院計算機(jī)系,38,2.4 Micro語言的語義分析,語義分析過程,讀入Token 遇到標(biāo)識符聲明時,檢查是否已聲明,是則報錯, 否則構(gòu)造標(biāo)識符的符號表; 遇到標(biāo)識符定義和使用時,檢查是否聲明過。 將變量的Token改為($id,entry)形式,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,39,Micro語言的語義分析程序,Procedure Semantic(); begin Creat; /建空符號表 ReadToken(); /讀掉$begin LD: ReadToken(token);/讀聲明/語句頭符 case token.LH of $var ReadToken(
34、token); /($id,name) Enter(token.RH, Entry, s); if s=true then Error(1); / 重復(fù)聲明錯 ReadToken(); ReadToken(token);/: $int/$real case token.LH of $int SetAttribute(entry,newAddr,intType); $real SetAttribute(entry,newAddr,realType) end; ReadToken(); goto LD;,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,40,Micro語言的語義分析程序
35、,Procedure Semantic(); begin Creat; /建空符號表 ReadToken(); /讀掉$begin LD: ReadToken(token);/讀聲明/語句頭符 case token.LH of $var ReadToken(token); /($id,name) Enter(token.RH,Entry,s); if s=true then Error(1); / 重復(fù)聲明錯 ReadToken(); ReadToken(token);/: $int/$real case token.LH of $int SetAttribute(entry,newAddr,
36、intType); $real SetAttribute(entry,newAddr,realType) end; ReadToken(); goto LD;,SetAttribute(entry,Addr,Type): 將標(biāo)識符的地址和類型填入符號表的entry項內(nèi),2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,41,other while token!=$stop do if token.LH=$id then Find(token.RH,entry,s); if s=false then Error(2); /無聲明錯 ChangeToken(entry) end,Find
37、(name,entry,s):用name查找符號表,并在addr和type中給出其name的地址和類型。若已有同名項,則s取true值,否則取false,ChangeToken(entry): 將被讀token(標(biāo)識符)的右半部改為entry地址,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,42,Procedure Semantic(); begin Creat; /建空符號表 ReadToken(); /讀掉$begin LD: ReadToken(token);/讀聲明/語句頭符 case token.LH of $var ReadToken(token); /($id,n
38、ame) Enter(token.RH,Entry,s); if s=true then Error(1); / 重復(fù)聲明錯 ReadToken(); ReadToken(token);/: $int/$real case token.LH of $int SetAttribute(entry,newAddr,intType); $real SetAttribute(entry,newAddr,realType) end; ReadToken(); goto LD; /; $other while token!=$stop do if token.LH=$id then Find(token.
39、RH,entry,s) ; if s=false then Error(2); /無聲明錯 ChangeToken(entry) end,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,43,2.5 Micro語言的目標(biāo)代碼,1.采用三地址形式的目標(biāo)語言,其指令如下: add d1 d2 d3 (d3)=(d1)+(d2) mul d1 d2 d3 (d3)=(d1)*(d2) sto d1 d2 (d2)=(d1) inp d1 輸入d1 out d1 輸出d1,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,44,例:表達(dá)式 a*b+c*d 的目標(biāo)代碼,2021/2
40、/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,45,Procedure GenCodeS(); begin LS: ReadNewToken(tk); case LH(tk) of $id Search(RH(tk), vAddr,vType); ; $write ReadNewToken(); /讀 ( GenCodeE(); ReadNewToken(); /讀 ) $read ReadNewToken(tk);/讀變量 ; /生成輸入代碼 end; ReadNewToken(tk);/讀后續(xù)符 if tk=$semi then goto LS else SendCode(STOP) e
41、nd,Micro語言的目標(biāo)代碼生成程序,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,46,Micro語言的目標(biāo)代碼生成程序,Procedure GenCodeS(); begin LS: ReadNewToken(tk); case LH(tk) of $id Search(RH(tk), vAddr,vType); ReadNewToken(); /:= GenCodeE(); pAddr:=SemanStack(top).addr; pType:=SemanStack(top).Type; if Equ(vType,pType) then Error(1); Sendcod
42、e(STO, pAddr, vAddr); POP,賦值的左邊,表達(dá)式代碼生成程序,類型不相容錯,Sendcode(code): 生成代碼code送入代碼區(qū),sto d1 d2 (d2)=(d1,write ReadNewToken(); /讀 ( GenCodeE(); pAddr:=SemanStack(top).addr; Sendcode(OUT,pAddr); POP; ReadNewToken(); /讀 ) $read ReadNewToken(tk);/讀變量 Search(RH(tk),vAddr,vType); /求變量地址 Sendcode(INP,vAddr); /生成
43、輸入代碼 end; ReadNewToken(tk);/讀后續(xù)符 if tk=$semi then goto LS else SendCode(STOP) end,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,48,無括號表達(dá)式的目標(biāo)代碼生成程序,Procedure GenCodeE(); begin L0: ReadNewToken(tok); L1: case tok of ($id,entry) Search(entry,vAddr,vType) ; /求變量地址 push(vAddr,vType); goto L0 ; ($intC,val) push(val,int);
44、 goto L0 ; ($reaC,val) push(val,real); goto L0 ; $plus|$mult if top1 goto L1 end,判斷優(yōu)先級,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,49,Procedure ProduceCode; begin if Equa(typ1,typ2) then Error() else temp:=NewAddr; SendCode(OP,Addr1,Addr1,temp); Pop(3); push(temp,typ1) end,ProduceCode :用語義棧SemanStack的內(nèi)容生成代碼,2021/2/23,北京化工大學(xué)信息科學(xué)與技術(shù)學(xué)院計算機(jī)系,50,帶括號表達(dá)式的目標(biāo)代碼生成程序,Procedure GenCodeE(); begin pushOperator(); L0: ReadNewToken(tok); L1: case tok of ($id,entry) Search(entry,vAddr,vType); pushOperand(vAddr,vType); goto L0 ; ($intC,val)
溫馨提示
- 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)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權(quán)】 ISO 23698:2024 EN Cosmetics - Measurement of the sunscreen efficacy by diffuse reflectance spectroscopy
- 【正版授權(quán)】 ISO/IEC TR 24722:2024 EN Information technology - Biometrics - Multimodal and other multibiometric fusion
- 【正版授權(quán)】 ISO 16173:2025 EN Ships and marine technology - Jacking system appliances on self-elevating unit - Rack pinion leg fixation system
- 【正版授權(quán)】 ISO 1171:2024 EN Coal and coke - Determination of ash
- 2025年度玻璃隔斷安裝與品牌授權(quán)合同
- 2025年度金融科技企業(yè)員工試工合作協(xié)議
- 2025年度高速公路服務(wù)區(qū)草坪綠化與旅客服務(wù)合同
- 2025年度草種研發(fā)與市場推廣合作協(xié)議
- 2025年度社會組織勞動合同范本解讀與應(yīng)用4篇
- 個人財務(wù)規(guī)劃的重要階段計劃
- 《婦幼保健學(xué)》課件-第一章 緒論
- 10S505 柔性接口給水管道支墩
- 移動寬帶注銷委托書模板需要a4紙
- 初一下冊期末模擬物理質(zhì)量檢測試卷解析1
- 《教育向美而生-》讀書分享課件
- 中海地產(chǎn)總部-員工考核手冊
- 左卡尼汀在減輕高原反應(yīng)中的應(yīng)用
- 《烹飪美學(xué)》課件-項目二 烹飪色彩
- 青海省西寧市選調(diào)生考試(行政職業(yè)能力測驗)綜合能力測試題匯編
- 2024年上海民航職業(yè)技術(shù)學(xué)院高職單招歷年職業(yè)技能測驗高頻考點試題含答案解析
- 夏枯草口服液相關(guān)項目實施方案
評論
0/150
提交評論