




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、語法分析實驗報告一、實驗?zāi)康模?1. 了解單詞(內(nèi)部編碼)符號串中的短語句型結(jié)構(gòu)形成規(guī)律。 2. 理解和掌握語法分析過程中語法分析思想(LL,LR)的智能算法化方法。二、實驗內(nèi)容:構(gòu)造自己設(shè)計的小語言的語法分析器: 1. 小語言的語法描述(語法規(guī)則)的設(shè)計即文法的設(shè)計; 2. 把文法形式符號中所隱含的信息內(nèi)容挖掘出來并用LL或LR的資料形式(分析表)表示出來; 3. 語法分析的數(shù)據(jù)輸入形式和輸出形式的確定;4. 語法分析程序各個模塊的設(shè)計與調(diào)試。主要設(shè)備和材料:電腦、winxp操作系統(tǒng)、VC語言系統(tǒng)三、實驗步驟:1、語法規(guī)則 <程序> := <變量定義語句> | <
2、;賦值語句> | <條件語句> | <循環(huán)語句> <變量定義語句> := var 變量,變量; <賦值語句> := 變量 := <表達式>; <表達式> := 標(biāo)識符運算符 標(biāo)識符 ; <標(biāo)識符> := 變量 |常量 <運算符 > := + | - | * | / | >= | <= <條件語句> := <if語句><else語句> <if語句> := if(表達式) thenbegin 賦值語句 |條件語句 | 循環(huán)語句end &l
3、t;else語句> := begin 賦值語句 | 條件語句 | 循環(huán)語句 end <循環(huán)語句> := while(表達式) begin 賦值語句 | 條件語句 | 循環(huán)語句 end <輸出語句> := prn 表達式-注1:若if語句、else語句、循環(huán)語句中出現(xiàn)begin,后面的end必須出現(xiàn),即begin與end同對出現(xiàn)-注2:if、while后的"(",")"表示終結(jié)符,而不是定義成分優(yōu)先的說明符號2、分析表: : =變量常量 , ;運算符 ( )變 量定 義-> -> -> -> 賦 值語
4、句->->->-> ->條 件語 句->-> -> -> -> -> -> 循 環(huán)語 句-> -> -> -> -> -> -> 輸 出語 句->->->分析表(續(xù)):whilevarbeginendifthenprn變 量 定 義-> 賦 值 語 句條 件 語 句->->-> -> 循 環(huán) 語 句-> -> -> 輸 出 語 句->3、 調(diào)試和測試四、實驗總結(jié):本實驗在詞法分析的基礎(chǔ)上,對提取出的標(biāo)識符進行語
5、法判斷。對已有的語法規(guī)則運用LL(1)文法判別并進行構(gòu)造分析表時,遇到的最大困難是:當(dāng)發(fā)生規(guī)約沖突時,該如何處理。如對于產(chǎn)生式s->aAb,當(dāng)對a進行規(guī)約時,滿足語法規(guī)則的(用戶輸入串中當(dāng)前要進行規(guī)約的標(biāo)識符)只有有限種,而不滿足的卻有無限種情況。當(dāng)發(fā)生規(guī)約沖突時,如何在這無限種情況中,確定沖突的具體信息,以便用戶查找。在反復(fù)的嘗試和驗證中,我們發(fā)現(xiàn)發(fā)生沖突的用戶輸入串滿足一定的規(guī)律,且按這種規(guī)律可以把這無限種情況化歸為有限類,于是我們找出其中規(guī)律并進行劃分,然后再對這些有限類沖突進行處理。五、實驗心得:通過這次實驗有以下幾點收獲:1 LR(1)的構(gòu)造使得對理論的知識理解的更加透徹。其中
6、LR(1)分析表構(gòu)造了很多遍,一直無法得到正確結(jié)果,這是恒心的考驗。2 在寫程序中用類數(shù)組來存放單詞屬性使得對單詞各項值的調(diào)用更加方便,特別是對出錯信息的檢測有很大的作用。3 本實驗是在詞法基礎(chǔ)上的更進一步,在詞法程序上添加語法程序,更加理解二者之間的關(guān)系。詞法分析為語法分析提供了詞法單元,方便分析,使程序模塊化,易于讀懂。六、附錄:#include <iostream>#include <fstream>#include <string>#include <math.h>#include <ctype.h>#include <
7、cstdlib>using namespace std;#define Max 655 /最大代碼長度#define WordMaxNum 256 /變量最大個數(shù)#define DigitNum 256/常量最大個數(shù)#define MaxKeyWord32/關(guān)鍵字數(shù)量#define MaxOptANum 8/運算符最大個數(shù)#define MaxOptBNum 4/運算符最大個數(shù)#define MaxEndNum 11/界符最大個數(shù)typedef struct DisplayTableint Index; /標(biāo)識符所在表的下標(biāo)int type;/標(biāo)識符的類型int line;/標(biāo)識符所在表
8、的行數(shù)char symbol20;/標(biāo)識符所在表的名稱Table;int TableNum = 0; /display表的表項總數(shù)char WordWordMaxNum20; /標(biāo)識符表char DigitWordMaxNum20; /數(shù)字表int WordNum = 0; /變量表的下標(biāo)int DigNum = 0; /常量表的下標(biāo)bool errorFlag = 0; /錯誤標(biāo)志 int TableIndex = -1; /display 表的下標(biāo)索引int beginCount = 0;/遇到begin加1,遇到end減1int ifCount = 0; /遇到if加1Table *ta
9、ble = new TableMax;/關(guān)鍵字const char* const KeyWordMaxKeyWord = "and","array", "begin","case","char","constant","do","else","end","false","for","if","input","integer&qu
10、ot;,"not","of","or","output","packed","procedure","program","read","real","repeat","set", "then", "to", "type", "until", "var","while&q
11、uot;, "with","prn" / 單目運算const char OptA = '+','-','*','/','=','#','<','>'/雙目運算符const char *OptB = "<=",">=",":=","<>"/ 界符const char End = '(', ')
12、' , ',' , '' , '.' , '' , '' , ':' , '' , '' , '"' void error(char str20,int nLine, int errorType) errorFlag = 1; cout <<" nError : " switch(errorType) case 1:cout << "第" << nLine-1
13、 <<"行" << str << " 變量的長度超過限制!n" break; case 2:cout << "第" << nLine-1 <<"行" << str << " 小數(shù)點錯誤!n" break;case 3:cout << "第" << nLine-1 <<"行" << str << &quo
14、t; 常量的長度超過限制!n"break;/switch/errorvoid Scanner(char ch,int chLen,int nLine)int chIndex = 0; while(chIndex < chLen) /對輸入的字符掃描/*處理空格和tab*/忽略空格和tabwhile(chchIndex = ' ' | chchIndex = 9 ) chIndex +; /*處理換行符*/ /遇到換行符,行數(shù)加1while(chchIndex = 10) nLine+;chIndex +;/*標(biāo)識符*/if( isalpha(chchIndex)
15、 /以字母、下劃線開頭 char str256; int strLen = 0;/是字母、下劃線while(isalpha(chchIndex) | chchIndex = '_' ) strstrLen + = chchIndex; chIndex +;while(isdigit(chchIndex)/不是第一位,可以為數(shù)字strstrLen + = chchIndex;chIndex +;strstrLen = 0; /字符串結(jié)束符if(strlen(str) > 20) /標(biāo)識符超過規(guī)定長度,報錯處理error(str,nLine,1); elseint i; fo
16、r(i = 0;i < MaxKeyWord; i+) /與關(guān)鍵字匹配/是關(guān)鍵字,寫入table表中 if(strcmp(str, KeyWordi) = 0) strcpy(tableTableNum.symbol,str);tableTableNum.type = 1; /關(guān)鍵字tableTableNum.line = nLine;tableTableNum.Index = i;TableNum +;break; if(i >= MaxKeyWord) /不是關(guān)鍵字tableTableNum.Index = WordNum;strcpy(WordWordNum+,str);ta
17、bleTableNum.type = 2; /變量標(biāo)識符strcpy(tableTableNum.symbol,str);tableTableNum.line = nLine;TableNum +;/*常數(shù)*/else if(isdigit(chchIndex) /遇到數(shù)字int flag = 0;char str256;int strLen = 0;/數(shù)字和小數(shù)點while(isdigit(chchIndex) | chchIndex = '.')/flag表記小數(shù)點的個數(shù),0時為整數(shù),1時為小數(shù),2時出錯if(chchIndex = '.') flag +;
18、strstrLen + = chchIndex;chIndex +; strstrLen = 0;if(strlen(str) > 20) /常量標(biāo)識符超過規(guī)定長度20,報錯處理error(str,nLine,3);if(flag = 0)tableTableNum.type = 3; /整數(shù)if(flag = 1)tableTableNum.type = 4; /小數(shù)if(flag > 1)error(str,nLine,2);tableTableNum.Index = DigNum;strcpy(DigitDigNum +,str);strcpy(tableTableNum.s
19、ymbol,str);tableTableNum.line = nLine;TableNum +;/*運算符*/else/用來區(qū)分是不是無法識別的標(biāo)識符,0為運算符,1為界符int errorFlag;char str3;str0 = chchIndex;str1 = chchIndex + 1;str2 = '0'int i;for( i = 0;i < MaxOptBNum;i+)/MaxOptBNum)if(strcmp(str,OptBi) = 0) errorFlag = 0; tableTableNum.type = 6; strcpy(tableTableN
20、um.symbol,str); tableTableNum.line = nLine; tableTableNum.Index = i; TableNum +; chIndex = chIndex + 2; break;if(i >= MaxOptBNum)for( int k = 0;k < MaxOptANum; k+)if(OptAk = chchIndex)errorFlag = 0;tableTableNum.type = 5;tableTableNum.symbol0 = chchIndex;tableTableNum.symbol1 = 0;tableTableNum
21、.line = nLine;tableTableNum.Index = k;TableNum +;chIndex +;break;/*界符*/for(int j = 0;j < MaxEndNum;j +)if(Endj =chchIndex)errorFlag = 1;tableTableNum.line = nLine;tableTableNum.symbol0 = chchIndex;tableTableNum.symbol1 = 0;tableTableNum.Index = j;tableTableNum.type = 7;TableNum +;chIndex +;/*其他無法
22、識別字符*/開頭的不是字母、數(shù)字、運算符、界符if(errorFlag != 0 && errorFlag != 1) char str256;int strLen = -1;strstrLen + = chchIndex;chIndex +;while(*ch != ' ' | *ch != 9 | chchIndex != 10)strstrLen + = chchIndex;chIndex +;strstrLen = 0;tableTableNum.type = 8;strcpy(tableTableNum.symbol,str);tableTableNu
23、m.line = nLine;tableTableNum.Index = -2;TableNum +; /*把十進制小數(shù)轉(zhuǎn)為16進制*/void Trans(double x,int p) /把十進制小數(shù)轉(zhuǎn)為16進制int i=0; /控制保留的有效位數(shù)while(i<p) if(x=0) /如果小數(shù)部分是0 break; /則退出循環(huán) else int k=int(x*16); /取整數(shù)部分 x=x*16-int(k); /得到小數(shù)部分 if(k<=9) cout<<k; else cout<<char(k+55); ; i+; ;/*語法錯誤*/void
24、 Gerror(int errorType,int nIndex)errorFlag = 1;switch(errorType)case 1:cout << "第" << tablenIndex.line <<"行:" << tablenIndex.symbol <<" 應(yīng)該為賦值號:= n"break; case 2:cout << "第" << tablenIndex.line <<"行:" &l
25、t;< tablenIndex.symbol <<" 應(yīng)為變量 n"break;case 3:cout << "第" << tablenIndex.line <<"行:" << tablenIndex.symbol <<" 應(yīng)為逗號 n"break; case 4:cout << "第" << tablenIndex.line <<"行:" << ta
26、blenIndex.symbol <<" 應(yīng)為分號 n"break;case 5:cout << "第" << tablenIndex.line <<"行:" << tablenIndex.symbol <<" 應(yīng)為運算符 n"break;case 6:cout << "第" << tablenIndex.line <<"行:" << tablenIndex
27、.symbol <<" 應(yīng)為變量或常量 n"break;case 7:cout << "第" <<tableTableIndex.line <<"行 " << tablenIndex.symbol << "與"<<tableTableIndex + 1.symbol <<"之間缺少運算符 n"break;case 8:cout << "第" << table
28、nIndex.line <<"行:" << tablenIndex + 1 .symbol <<" 應(yīng)為'(' n"break;case 9:cout << "第" <<tableTableIndex.line <<"行 " << tableTableIndex.symbol << "與" <<tablenIndex + 1.symbol <<"之間缺
29、少'(' n"break;case 10:cout << "第" << tableTableIndex - 1.line << " 行: 缺少'then'" << endl;break;case 11: cout << "第" << tableTableIndex.line << " 行:"<<tablenIndex.symbol << "應(yīng)為then n&
30、quot; break;case 12:cout << "第" << tableTableIndex.line << " 行: end 后不能接 " <<tableTableIndex.symbol << endl; break;case 13:cout << "第" << tablenIndex.line <<"行: " << tablenIndex - 1.symbol <<"與 &
31、quot; <<tableTableIndex.symbol <<"之間缺少變量 n"break;case 14:cout << "第" <<tablenIndex .line <<"行 " << tablenIndex .symbol << "后缺少'' n"break;case 15:cout << "第" << tableTableIndex.line <<
32、; " 行:"<<tablenIndex.symbol << "應(yīng)為')' n"break;case 16:cout << "第" << tableTableIndex.line << " 行,begin 后不能接 " <<tableTableIndex.symbol << endl;break;/*表達式判斷*/bool express()while(1 ) if(tableTableIndex.type=2|ta
33、bleTableIndex.type = 3 ) if(tableTableIndex.type=3&&tableTableIndex+ 1.type = 2 && tableTableIndex.line = tableTableIndex + 1.line) Gerror(7,TableIndex); /出錯信息:該處缺少運算符/TableIndex = TableIndex + 2;TableIndex +;if(tableTableIndex.line != tableTableIndex + 1.line)/Gerror(14,TableIndex);
34、 /出錯信息:該語句缺少分號return 1; TableIndex +;else if(tableTableIndex.type = 5|tableTableIndex.type = 6)Gerror(13,TableIndex);TableIndex +;else Gerror(6,TableIndex); /出錯信息:該處應(yīng)為變量或常量TableIndex +;if(tableTableIndex.type =5| tableTableIndex.type = 6 )TableIndex +;else if(tableTableIndex.type = 7)return 1;else i
35、f(TableIndex >= TableNum) /| ) Gerror(14,TableIndex); /出錯信息:該語句缺少分號 return 1;else Gerror(5,TableIndex); /出錯信息:此處應(yīng)為運算符 TableIndex +;/*賦值語句判斷*/bool Assign() /賦值語句的判斷TableIndex +;if(strcmp( ":=" , tableTableIndex.symbol) = 0)TableIndex +; else Gerror(1,TableIndex); /出錯信息:賦值號應(yīng)該為":=&quo
36、t; TableIndex +;if(express() /":="后可以為變量或常量if(strcmp("",tableTableIndex.symbol) = 0)return 1;else if(TableIndex >= TableNum) Gerror(14,TableIndex); /出錯信息:該語句缺少分號 return 1;else if(tableTableIndex.line != tableTableIndex + 1.line)Gerror(14,TableIndex); /出錯信息:該語句缺少分號return 1;/Tab
37、leIndex +;else Gerror(6,TableIndex); /出錯信息:":="后應(yīng)為變量或常量 TableIndex +;return 0;/*語句判斷*/bool judge() /條件、循環(huán)、初始化語句的判斷/*begin*/if(strcmp("begin",tableTableIndex.symbol)=0) /匹配begin beginCount +; if(tableTableIndex + 1.type = 7)TableIndex +;cout << "第" << tableTa
38、bleIndex.line << " 行,begin 后不能接 " <<tableTableIndex.symbol << endl;return 1;/*end*/if(strcmp("end",tableTableIndex.symbol) = 0) /匹配endbeginCount -;if(TableIndex < TableNum)if(tableTableIndex+1.type=7|tableTableIndex+ 1.type = 8)TableIndex +;Gerror(12,TableInd
39、ex);return 1; /*else*/if(strcmp("else",tableTableIndex.symbol) = 0) /匹配else ifCount -; return 1;if(strcmp("prn",tableTableIndex.symbol) = 0)/匹配prn TableIndex +; if(tableTableIndex.type = 2 | tableTableIndex.type = 3) / prn 后為變量或常量TableIndex +; /語句結(jié)束,“;”if(strcmp("",tabl
40、eTableIndex.symbol) = 0)return 1; else Gerror(4,TableIndex);/出錯信息:此處應(yīng)為"" /ifelse Gerror(2,TableIndex);/出錯信息:此處應(yīng)為變量 TableIndex +; /if_prn/*var變量定義*/if(strcmp("var",tableTableIndex.symbol) = 0)/ var a,b,c;TableIndex +;if(tableTableIndex.type != 2)if(strcmp(",",tableTableI
41、ndex.symbol) = 0)Gerror(13,TableIndex);else Gerror(2,TableIndex);/出錯信息:此處應(yīng)為變量TableIndex +;if(strcmp(",",tableTableIndex.symbol) !=0)Gerror(3,TableIndex); /出錯信息:此處應(yīng)為","TableIndex +;while(1)while(strcmp(",",tableTableIndex.symbol)=0)TableIndex = TableIndex + 1;if(tableTab
42、leIndex.type !=2)Gerror(2,TableIndex);/出錯信息:此處應(yīng)為變量TableIndex +; TableIndex +;if(strcmp("",tableTableIndex.symbol)=0)return 1;else Gerror(4,TableIndex);/出錯信息:此處應(yīng)為分號""return 0;/*if語句判斷*/if語句else if(strcmp("if",tableTableIndex.symbol) = 0) ifCount +; /if個數(shù)加1 if(tableTableIndex +1.type = 2 | tableTableIndex + 1.type = 3)Gerror(9,TableIndex); /出錯信息:此處缺少')'TableIndex +; else if(strcmp("(",tableTableIndex + 1.symbol) != 0
溫馨提示
- 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. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2023-2024學(xué)年四川省瀘州市江陽區(qū)高二下學(xué)期6月期末數(shù)學(xué)試題(解析版)
- 2025屆河南省天一小高考高三三模語文試題(解析版)
- 新疆紫金鋅業(yè)有限公司烏拉根鋅礦新增15000td低品位資源及廢石綜合利用技改尾礦庫工程環(huán)境影響報告書
- 房屋委托轉(zhuǎn)讓協(xié)議
- 環(huán)境工程原理課件下載
- 佛教寺院僧人管理制度
- 作品創(chuàng)作審批管理制度
- 供暖動態(tài)日常管理制度
- 供熱企業(yè)考核管理制度
- 依法辦學(xué)監(jiān)督管理制度
- 物業(yè)消防安全管理培訓(xùn)【共54張課件】
- 2024福建金皇環(huán)??萍脊拘@招聘(高頻重點提升專題訓(xùn)練)共500題附帶答案詳解
- DL∕T 5766-2018 20KV及以下配電網(wǎng)工程工程量清單計算規(guī)范
- 【正版授權(quán)】 ISO/IEC 15421:2010 EN Information technology - Automatic identification and data capture techniques - Bar code master test specifications
- 云南省昆明市官渡區(qū)2023-2024學(xué)年五年級下學(xué)期7月期末道德與法治試題
- GB/T 43988-2024滑板課程學(xué)生運動能力測評規(guī)范
- GB/T 44092-2024體育公園配置要求
- DL-T1069-2016架空輸電線路導(dǎo)地線補修導(dǎo)則
- 2024年陜西新華出版?zhèn)髅郊瘓F有限責(zé)任公司招聘筆試沖刺題(帶答案解析)
- 江蘇開放大學(xué)本科行政管理專業(yè)060193國家公務(wù)員制度期末試卷
- 農(nóng)村排灌用電安全管理
評論
0/150
提交評論