![編譯原理-實(shí)驗(yàn)報告實(shí)驗(yàn)三--語法分析(LR分析程序_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-5/4/98be6653-5982-4afa-bf0e-ef9bd139c48b/98be6653-5982-4afa-bf0e-ef9bd139c48b1.gif)
![編譯原理-實(shí)驗(yàn)報告實(shí)驗(yàn)三--語法分析(LR分析程序_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-5/4/98be6653-5982-4afa-bf0e-ef9bd139c48b/98be6653-5982-4afa-bf0e-ef9bd139c48b2.gif)
![編譯原理-實(shí)驗(yàn)報告實(shí)驗(yàn)三--語法分析(LR分析程序_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-5/4/98be6653-5982-4afa-bf0e-ef9bd139c48b/98be6653-5982-4afa-bf0e-ef9bd139c48b3.gif)
![編譯原理-實(shí)驗(yàn)報告實(shí)驗(yàn)三--語法分析(LR分析程序_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-5/4/98be6653-5982-4afa-bf0e-ef9bd139c48b/98be6653-5982-4afa-bf0e-ef9bd139c48b4.gif)
![編譯原理-實(shí)驗(yàn)報告實(shí)驗(yàn)三--語法分析(LR分析程序_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-5/4/98be6653-5982-4afa-bf0e-ef9bd139c48b/98be6653-5982-4afa-bf0e-ef9bd139c48b5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上華北水利水電學(xué)院 編譯原理 實(shí)驗(yàn)報告20122013學(xué)年 第 一 學(xué)期 2011 級 計(jì)算機(jī)科學(xué)與技術(shù) 專業(yè)班級: 學(xué)號: 姓名: 一、 實(shí)驗(yàn)題目:語法分析(LR分析程序)(1)選擇最有代表性的語法分析方法LR分析法;(2)選擇對各種常見程序語言都用的語法結(jié)構(gòu),如賦值語句(尤指表達(dá)式)作為分析對象,并且與所選語法分析方法要比較貼切。二、 實(shí)驗(yàn)內(nèi)容(1)根據(jù)給定文法,先求出FirstVt和LastVt集合,構(gòu)造算符優(yōu)先關(guān)系表(要求算符優(yōu)先關(guān)系表輸出到屏幕或者輸出到文件);(2)根據(jù)算法和優(yōu)先關(guān)系表分析給定表達(dá)式是否是該文法識別的正確的算術(shù)表達(dá)式(要求輸出歸約過程)(3)
2、給定表達(dá)式文法為:G(E): E#E#EE+T | TTT*F |FF(E)|i(4) 分析的句子為: (i+i)*i和i+i)*i三、 根據(jù)以上文法構(gòu)造出的LR(1)分析表為:+*()I#ETF0S4S51231S6Acc2R2S7R2R23R4R4R4R44S4S58235R6R6R6R66S4S5937S4S5108S6S119R1S7R1R110R3R3R3R311R5R5R5R5四、程序源代using System;using System.Text;using System.IO;namespace Syntax_Analyzerclass SyntaxStreamReader m
3、yStreamReader;int t;int lengh;int l =0;string grammar;int s=0;string Word;int w=0;int wordNum ;int n =0;int, LR;public Syntax() lengh = new int7; grammar=new string7; Word=new string100; wordNum = new int100; LR=new int30,30;public void analyzer()/讀入grammarSyntax myTextRead=new Syntax();Console.Writ
4、eLine("-語法分析開始-n");/*/循環(huán)讀取文法/*string strStart;strStart="grammar.txt"myTextRead.myStreamReader=new StreamReader(strStart);string strBufferStart;int uu=0;dostrBufferStart =myTextRead.myStreamReader.ReadLine();if(strBufferStart=null)break;foreach (String subString in strBufferStart.
5、Split()grammaruu=subString; /每行文法存入grammaruu+;while (strBufferStart!=null);myTextRead.myStreamReader.Close();/*/循環(huán)讀取lengh/*strStart="lengh.txt"myTextRead.myStreamReader=new StreamReader(strStart);uu=0;dostrBufferStart =myTextRead.myStreamReader.ReadLine();if(strBufferStart=null)break;forea
6、ch (String subString in strBufferStart.Split()lenghuu=Convert.ToInt32(subString); /每行文法存入grammaruu+;while (strBufferStart!=null);myTextRead.myStreamReader.Close();/*/ 讀入文件,進(jìn)行語法分析/*string strReadFile;strReadFile="input.txt"myTextRead.myStreamReader=new StreamReader(strReadFile);string strBu
7、fferText;int wid =0;Console.WriteLine("分析讀入程序(記號ID):n");dostrBufferText =myTextRead.myStreamReader.ReadLine();if(strBufferText=null)break;foreach (String subString in strBufferText.Split()if(subString!="")int ll;if(subString!=null)ll= subString.Length; /每一個長度elsebreak;int a=ll+1;
8、char b = new chara;StringReader sr = new StringReader(subString);sr.Read(b, 0, ll); /把substring 讀到char數(shù)組里int sort=(int)b0;/ wordi 和wordNumi對應(yīng)/先識別出一整個串,再根據(jù)開頭識別是數(shù)字還是字母Wordwid=subString;if(subString.Equals("+")wordNumwid=0;else if(subString.Equals("*")wordNumwid=1;else if(subString.
9、Equals("(")wordNumwid=2;else if(subString.Equals(")")wordNumwid=3;else if(subString.Equals("i")wordNumwid=4;Console.Write(subString+"("+wordNumwid+")"+" ");wid+;Console.WriteLine("n");while (strBufferText!=null);wordNumwid=5;myTex
10、tRead.myStreamReader.Close();/*/讀入LR分析表/*string strLR;strLR="LR-table.txt"myTextRead.myStreamReader=new StreamReader(strLR);string strBufferLR;int pp=0;dostrBufferLR =myTextRead.myStreamReader.ReadLine();if(strBufferLR=null)break;elseint j=0;foreach (String subString in strBufferLR.Split()
11、if(subString!=null)int lllr=Convert.ToInt16(subString);LRpp,j=lllr; /把行與列讀入數(shù)組j+;pp+;while (strBufferLR!=null);myTextRead.myStreamReader.Close();int state = new int100;string symbol =new string100;state0=0;symbol0="#"int p1=0;int p2=0;Console.WriteLine("n按文法規(guī)則歸約順序如下:n");/*/歸約算法/*w
12、hile(true)int j,k;j=statep2;k=wordNump1;t=LRj,k; /當(dāng)出現(xiàn)t為的時候if(t=0)/錯誤類型 string error = "" ; if (k = 0) error = "+" else if (k = 1) error = "*" else if (k = 2) error = "(" else if (k = 3) error = ")" else if (k = 4) error = "i" else error = &
13、quot; 其它錯誤!"Console.WriteLine("n檢測結(jié)果:");Console.WriteLine("代碼中存在語法錯誤");Console.WriteLine("錯誤狀況:錯誤狀態(tài)編號為"+j+" 讀頭下符號為"+error);break;elseif(t=-100) /-100為達(dá)到接受狀態(tài)Console.WriteLine("n");Console.WriteLine("n檢測結(jié)果:");Console.WriteLine("代碼通過
14、語法檢測");break;if(t<0&&t!=-100) /歸約string m=grammar-t;Console.Write(m+" "); /輸出開始符int length=lengh-t; p2=p2-(length-1); Search mySearch=new Search();int right=mySearch.search(m);if(right=0)Console.WriteLine("n");Console.WriteLine("代碼中有語法錯誤");break;int a=st
15、atep2-1;int LRresult= LRa,right;statep2=LRresult;symbolp2=m;if(t>0)p2=p2+1;statep2=t;symbolp2=Convert.ToString(wordNump1);p1=p1+1;myTextRead.myStreamReader.Close();Console.WriteLine("-語法分析結(jié)束-n");Console.Read();class Searchpublic int search(string x)string mysymbol=new string3;mysymbol0="E"mysymbol1="T"mysymbol2="F" int r = 0;for(int s=0;s<=2;s+)if(mysymbols.Equals(
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度企業(yè)年會贊助商權(quán)益執(zhí)行與監(jiān)測合同
- 2025年度新能源合作伙伴廉潔合作協(xié)議(新版)
- 2025年中國安防電源行業(yè)市場前瞻與投資戰(zhàn)略規(guī)劃分析報告
- 2025年變壓器繞組溫度計(jì)項(xiàng)目可行性研究報告
- 2025年度數(shù)據(jù)中心網(wǎng)絡(luò)安全借款合同范本
- 2025年度養(yǎng)老地產(chǎn)項(xiàng)目認(rèn)籌協(xié)議書
- 2019-2025年中國冷凍調(diào)理食品行業(yè)發(fā)展趨勢預(yù)測及投資戰(zhàn)略咨詢報告
- 2025年假離婚協(xié)議書撰寫及隱私保護(hù)服務(wù)合同
- 2025年度藝術(shù)品交易合同:古玩字畫買賣專項(xiàng)服務(wù)協(xié)議
- 2025年陶瓷磚 項(xiàng)目可行性研究報告
- 新能源汽車概論題庫
- 設(shè)備維保的維修成本和維護(hù)費(fèi)用
- 2024年濰坊護(hù)理職業(yè)學(xué)院高職單招(英語/數(shù)學(xué)/語文)筆試歷年參考題庫含答案解析
- 客運(yùn)站員工安全生產(chǎn)教育培訓(xùn)
- 口腔預(yù)防兒童宣教
- 綠城桃李春風(fēng)推廣方案
- 體質(zhì)健康概論
- 檔案管理流程優(yōu)化與效率提升
- 2023高考語文實(shí)用類文本閱讀-新聞、通訊、訪談(含答案)
- 人工智能在商場應(yīng)用
- (完整word版)大格子作文紙模板(帶字?jǐn)?shù)統(tǒng)計(jì))
評論
0/150
提交評論