完整版-編譯實驗報告詞法分析_第1頁
完整版-編譯實驗報告詞法分析_第2頁
完整版-編譯實驗報告詞法分析_第3頁
完整版-編譯實驗報告詞法分析_第4頁
完整版-編譯實驗報告詞法分析_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論