《合肥工業(yè)大學編譯原理實驗》由會員分享,可在線閱讀,更多相關《合肥工業(yè)大學編譯原理實驗(16頁珍藏版)》請在裝配圖網上搜索。
1、宣城校區(qū)實驗報告編譯原理計算機0001班趙保飛68課程名稱 專業(yè)班級 學生姓名及學號 指導教師 實驗地點李芒宏計算機中心樓第四機房20172018學年第一學期68編譯原理課程實驗報告詞法分析設計姓名趙保飛系院專業(yè)計算機科學與技術班級計算機01班學號實驗日期指導教師李芒宏成績一、實驗目的和要求通過本實驗的編程實踐,使學生了解詞法分析的任務,掌握詞法分析程序設 計的原理和構造方法,便學生對編譯的基本概念、原理和方法有完整的和清楚的 理解,并能正確地、熟練地運用。二.實驗原理(1)實驗數據結構說明KString數組-關鍵字表;schar數組一分界符;mchar數組一算術運算符;rString數 組一
2、關系運算符;ArrayList型String數組ci常數;ArrayList型String數組id標識符(2)實驗算法描述退回(一)退回亠)返回(relop, DE返回(relop, DT返回(relop,BQ返回(-返回(:Q非法字符備(3)算法流程圖實驗名稱返回(num, num在常數表中的位直)50開帕非字每數字邀回 (記, 記在符號表中的位盤)或退回(:保宙字詞法分析設計流程圖三、源程序代碼和測試結果package lexicalAna lysisimportimportpublic class lexicalAnalysisstatic StringA*LJfor main 11wh
3、 11 evoid publICstatic prinlf scan! asm do return typedcf auto” double”nbreakrshortrusing” default Tong cyth() 1) quals(b)(display(4 ri * *) cngthO 2) (quals(a) lexicalAnalysis Java Application! C:Proc單詞二元序列 類型位潼(行, 列)要處理的語句為:if 1=0 then n+if(lzif)關鍵字 (1J)ii)標識符(1,2)=(4 產) 關系運算符(1,3)0(5,0)常數(1,4)the
4、n(6,then)標識符(1,5)n(6 川) 標識符(1,6)+(3r+)算木運算符(1,7)+(3Z+ )算木運算符(1,8)t(2Z;)分界符(1,9)要處理的語句為.a = 3b%);a(6za)標識符(2,1) 不為 z 時將 s 加到Mvn(u)vt(Gri. charAt(j)中MvnNum(u)vtNum(Gri charAI(j) selse (harAt (j)為t9將屬于u的fol low集的元素b的位置用s加到Mvn(u) vt (b)中 if(i rl.l. HvriNi ni(u)k)Mvnum(u) k selse harAt (j)為辛終釘卻for( TG2#G
5、SFT-FS3#GSiF-i4#GS十 i 啼5#G&社6#GT$C-TG二4II衛(wèi)ifI)(actionl movel = 6;action2 changel二2; action2.move2 = 7;action2 change4 = 2; action2change5 = 2; action3 changel = 4; action3 change2 = 4; action3 change4 = 4; action3change5 = 4; action4.movel = 5; action4. move3 = 4; action5 change1 = 6; action5change2
6、 = 6; action5 change4 = 6; action5 change5 = 6; action6.move0 = 5; action6move3 = 4; action7 move0 = 5; action7.move3 = 4; action8 change1 = 6; action8change4 = 11: action9 change1 = 1; act ion9. move2 = 7: action9 change4 = 1; action9change5 = 1;action10 change1 = 3; action10 change2 = 3; action10
7、change4 = 3; action10 change5= 3; actionll change1 = 5; actionll change2 = 5; action11 change4 = 5; actionllchange5 = 5;/*存儲GOTO表*/go0.head0 =rEr;eadLl = T; go0.head2 = F; go0. gt0= 1;tl = 2;go 0. gt23;go4.head0= E;go4 head1=T;go4 head2=F;go4. gt0=8;go4.gtl=2;go4.gt2=3;go6. headl=T;go6. head2=rFr;go
8、6. gtl=9;left =strcpy(s have1 sright, have2sright, have3 sright, have4sright, have5 sright, have6sright, move3 = 4;strcpy(sstrcpy(sstrcpy(sstrcpy(sstrcpy (s” E+T”eft =fEr;T):left = T; W) ;left二T;F);left = *F; (E)Jeft = F*;in);ove0 = 5;action0go6. gt2=3;go7.head2=F1;go7.gt2=10;oveEcol != 0)cout ACTIO
9、N】sta symb M = S actionEsta, move col ,PUSH” actionsta. movecol endl;status+sta_Index = actionsta movecol;hangecol != 0)cout actionsta change col:s_haveactionsta changecol sleft s haveactionEsta. changecol . sright 規(guī)約,GOTO(” statussta_Index一strlen(s_haveactionsta changecol sright) s haveactionsta ch
10、angetcol. sleft ) = ;int i = 0;for (i =0: i strlen(s haveactionsta changecol sright):i+)hangecol sleft:hangecol sright): i+)eadi = symb) status+sta Index = gosta gti;cout gosta. gt i PUSH endl: return; *查表*/void start find(void)int s = statussta Index:char exp first char = inputstringinstr top: char
11、 sym = symbolsym Index:while (IsAccept != 1)s = statussta_Index:exp first char = inputstringir)str top: sym = symbolsym Index:嚴輸出步驟*/cout step+ITttM;/*輸出當前狀態(tài)棧*/int i = 0;for (i = 0; i = sta_Index; i+)coutstatusi:cout /*輸出符號棧*/cout symbol /*輸出剩余的輸入串*/i= 0;for (i = 0; i instr_top; i+) coutn;for (i = i
12、nstr top; i = instr_index; i+) coutinputstringi:coutttH;switch (exp first char)r icase 1 :find table(s, exp_first_char, 0); break;case +:int main()/cout 輸入要判斷的分析串;string temp;cin temp;for (int i = 0; i i規(guī)約,GOTO F) =3PU+i*i#r4:T-F規(guī)約,GOTO(0, T) =2PU+i*ir2:E-T規(guī)約,GOTO(0?E)=1PU+i*iCACTIONLL+J=S6,PUSH6訐i#
13、ACTION6, i二S3,PUSH5r6:F-i規(guī)約,GOTO(6,F)=3PU*i#r4:T-F規(guī)約,GOTO (6, T) =9PUACTION9,=S7,PUSH7i#ACTION7,=S5,PUSH5r6:F-i規(guī)約,GOTO (7, F)=10Pr3:T-T*F規(guī)約,GOTO (6, T)二9rl:E-E+T規(guī)約,GOTO(0, E)=l:Acc:分析成功find table(s,break;case1*:find table(s,break;caser(*:find table(s,break;caser)1: find table(s,break;casefind table(
14、s,break;exp first_char,exp first char,exp first一char,exp first char,exp first char,1);2);3) ;4) ;5) ;四、實驗評價、收獲與體會這一周真是驚險萬分,有周二四個小時 OS 實驗,周三網絡考試,周三就要驗收實驗,還有一堆 事好像老師都喜歡集體行動把寧情堆到一起解決。好在挺過來。這個實驗總的來說太難,嘗試了 java寫圖形界面和自動化實現(xiàn)分析表等,但是時間緊迫只好以后有時間慢慢搞?,F(xiàn)在的實驗只 是一份低配版的實驗,LR 分析表都不是自動生成的,是存儲在數組中直接使用的,所以老師很失 望給了及格分,我也很無奈。沒有大神的超速只能怪自己學藝不精,效率太低。希望以后可以快 點。