




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、 編譯原理實驗報告實 驗 編 號實驗二實 驗 名 稱詞法分析程序的設(shè)計與實現(xiàn)學(xué) 號:姓 名:提交日期:成 績:一實驗二:詞法分析程序的設(shè)計與實現(xiàn)【實驗?zāi)康暮鸵蟆?設(shè)計、編制、調(diào)試一個具體的詞法分析程序,加深對詞法分析原理的理解?!緦嶒灻枋觥?通過對pl/0詞法分析程序(getsym)的分析,并在此基礎(chǔ)上按照附錄a中給出的pl/0語言的語法描述,編寫一個pl/0語言的詞法分析程序。此程序應(yīng)具有如下功能: 輸入為字符串(待進(jìn)行詞法分析的源程序),輸出為單詞串,即由(單詞、類別)所組成的二元組序列。有一定檢查錯誤的能力,例如發(fā)現(xiàn)2a這類不能作為單詞的字符串?!局饕兞棵f明】char *key8=
2、"if","else","for","while","do","return","break","continue" 確定關(guān)鍵字char *border6=",","","","","(",")"確定界符char *arithmetic4="+","-","*"
3、;,"/"確定算術(shù)運算符 char *relation6="<","<=","=",">",">=","<>"確定關(guān)系運算符【程序清單】#include <stdio.h>#include <ctype.h> #include <stdlib.h>#include <string.h>#define null 0#include <iostream>using
4、namespace std;file *fp;char cbuffer;char *key8="if","else","for","while","do","return","break","continue"char *border6=",","","","","(",")"char *arithmetic4=&qu
5、ot;+","-","*","/"char *relation6="<","<=","=",">",">=","<>"char *consts20;char *label20;int constnum=0,labelnum=0;/int search(char searchchar,int wordtype)int i=0;switch (wordtype) case 1
6、:for (i=0;i<=7;i+)if (strcmp(keyi,searchchar)=0)return(i+1); return 0; case 2:for (i=0;i<=5;i+) if (strcmp(borderi,searchchar)=0)return(i+1); return(0); case 3:for (i=0;i<=3;i+)if (strcmp(arithmetici,searchchar)=0)return(i+1); return(0); case 4:for (i=0;i<=5;i+) if(strcmp(relationi,searc
7、hchar)=0)return(i+1); return(0); case 5:for (i=0;i<=constnum;i+) if(i!=constnum)if (strcmp(constsi,searchchar)=0)return(i+1); constsi-1=(char *)malloc(sizeof(searchchar);strcpy(constsi-1,searchchar);constnum+;return(i);case 6:for (i=0;i<=labelnum;i+) if(i!=labelnum)if(strcmp(labeli,searchchar)
8、=0)return(i+1);labeli-1=(char *)malloc(sizeof(searchchar);strcpy(labeli-1,searchchar);labelnum+;return(i);default:return 0;/char alphaprocess(char buffer)int atype; int i=-1; char alphatp20; while (isalpha(buffer)|(isdigit(buffer)alphatp+i=buffer; buffer=fgetc(fp); alphatpi+1='0' if (atype=s
9、earch(alphatp,1)printf("%8s (1,%d)n",alphatp,atype-1); elseatype=search(alphatp,6);printf("%8s (6,%d)n",alphatp,atype-1); return(buffer);/char digitprocess(char buffer)int i=-1; char digittp20; int dtype; while (isdigit(buffer)digittp+i=buffer; buffer=fgetc(fp); digittpi+1='0
10、' dtype=search(digittp,5); printf("%8s (5,%d)n",digittp,dtype-1); return(buffer);/char otherprocess(char buffer)int i=-1; char othertp20; int otype,otypetp; othertp0=buffer; othertp1='0' if (otype=search(othertp,3)printf("%8s (3,%d)n",othertp,otype-1);buffer=fgetc(fp)
11、;goto out;if(otype=search(othertp,4)buffer=fgetc(fp); othertp1=buffer; othertp2='0' if (otypetp=search(othertp,4)buffer=fgetc(fp); elseothertp1='0' printf("%8s (4,%d)n",othertp,otype-1);goto out;if (buffer=':')buffer=fgetc(fp);if (buffer='=')printf("%8s
12、 (2,2)n",":=");buffer=fgetc(fp);goto out; elseif (otype=search(othertp,2)printf("%8s (2,%d)n",othertp,otype-1);buffer=fgetc(fp);goto out;if (buffer!='n')&&(buffer!=' ')&&(buffer!='t')printf("%8c error,not a wordn",buffer); bu
13、ffer=fgetc(fp);out: return(buffer);/void main()int i;for (i=0;i<=20;i+)labeli=null; constsi=null;if (fp=fopen("example.c","r")=null)printf("error!n");elsecbuffer = fgetc(fp);while(cbuffer!=eof)if (isalpha(cbuffer)cbuffer=alphaprocess(cbuffer);else if (isdigit(cbuffer
14、)cbuffer=digitprocess(cbuffer);else cbuffer=otherprocess(cbuffer);printf("over!n");system("pause");/ 源程序over/實驗中用到的測試的 example.c的文件內(nèi)容/#include <stdio.h>void mian() int a=10,b=5;if(a<b)printf("a is smaller than b");else if(a>b)printf("a is bigger than b");elsedoa=b+a; b=a*b; break;while(a=b)return 0;/圖2程序運行結(jié)果:圖1圖4圖3【調(diào)試情況】程序中,用到了較多的數(shù)組和動態(tài)數(shù)組分配,和較多的循環(huán)判斷,在動態(tài)內(nèi)存分配時出現(xiàn)了較多的錯誤,而在關(guān)鍵字和單詞的判定中,也出現(xiàn)了 不可預(yù)知的錯誤和警告
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 計算機軟件編程基礎(chǔ)試題集及答案解析
- 移動醫(yī)療健康應(yīng)用軟件授權(quán)使用協(xié)議
- 物業(yè)管理裝修協(xié)議書
- 產(chǎn)品市場推廣策略與操作手冊編制
- 設(shè)備分期付款銷售合同
- 初中生心理健康故事
- 國際物流與運輸合同
- 知識產(chǎn)權(quán)轉(zhuǎn)讓協(xié)議簽署細(xì)節(jié)說明
- 物流行業(yè)個性化配送優(yōu)化方案
- 初中生職業(yè)規(guī)劃課程心得
- 人教版(2024新版)英語七年級上冊期末復(fù)習(xí)綜合測試卷(含答案)
- 網(wǎng)絡(luò)虛擬貨幣交易合同
- 2 讓我們的家更美好 第一課時 擔(dān)當(dāng)家庭責(zé)任(教學(xué)設(shè)計)2023-2024學(xué)年統(tǒng)編版道德與法治五年級下冊
- 2024年中國電動助力汽車轉(zhuǎn)向系統(tǒng)(EPS)行業(yè)市場現(xiàn)狀、前景分析研究報告
- 復(fù)數(shù)算符在量子力學(xué)中的應(yīng)用
- 2023年全國初中物理競賽試題及答案
- 2024年各地中考試卷【文言文閱讀題】匯集練(一)附答案解析
- 2024年重慶市中考英語試卷真題B卷(含標(biāo)準(zhǔn)答案及解析)+聽力音頻
- 旅游景區(qū)服務(wù)標(biāo)準(zhǔn)化操作手冊
- 中央2024年中國醫(yī)學(xué)科學(xué)院血液學(xué)研究所血液病醫(yī)院招聘17人筆試歷年典型考題及考點附答案解析
- 產(chǎn)調(diào)授權(quán)委托書模板
評論
0/150
提交評論