天津理工大學(xué)編譯原理實(shí)驗(yàn)一_第1頁
天津理工大學(xué)編譯原理實(shí)驗(yàn)一_第2頁
天津理工大學(xué)編譯原理實(shí)驗(yàn)一_第3頁
天津理工大學(xué)編譯原理實(shí)驗(yàn)一_第4頁
天津理工大學(xué)編譯原理實(shí)驗(yàn)一_第5頁
已閱讀5頁,還剩18頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、實(shí)驗(yàn)報告學(xué)院(系)名稱:計算機(jī)與通信工程學(xué)院姓名學(xué)號專業(yè)計算機(jī)科學(xué)與技術(shù)班級實(shí)驗(yàn)項(xiàng)目實(shí)驗(yàn)一:詞法分析課程名稱編譯原理課程代碼實(shí)驗(yàn)時間2016/03/172016/03/22實(shí)驗(yàn)地點(diǎn)軟件實(shí)驗(yàn)室7-219批改意見成績教師簽字: 實(shí)驗(yàn)內(nèi)容:實(shí)現(xiàn)標(biāo)準(zhǔn)C語言詞法分析器。實(shí)驗(yàn)要求:(1)單詞種別編碼要求 基本字、運(yùn)算符、界符:一符一種 標(biāo)識符:統(tǒng)一為一種; 常量:按類型編碼;(2)詞法分析工作過程中建立符號表、常量表。 并以文本文件形式輸出。(3)詞法分析的最后結(jié)果以文本文件形式輸出。實(shí)驗(yàn)源代碼和心得體會#include<iostream>#include<ctype.h> #in

2、clude<cstring>#define bufsize1024/關(guān)鍵字 #define INCLUDE256 #define AUTO257 #define BREAK258 #define CASE259 #define CHAR260 #define CONST261 #define CONTINUE262 #define DEFAULT263 #define DO264 #define DOUBLE265 #define ELSE266 #define ENUM267 #define EXTERN268 #define FLOAT269 #define FOR 270 #

3、define GOTO 271 #define IF 272 #define INT 273 #define LONG 274 #define REGISTER275 #define RETURN276 #define SHORT277 #define SIGNED278 #define SIZEOF279 #define STATIC 280 #define STRUCT281 #define SWITCH282 #define TYPEDEF283 #define UNION284 #define UNSIGNED285 #define VOLATILE286 #define WHILE2

4、87 /運(yùn)算符 #define PLUS288/+#define MINUS289/-#defineMUL290/*#define DIV291/#define REMAIN292/%#define GREATER293/>#defineLESS294/<#defineEQUAL295/=#defineMISTAKE296/!#define AND297/&#define OR298/ | #define PP299/+#defineMM300/-#defineEE301/=#defineGE302/>=#define LE303/<=#define MISE3

5、04/!=#define AA305/&&#define OO306/|#define PE307/+=#define MINUSE308/ -=#defineMULE309/*=#define DIVE310/=#define POW311/ 界符 #define SEMIC312/;#define COMMA313/,#define MULANNO_L314/*#define MULANNO_R315/*/ #define BRACE_L316/#define BRACE_R317/#define BRAKET_L318/(#define BRAKET_R319/)#def

6、ine MIDBRA_L320/#define MIDBRA_R321/#define ONE_ANNO322/ /標(biāo)識符和常量符 #define TAG400#defineCONINT401#defineCONFLOAT402#defineCONCHAR403#defineCONSTRING404/轉(zhuǎn)義字符和字符串#define CA 500 #define CB 501 #define CF 502 #define CN 503 #define CR 504 #define CT 505 #define CV 506 #define CBSL 507 #define CQUE 508 #d

7、efine CDQM 509 #define CQM 510 #define ZERO 511 using namespace std;typedef struct Variate/變量標(biāo)識符 int id;char name50;Variate;typedef struct Constant/常量 int id;char name50;Constant;typedef struct Signchar name100; int sym; char attr100;Sign;const char *keywordTable="include","auto"

8、,"break","case","char","const","continue", "default","do","double","else","enum","extern","float","for", "goto","if","int","long",

9、"register","return","short","signed", "sizeof","static","struct","switch","typedef","union","unsigned", "volatile","while","","#"/#作用是判斷是否結(jié)束 const cha

10、r *operateTable="+","-","*","/","%",">","<","=","!","&","|","+","-","=",">=","<=","!=","&&",&quo

11、t;|","+=","-=","*=","/=","","#"const char *borderTable="",",","/*","*/","","","(",")","","","/","#"const char chang

