




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、編譯原理綜合訓(xùn)練語義分析實驗報告 指引教師: 班級:學(xué)生:zeadom學(xué)號:學(xué)生: 學(xué)號: /6/29目錄 TOC o 1-3 h z u HYPERLINK l _Toc297722858 語言文法的形式化描述 PAGEREF _Toc297722858 h 3 HYPERLINK l _Toc297722859 語義規(guī)則 PAGEREF _Toc297722859 h 3 HYPERLINK l _Toc297722860 運行環(huán)境介紹 PAGEREF _Toc297722860 h 5 HYPERLINK l _Toc297722861 關(guān)鍵算法的流程圖及文字解釋 PAGEREF _To
2、c297722861 h 5 HYPERLINK l _Toc297722862 1、本編譯器的總框架 PAGEREF _Toc297722862 h 5 HYPERLINK l _Toc297722863 2、在語義分析中的主要函數(shù)介紹 PAGEREF _Toc297722863 h 5 HYPERLINK l _Toc297722864 3、產(chǎn)生布爾表達式 PAGEREF _Toc297722864 h 7 HYPERLINK l _Toc297722865 4、While-do語句的語義分析 PAGEREF _Toc297722865 h 8 HYPERLINK l _Toc297722
3、866 5、詞法、語法和語義分析的銜接 PAGEREF _Toc297722866 h 8 HYPERLINK l _Toc297722867 測試報告 PAGEREF _Toc297722867 h 8 HYPERLINK l _Toc297722868 附錄 PAGEREF _Toc297722868 h 12語言文法旳形式化描述(BNF范式) 程序開始P-program i;SDn SC;定義語句SDn-SDSDn|nullSD-var int iSDTSDT - null|,iSDT復(fù)合語句SC-begin Sn endSn-S;Sn|null單個語句S-SD|SA|SIF|SW|SC
4、賦值語句SA-i:=E算術(shù)體現(xiàn)式E-cET|iET|(E)ETET-AE|CE|DE|null;C-+|-|*|/;布爾體現(xiàn)式B-EAEBT|NOT B|(B)BT BT-DB|nullA-|=|=|D-AND|ORif語句SIF-if B then S SELSESELSE-null|else S2while語句SW-while B do S語義規(guī)則(屬性文法)產(chǎn)生式語 義 規(guī) 則i:=E Gen(:=, E.PLACE , ,entry(i) EE1+E2 E.PLACE = Newtemp; Gen(+ , E1.PLACE, E2.PLACE , E.PLACE ) EE1*E2 E.
5、PLACE = Newtemp; Gen(* , E1.PLACE, E2.PLACE , E.PLACE ) E E1 E.PLACE = Newtemp; Gen( , E1.PLACE, , E.PLACE ) E (E1) E.PLACE = E1.PLACE Ei E.PLACE = Entry(i) 產(chǎn)生式語 義 規(guī) 則Ei E.truelist:=makelist(nextquad); E.falselist:=makelist(nextquad+1); Gen( jnz,entry(i), ,0 ); Gen( j , , ,0 ) Ei1 R i2 E.truelist:=m
6、akelist(nextquad); E.falselist:=makelist(nextquad+1); Gen( jR,entry(i1),entry(i2),0 ); Gen( j , , ,0 ) E E1 E.truelist:= E1.falselist ; E.falselist:= E1.truelist ; E ( E1 ) E.truelist:= E1.truelist ; E.falselist:= E1.falselist ; M M.quad := nextquad ; EE1ME2 backpatch(E1.truelist, M.quad ); E.trueli
7、st:=E2.truelist; E.falselist:=merge(E1.falselist,E2. Falselist)EE1ME2 backpatch(E1. falselist, M.quad ); E.truelist:= merge(E1. truelist, E2. truelist); E.falselist:= E2. Falselist EE1ME2backpatch(E1.truelist, M.quad );E.truelist:= E2.truelist;E.falselist:=merge(E1.falselist, E2.Falselist) 產(chǎn)生式語 義 規(guī)
8、則Sif E then M S1 backpatch(E.truelist, M.quad );S.nextlist:=merge(E.falselist, S1.nextlist) M M.quad := nextquad ; N N.nextlist:=makelist(nextquad);Gen( j , , , 0 ) Sif E then M1 S1 N else M2 S2 backpatch(E.truelist, M1.quad );backpatch(E.falselist, M2.quad );S.nextlist:=merge(S1.nextlist, N.nextlis
9、t, S2.nextlist) Swhile M1 E do M2 S1 backpatch(S1.nextlist, M1.quad ); Gen( j , , , M1.quad ); backpatch(E.truelist, M2.quad ); S.nextlist:= E.falselist S begin L end S.nextlist:=L.nextlist S A S.nextlist:= makelist() /*空鏈*/ L S L.nextlist:=S.nextlist LL1; M S backpatch(L1.nextlist, M.quad );L.nextl
10、ist:=S.nextlist 運營環(huán)境簡介運營環(huán)境是DEVC+Dev-C+是一種C&C+開發(fā)工具,它是一款自由軟件,遵守GPL合同。它集合了GCC、MinGW32等眾多自由HYPERLINK t _blank軟件,并且可以獲得最新版本旳多種工具支持,而這一切工作都是來自全球旳狂熱者所做旳工作,并且你擁有對這一切工具自由使用旳權(quán)利,涉及獲得 HYPERLINK t _blank 源代碼等,前提是你也必須遵守GNU合同。Dev-C+每一天都在進步著,由于它是一種自由軟件。 Dev-C+是一種非常實用旳編程軟件,多款出名軟件均由它編寫而成,它在C旳基本上,增強了邏輯性核心算法旳流程圖及文字解釋1、
11、本編譯器旳總框架2、在語義分析中旳重要函數(shù)簡介Backpatch(int list,int quad)代碼:void backpatch(int list,int patch) int tmp; while(list) tmp = list; list = RSStmp.jump; RSStmp.jump = patch; Merge (int list1,int list2)代碼:int merge(int list1, int list2) int tmp = list2; if(list2=0)list2 = list1; else while(RSStmp.jump) tmp = RS
12、Stmp.jump; RSStmp.jump = list1; return list2;3、產(chǎn)生布爾體現(xiàn)式4、While-do語句旳語義分析5、詞法、語法和語義分析旳銜接1、詞法分析是分析輸入代碼產(chǎn)生詞法三元式旳程序。讀入代碼,并將代碼中旳單詞分解成詞法三元式。2、語法分析讀入詞法三元式,并根據(jù)詞法三元式對句子進行語法分析。3、語義分析嵌入在語法分析中。根據(jù)語法分析中得到旳句子類型和語義四元式產(chǎn)生規(guī)則,產(chǎn)生四元式測試報告(測試用例,測試成果)測試用例輸入程序(文獻input.txt):program example;var int j,m,n; begin /*there is a comm
13、ent*/ j:=6; m:=3; /there is a comment n:=j+m; if n=3 and n10 then j:=j*4; while a=,8)(16,3,8)(12,and,8)(15,n,8)(23,10)(16,10,10)(7,then,10)(15,j,10)(26,:=,10)(15,j,10)(28,*,10)(16,4,10)(24,;,10)(10,while,11)(15,a,11)(23,=,n,3,106)105(j,-,-,111)106(j,n,10,113)112(j,-,-,115)113(*,j,4,T3)114(:=,T3,-,j)
14、115(j:22, :23,;:24,:25,:=:26,:27, *:28,/:29,.:30,=:32,:33 def isLetter(): global ch if ch=None: return False else: return ch.isalpha()def isDigit(): global ch if ch=None: return False else: return ch.isdigit()def concat(): global code global ch code = code+chdef getchar(l,i): if len(l)=i or i0: retu
15、rn None else: return lidef getstr():global codeglobal key_wordsglobal lltype = 0state = Nonestr = ;if code=None:str = u(0,+code+,%d)%llelif code in key_words:if(key_wordscode=25):str = error code in line %dn%llstr = str + error code :else:str= u(+key_wordscode+u,+code+,%d)%llelif code.isdigit():str=
16、 (16,+code+,%d)%llelse:str= (15,+code+,%d)%llreturn strll = 0flag = ;if _name_=_main_: src = rinput.txt outfile = routput.txt if not os.path.exists(src): print cannot open file,src else:inputf = open(src,r)outputf = open(outfile,w)annotation = 0ll = 0for line in inputf:ll=ll+1i = 0l = len(line)while
17、 i=len(line):breakcode = ch = getchar(line,i)i = i+1if annotation=1:if ch=* and getchar(line,i)!=None and getchar(line,i)=/:str = annotation end in line %dn%lloutputf.write(str)annotation = 0i=i+1continueif ch=/ and getchar(line,i)!=None and getchar(line,i)=/:outputf.write(line annotation in line %d
18、n%ll)i = i+1breakif ch=None:breakelif ch=/ and getchar(line,i)!=None and getchar(line,i)=*:str = annotation start in line %dn%lloutputf.write(str)i=i+1annotation = 1elif ch= or ch=n or ch=t:continueelif isLetter():while isLetter() or isDigit():concat()ch = getchar(line,i)i=i+1i=i-1str = getstr()str
19、= str+noutputf.write(str)continueelif isDigit():while isDigit():concat()ch = getchar(line,i)i=i+1i=i-1str = getstr()str = str+noutputf.write(str)continueelif ch in key_words:concat()ch = getchar(line,i)i=i+1if ch!=None and (code+ch) in key_words:concat()str = getstr()str = str+noutputf.write(str)els
20、e:i=i-1str = getstr()str = str+noutputf.write(str)else:str = error in line %dn%lloutputf.write(str)outputf.write(error code +ch+n);*資源文獻(resource.h)*#ifndef RESOURCE_H_ZEADOM#define RESOURCE_H_ZEADOM#define $program 1#define $begin 2#define $end 3#define $var 4#define $integer 5#define $if 6#define
21、$then 7#define $else 8#define $do 9#define $while 10#define $int 11#define $and 12#define $or 13#define $not 14#define $flag 15#define $num 16#define $add 17#define $sub 18#define $left 19#define $right 20#define $eq 21#define $gt 22#define $lt 23#define $ 24#define $copy 26#define $comma 27#define
22、$mul 28#define $div 29#define $point 30#define $loe 31#define $goe 32#define $ne 33#endif*語義分析頭文獻(semantic.h)*#ifndef ZEADOM_SEMANTIC_H#define ZEADOM_SEMANTIC_H#include stringusing namespace std;#includeiostream#include fstream#include sstreamint quad,varT;int Nextquad() return quad+;string NextT()
23、varT+; string temp = T; stringstream ss; ssvarT; temp = temp+ ss.str(); return temp;void semantic_init() varT=0; quad=100;struct siyuanshi string op,par1,par2,result; bool hasop,haspar1,haspar2,hasresult; bool isjump; int jump; siyuanshi() op = par1 = par2 = result = ; hasop = haspar1 = haspar2 = ha
24、sresult = false; jump = 0; isjump=false; void setop(string arg) op = arg; hasop = true; void setpar1(string arg) par1 = arg; haspar1 = true; void setpar2(string arg) par2 = arg; haspar2 = true; void setresult(string arg) result = arg; hasresult = true; void setjump(int arg) jump = arg; isjump = true
25、; void output(ofstream &s) s (op,; if(haspar1) spar1,; else s-,; if(haspar2) spar2,; else s-,; if(isjump) sjump)endl; else if(hasresult) sresult)endl; else s-)endl; ;siyuanshi RSS10000;struct Estruct string lexval; Estruct()lexval=;struct Bstruct int truelist; int falselist; Bstruct()truelist=falsel
26、ist=0;struct Mstruct int quad; Mstruct()quad=0;void backpatch(int list,int patch) int tmp; while(list) tmp = list; list = RSStmp.jump; RSStmp.jump = patch; int merge(int list1, int list2) int tmp = list2; if(list2=0)list2 = list1; else while(RSStmp.jump) tmp = RSStmp.jump; RSStmp.jump = list1; retur
27、n list2;void semantic_output() ofstream out(Semantic.out); for(int i=100;iquad;i+) outi; RSSi.output(out); out.close();struct Sstruct int nextlist; Sstruct()nextlist=0;struct Nstruct int nextlist; Nstruct()nextlist=0;#endif*語法和語義分析程序(main.cpp)*#include iostreamusing namespace std;#include string.h#i
28、nclude string#include stdlib.h#include stdio.h#include resource.h#include semantic.h#define deal(s) if(!(s)return false;#define pdeal(s) if(!(s)cout沒有找到文獻結(jié)尾(程序與否未完畢?)line; if(!cin)cin.clear();normal = false;return false; while(line0!=() cinline; att=; int i,j=0; int l = line.length(); i=3; if(line2=
29、0&line2=9) type = (line1-0)*10+(line2-0); i=4; else type = line1-0; for(;linei!=,;i+) att.push_back(linei); for(i=0;i=0&linei=9) pos = pos*10 + (linei-0); i+; return true; word;/定義函數(shù) bool error(string s);bool P();bool Sn(Sstruct &ss);bool S(Sstruct &ss);bool SDn();bool SD();bool SDT();bool SA();bool
30、 SIF(Sstruct &ss);bool SELSE(Sstruct &ss);bool SW(Sstruct &ss);bool SC(Sstruct &ss);bool B(Bstruct &bs);bool BT(Bstruct &bs);bool E(Estruct &es);bool ET(Estruct &es);bool A();bool C();bool D();/函數(shù)實現(xiàn) bool error(string s) printf(語法錯誤:在第%d行n,word.pos); coutsprogram i;SDn SC;bool P() pdeal(word.normal);
31、 bool ret = true; Sstruct ss; if(word.type!= $program )error(缺少程序定義語句);ret = false; else pdeal(word.readNext(); if(word.type!=$flag)error(缺少程序名);ret = false; else pdeal(word.readNext(); if(word.type!=$)error(缺少語句結(jié)束標志); ret = false; else pdeal(word.readNext(); if(!SDn()ret = false; pdeal(word.normal)
32、; if(!SC(ss)ret = false; pdeal(word.normal); if(word.type!=$)error(缺少程序結(jié)束標志); ret = false; cout程序S;Sn|nullbool Sn(Sstruct &ss) deal(word.normal); bool ret = true; while(word.type!=$if&word.type!=$flag&word.type!=$var&word.type!=$while&word.type!=$begin) if(word.type=$end) return ret; error(語法錯誤); re
33、t = false; deal(word.readNext(); if(!S(ss)ret = false; backpatch(ss.nextlist,quad); deal(word.normal); if(word.type!=$)ret = false; error(缺少行終結(jié)符); else deal(word.readNext(); if(!Sn(ss)ret = false; deal(word.normal); return ret;/S-SD|SA|SIF|SW|SCbool S(Sstruct &ss) deal(word.normal); bool ret = true;
34、 switch(word.type) case $if:return SIF(ss); case $var: ss.nextlist=0; return SD(); case $flag: ss.nextlist=0; return SA(); case $while:return SW(ss); case $begin:return SC(ss); default: error(語法錯誤); /SDn-SD;SDn|nullbool SDn() deal(word.normal); if(word.type=$begin) return true; bool ret = true; whil
35、e(true) while(word.type!=$begin&word.type!=$var) error(語法錯誤); deal(word.readNext(); if(word.type=$var) if(!SD() while(true) if(word.type=$|word.type=$begin)break; deal(word.readNext(); else if(word.type!=$) error(缺少行終結(jié)符); ret = false; else deal(word.readNext(); if(word.type=$begin)break; return ret;
36、/SD-var int iSDTbool SD() deal(word.normal); bool ret = true; if(word.type!=$var) error(語法錯誤); return false; else word.readNext(); deal(word.normal); if(word.type!= $int ) error(語法錯誤); return false; else word.readNext(); deal(word.normal); if(word.type!=$flag) error(語法錯誤); return false; else word.re
37、adNext(); cout定義語句 null|,iSDTbool SDT() deal(word.normal); bool ret = true; if(word.type!=$comma)return true; word.readNext(); deal(word.normal); if(word.type!=$flag) error(語法錯誤); return false; else word.readNext(); deal(word.normal); return SDT();/SA-i:=Ebool SA() deal(word.normal); string result;
38、if(word.type!= $flag )error(語法錯誤);return false; else result = word.att; deal(word.readNext(); deal(word.normal); if(word.type!= $copy )error(語法錯誤);return false; cout賦值語句if B then S SELSEbool SIF(Sstruct &ss) deal(word.normal); bool ret = true; if(word.type!= $if ) ret =false; error(缺少if); else word.
39、readNext(); deal(word.normal); Bstruct bs; if(!B(bs)return false; if( word.type != $then ) ret = false; error(缺少then); else word.readNext(); deal(word.normal); coutnull|else S2bool SELSE(Sstruct &ss) deal(word.normal); bool ret = true; if(word.type!=$else)return true; word.readNext(); deal(word.norm
40、al); coutwhile B do Sbool SW(Sstruct &ss) cout循環(huán)語句begin Sn endbool SC(Sstruct &ss) deal(word.normal); bool ret = true; if(word.type!=$begin)error(復(fù)合語句缺少begin);ret = false; else word.readNext(); deal(word.normal); if(!Sn(ss)ret = false; deal(word.normal); if(word.type!=$end)error(復(fù)合語句缺少end);ret = fal
41、se; else word.readNext(); cout復(fù)合語句EAEBT|NOT B|(B)BT bool B(Bstruct &bs) deal(word.normal); bool ret = true; if(word.type= $left ) word.readNext(); deal(word.normal); if(!B(bs) return false; if(word.type!= $right ) error(缺少括號); ret = false; else word.readNext(); deal(word.normal); if(!BT(bs) ret = fa
42、lse; deal(word.normal); else if(word.type= $not) word.readNext(); deal(word.normal); Bstruct bsa; if(!B(bsa)return false; bs.falselist = bsa.truelist; bs.truelist = bsa.falselist; else if(word.type=$flag|word.type=$num) Estruct es1; if(!E(es1)return false; if(!A()return false; string op = word.att;
43、op = j+op; word.readNext(); Estruct es2; if(!E(es2)return false; int quad = Nextquad(); bs.truelist = quad; RSSquad.setjump(0); RSSquad.setpar1(es1.lexval); RSSquad.setpar2(es2.lexval); RSSquad.setop(op); quad = Nextquad(); bs.falselist=quad; RSSquad.setop(j); RSSquad.setjump(0); if(!BT(bs)return false; else error(語法錯誤); return false; return ret;/BT-DB|null bool BT(Bstruct &bs) deal(word.normal); if(!D() return true; int type = word.type; word.readNext(); deal(word.normal); Mstruct mtmp; mtmp.quad = quad; Bstruct bsa; if(!B(bsa)cout語法錯誤 word.ty
溫馨提示
- 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)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2012建筑合同范例
- 買更名房合同范本
- 公寓維修合同范本
- 勘探鉆機租賃合同范本
- 加壓設(shè)備安裝合同范本
- 買房付款補充合同范本
- 4人合作合同范本
- 產(chǎn)品運營合作合同范本
- 辦公窗簾加盟合同范例
- 主題七 任務(wù)一 認識因特網(wǎng) 教學(xué)設(shè)計 -2023-2024學(xué)年桂科版初中信息技術(shù)七年級上冊
- CJT 362-2011 城鎮(zhèn)污水處理廠污泥處置 林地用泥質(zhì)
- 《食品安全風(fēng)險管控清單》
- 數(shù)控機床技術(shù)先進性
- 電梯井腳手架搭設(shè)施工施工方法及工藝要求
- 【正版授權(quán)】 IEC 62317-9:2006+AMD1:2007 CSV EN Ferrite cores - Dimensions - Part 9: Planar cores
- 《阿Q正傳》(課件)2023-2024高二語文選擇性必修下冊
- 2024年黑龍江交通職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試題庫及1套參考答案
- 愛國主義教育基地組織管理制度
- 2024屆遼寧省沈陽市名校中考化學(xué)模擬試題含解析
- 2023版《思想道德與法治》(緒論-第一章)緒論 擔(dān)當(dāng)復(fù)興大任 成就時代新人;第一章 領(lǐng)悟人生真諦 把握人生方向 第3講 創(chuàng)造有意義的人生
- 第6課 歐洲的思想解放運動(教學(xué)課件)-【中職專用】《世界歷史》同步課堂(同課異構(gòu))(高教版2023?基礎(chǔ)模塊)
評論
0/150
提交評論