12、eList12='a','b','f','n','r','t','v','','?','"',''','0' FILE *in;FILE *Out;FILE *Error; int line=1;/用于輸出錯誤的行數(shù)或者其他情況。默認(rèn)值是從 1 開始,為第一行char bufbufsize;/存儲讀取的一行的字符串 char firchar;/頭文件下第一個字符 char Char;in

13、t start=0;int VariateNum=0;/記錄變量的個數(shù),減去1 int ConstantNum=0;/記錄常量的個數(shù)int SignNum=0;/記錄標(biāo)記的個數(shù) int notation=1;/記錄是否找到多行注釋的另一半 */ ,默認(rèn)值是1 即為有另一半 int isNotation=0;/判斷是否是在注釋行內(nèi) 0 不是, 1 單行注釋 2多行注釋 bool last=false;int late=0;Variate var;Constant con;Sign sign;Variate VarArrbufsize;Constant ConArrbufsize;Sign Sig

14、Arrbufsize;/獲取讀取的文件本行的第一個字符 ,直到找到一個非空格字符 char getfirstc(FILE *in)char ch=fgetc(in);/fgetc() 函數(shù)的作用是讀取文件的當(dāng)前行的一個字符,返回讀取的字符 while(ch=' '|ch='n'|ch='t')if(ch='n')line+;fputc('n',Out);/向輸出的文本文件中打印換行 ch=fgetc(in);return ch;/處理讀取的本行內(nèi)容void dealhead(char *buf) char ch10

15、;char cha; char strbufsize;int i=0;int j=0;int temp=0;while(i<bufsize)/找到include<并保存 if(bufi!=' '&&bufi!='0')if(temp=0)if(bufi='i')temp=1;else if(bufi='d')temp=2;elsefprintf(Error,"Line:%dtformat is wrong! Without'>'n",line);break; i

16、f(temp=1)chj=bufi;j+;if(bufi='<')chj='0'break;else if(temp=2)chj=bufi;j+;if(bufi+1=' ')chj='0'i+;break;i+;if(temp=1)int index=0;fputc('#',Out);while(bufindex!='0')if(bufindex!=' ')fputc(bufindex,Out);index+;if(strcmp(ch,"include<&quo

17、t;)=0) i+;/因?yàn)樯厦娉绦驔]進(jìn)行+就直接break所以這里就需要加 1到下一個角標(biāo) while(cha=bufi)!='>') i+; if(cha='n') fprintf(Error,"Line:%dtinclude end without '>'n",line); break; else fprintf(Error,"Line:%dtinclude format is wrongn",line);else if(temp=2)if(strcmp(ch,"define&qu

18、ot;)!=0) i+;while(cha=bufi)=' ') if(cha='n') / fprintf(Error,"Line:%dtinclude end without '>'n",line); break; i+; if(bufi!=' ') if(!(isalpha(bufi) fprintf(Error,"Line:%dtdefine format is wrongn",line); /此處意思是define后必須有變量名稱 else/在上面的break之前已經(jīng)進(jìn)行過i

19、+;所以這里可以直接用 while(!isalpha(bufi)if(bufi='0')fprintf(Error,"Line:%dtdefine without vatiate and name!n",line);break;i+;int index=0;int space=0;/計算在上一個字母之后第幾次遇到空格 fputc('#',Out);while(bufindex!='0')if(bufindex!=' ')fputc(bufindex,Out);space=0;elsespace+;if(spac

20、e=1&&index!=0) fputc(' ',Out); index+;/處理頭文件 char head(FILE *in)char ch;if(late=0)ch=getfirstc(in);elsech=firchar;/即為# while(ch='#')fgets(buf,bufsize,in);/fgets()讀取in文件當(dāng)前一行的內(nèi)容為bufsize-1個字符的內(nèi)容int len=strlen(buf); buflen-1='0'dealhead(buf); /這條語句執(zhí)行完畢后且找到'>'就說

21、明這行結(jié)束 line+;ch=getfirstc(in);if(ch='#')fputc('n',Out);return ch;/判斷關(guān)鍵字 int keyword(char *str)int i; for(i=0;keywordTablei!="#"i+) if(strcmp(str,keywordTablei)=0) return i+256; /返回關(guān)鍵字對應(yīng)的值 return -1; /處理字母 void dealAlpha()char str50;Variate var;Sign sign;int i;int key;/記錄字符串s

22、tr對應(yīng)的值。 str0=firchar;for(i=start;isalpha(bufi)|isdigit(bufi);i+)stri-start+1=bufi;/將本行的第一個字符串賦值給str stri-start+1='0'start=i;firchar=bufstart;key=keyword(str);if(key=-1)/說明不是關(guān)鍵字,是標(biāo)識符。即為變量,函數(shù)名一類的 var.id=VariateNum;strcpy(,str);VarArrVariateNum=var;VariateNum+;sign.sym=TAG;sprintf(sign.

23、attr,"%d",var.id);/暫時不知道什么用處 strcpy(,str);SigArrSignNum=sign;SignNum+;else/說明是關(guān)鍵字 sign.sym=key;strcpy(,str);strcpy(sign.attr,"-");/sign.attr="-"是不正確的,attr的長度大,后面的不能忽略 SigArrSignNum=sign;SignNum+;/判斷是否在常量表里int InConTable(char *name)int i; for(i=0;i<C

24、onstantNum;i+) if(strcmp(name,ConA)=0) return ConArri.id; return -1; /處理數(shù)字void dealDigit()int symbol; int id; char word100; Sign sign; Constant constant; int i; word0 = firchar; for(i=start; isdigit(bufi); i+) wordi-start+1 = bufi; if(bufi='.') i+; if(!isdigit(bufi) start=i; firchar=

25、bufstart; fprintf(Error,"Line: %dtunavailabe floatn",line); return; wordi-start = '.' for(;isdigit(bufi);i+) wordi-start+1 = bufi; wordi-start+1='0' start=i; firchar=bufstart; id=InConTable(word); /*不在常量表里,新加項(xiàng)*/ if(id=-1) constant.id=ConstantNum; strcpy(,word);

26、 ConArrConstantNum=constant; ConstantNum+; id=constant.id; sign.sym=CONFLOAT; sprintf(sign.attr,"%d",id);/change int to string strcpy(,word); SigArrSignNum=sign; SignNum+; else wordi-start+1='0' start=i; firchar= bufstart; id=InConTable(word); /*不在常量表里,新加項(xiàng)*/ if(id=-1) cons

27、tant.id = ConstantNum; strcpy(,word); ConArrConstantNum=constant; ConstantNum+; id=constant.id; sign.sym=CONINT; sprintf(sign.attr,"%d",id);/change int to string strcpy(,word); SigArrSignNum=sign; SignNum+; /處理注釋void dealNotation()char str3;str0='/'str2='0

28、'if(bufstart='/')/確定是單行注釋。 isNotation=1; Sign sign;str1='/'int i;for(i=0;borderTablei!="#"i+)if(strcmp(str,borderTablei)=0)sign.sym=i+288;break;strcpy(,str);strcpy(sign.attr,"-");SigArrSignNum=sign;SignNum+; else/多行注釋 isNotation=2; str1='*'Sig

29、n sign1,sign2;int i;int st;for(i=0;borderTablei!="#"i+)if(strcmp(str,borderTablei)=0)sign1.sym=i+312;break;strcpy(,str);strcpy(sign1.attr,"-");SigArrSignNum=sign1;SignNum+;fputc('n',Out);if(Char!='/')fputc(Char,Out);int len ;len=strlen(buf);char c=Char;f

30、or(start=0;start<len;start+)/將多行注釋/*之前的非注釋部分以及/*先進(jìn)行輸出 if(c='/'&&bufstart='*')fputc('/',Out);fputc('*',Out);break;elsefputc(bufstart,Out);c=bufstart;start+;if(bufstart='0')/ /*XXXn 類型fputc('n',Out);line+;while(fgets(buf,bufsize,in)!=NULL)/說明這

31、一行至少有一個字符 0除外 len=strlen(buf); buflen-1='0'start=0;if(bufstart='0')fputc('n',Out);line+;elsest=start;start+;while(bufstart!='0')if(bufst='*'&&bufstart='/')str0=bufst;str1=bufstart;for(i=0;borderTablei!="#"i+)if(strcmp(str,borderTablei

32、)=0)sign2.sym=i+312;break;strcpy(,str);strcpy(sign2.attr,"-");SigArrSignNum=sign2;SignNum+;fputs(,Out);start+;firchar=bufstart;last=true;return ;st=start;start+;fputc('n',Out);line+; notation=0;elsest=start;start+;while(bufstart!='0')if(bufst='*'

33、&&bufstart='/')/ /*/ 類型str0=bufst;str1=bufstart;for(i=0;borderTablei!="#"i+)if(strcmp(str,borderTablei)=0)sign2.sym=i+312;break;strcpy(,str);strcpy(sign2.attr,"-");SigArrSignNum=sign2;SignNum+;len=strlen(buf);for(int temp=start;temp<len;temp+)if(bufte

34、mp-1='*'&&buftemp='/')temp-;while(buftemp!='0')fputc(buftemp,Out);temp+;break;last=true;return ;st=start;start+;fputc('n',Out); / /*XXXn 類型line+;while(fgets(buf,bufsize,in)!=NULL)int len=strlen(buf); buflen-1='0'start=0;if(bufstart='0')fputc(&#

35、39;n',Out);line+;elsest=start;start+;while(bufstart!='0')if(bufst='*'&&bufstart='/')str0=bufst;str1=bufstart;for(i=0;borderTablei!="#"i+)if(strcmp(str,borderTablei)=0)sign2.sym=i+312;break;strcpy(,str);strcpy(sign2.attr,"-");SigArrSig

36、nNum=sign2;SignNum+;for(int temp=st;temp<len;temp+)if(buftemp!='0')fputc(buftemp,Out);elsebreak;last=true;return ;st=start;start+;fputc('n',Out);line+; notation=0; /判斷界符bool Border(char c) for(int i=0;borderTablei!="#"i+)if(c=borderTablei0&&borderTablei1='0&#

37、39;)return true;return false; /處理界符void dealBorder()/這里已經(jīng)將注釋和普通界符分開,所以所有已定義的界符都是單個的 char str2;Sign sign;str0=firchar;str1='0'int i=0;while(borderTablei!="#")if(strcmp(str,borderTablei)=0)strcpy(,str);strcpy(sign.attr,"-");sign.sym=i+312;SigArrSignNum=sign;SignNum

38、+;firchar=bufstart;return;i+; /判斷運(yùn)算符int Operate(char ch)for(int i=0;operateTablei!="#"i+)if(ch=operateTablei0)return 1;return 0; /處理運(yùn)算符 void dealOperate()char str3;Sign sign;str0=firchar;str1=bufstart;str2='0'int i;if(/*str1!='0'&&str1!=' '&&*/Operat

39、e(str1)/說明是雙運(yùn)算符 i=0;while(operateTablei!="#")if(strcmp(str,operateTablei)=0)strcpy(,str);sign.sym=i+288;strcpy(sign.attr,"-");SigArrSignNum=sign;SignNum+;firchar=bufstart;start+;return;i+;str1='0'/說明是單號 i=0;while(operateTablei!="#")if(strcmp(str,operate

40、Tablei)=0)strcpy(,str);sign.sym=i+288;strcpy(sign.attr,"-");SigArrSignNum=sign;SignNum+;firchar=bufstart;return; i+;void dealChar(char ch)Sign sign; Constant constant; int i=start,id,j; char word100; word0=ch; if(ch=''') if(bufi='') for(j=0;j<12;j+) if(bufi+

41、1=changeListj) word1='' word2=bufi+1; word3=''' word4='0' strcpy(,word); strcpy(sign.attr,"-"); sign.sym=j+500; SigArrSignNum=sign; SignNum+; start=i+3; firchar=bufstart; return; if(j=12) fprintf(Error, "Line: %dtunavailable change charn",line

42、); else if(bufi+1 != ''') fprintf(Error, "Line: %dtThe length of const char is unavailaben",line); for(i=i+2;bufi!='''i+); start=i+1; firchar=bufstart; return; else word1=bufi; word2=''' word3='0' id=InConTable(word); if(id=-1) constant.id=Consta

43、ntNum; strcpy(,word); ConArrConstantNum=constant; ConstantNum+; id=constant.id; sign.sym=CONCHAR; sprintf(sign.attr,"%d",id);/change int to string strcpy(,word); SigArrSignNum=sign; SignNum+; start=i+2; firchar=bufstart; return; else if(ch='"')/字符串常量 for(

44、;bufi!='"'i+) wordi-start+1=bufi; wordi-start+1='"' wordi-start+2='0' id=InConTable(word); /*不在常量表里*/ if(id=-1) constant.id=ConstantNum; strcpy(,word); ConArrConstantNum=constant; ConstantNum+; id=constant.id; sign.sym=CONSTRING; sprintf(sign.attr,"

45、;%d",id);/change int to string strcpy(,word); SigArrSignNum=sign; SignNum+; start=i+1; firchar=bufstart; void writeSign() FILE* fsign; int i=0; fsign=fopen("sign.txt", "w+"); if(fsign=NULL) printf("Cant create file sign.txt!n"); for(i=0;i<SignNum;i+) fprintf(fsign,"%stt(%d, %s)n",SigA,SigArri.sym,SigArri.attr); fclose(fsign); void writeVariate() FILE* fVariate; int i=0; fVariate=fopen("Variate.txt"

溫馨提示

  • 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

提交評論