![北郵編譯原理詞法分析器實(shí)驗(yàn)_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/4/64e5b8e1-e5f2-49eb-a8aa-95757e8ac84a/64e5b8e1-e5f2-49eb-a8aa-95757e8ac84a1.gif)
![北郵編譯原理詞法分析器實(shí)驗(yàn)_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/4/64e5b8e1-e5f2-49eb-a8aa-95757e8ac84a/64e5b8e1-e5f2-49eb-a8aa-95757e8ac84a2.gif)
![北郵編譯原理詞法分析器實(shí)驗(yàn)_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/4/64e5b8e1-e5f2-49eb-a8aa-95757e8ac84a/64e5b8e1-e5f2-49eb-a8aa-95757e8ac84a3.gif)
![北郵編譯原理詞法分析器實(shí)驗(yàn)_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/4/64e5b8e1-e5f2-49eb-a8aa-95757e8ac84a/64e5b8e1-e5f2-49eb-a8aa-95757e8ac84a4.gif)
![北郵編譯原理詞法分析器實(shí)驗(yàn)_第5頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/4/64e5b8e1-e5f2-49eb-a8aa-95757e8ac84a/64e5b8e1-e5f2-49eb-a8aa-95757e8ac84a5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、詞法分析程序設(shè)計(jì)一問題描述1.可以識(shí)別出用c語言編寫的源程序中的每個(gè)單詞符號(hào),并以記號(hào)的形式輸出每個(gè)單詞符號(hào)。2.可以并識(shí)別讀取源程序中的注釋。3.可以統(tǒng)計(jì)源程序中的語句行數(shù)、單詞個(gè)數(shù)和字符數(shù),其中標(biāo)點(diǎn)和空格不計(jì)為單詞,并輸出統(tǒng)計(jì)結(jié)果。4.檢察源程序中存在的錯(cuò)誤,并可以報(bào)告錯(cuò)誤所在行列的位置。5.發(fā)現(xiàn)原程序中存在的錯(cuò)誤,進(jìn)行適當(dāng)修復(fù),使詞法分析可以繼續(xù)進(jìn)行,通過一次詞法分析處理,可以檢查并報(bào)告源程序中存在的所有錯(cuò)誤。二算法思想編寫一個(gè)詞法分析程序,它從左到右逐個(gè)字符的對(duì)源程序進(jìn)行掃描,產(chǎn)生一個(gè)個(gè)的單詞形成記號(hào)流文件輸出。其中,具體子問題有:(1)源程序文件讀入緩沖區(qū)中(注意要?jiǎng)h除空格和無用符號(hào)
2、)(2)確定讀入的為關(guān)鍵字還是運(yùn)算符還是變量名,對(duì)于普通標(biāo)識(shí)符和常量,分別建立標(biāo)識(shí)符表和常量表當(dāng)遇到一個(gè)標(biāo)識(shí)符或常量時(shí),查找標(biāo)識(shí)符表或常量表,若存在,則返回位置,否則進(jìn)入符號(hào)表或常量表中并返回表的入口地址。(3)對(duì)于各類運(yùn)算符、標(biāo)點(diǎn)符號(hào)、以及注釋符號(hào)等,準(zhǔn)確識(shí)別出來并打印輸出結(jié)果(4)對(duì)于源文件中出現(xiàn)的數(shù)字常量,不但能按要求加入常量表中,還進(jìn)行了字符型到float型數(shù)值的轉(zhuǎn)換,便于后續(xù)程序操作處理。(4)盡量精簡整合各種情況,使算法復(fù)雜度降低,精簡易讀。三、實(shí)驗(yàn)程序設(shè)計(jì)說明1.主要函數(shù)說明void readchar();/讀字符過程,每調(diào)用一次,從輸入緩沖區(qū)讀一個(gè)字符,并把它放入變量c中,且向
3、前掃描指針pointer指向下一個(gè)字符void ignorespace();/每次調(diào)用時(shí),檢查c中的字符是否為空字符,若是,則反復(fù)調(diào)用該過程,直到c進(jìn)入一個(gè)非空字符為止void link();/把c中的字符與token中的字符串連接起來bool alphabet();/布爾函數(shù),判斷c中的字符是否為字母,若是則返回true,否則返回falsebool digit();/布爾函數(shù),判斷c中的字符是否為數(shù)字,若是則返回true,否則返回falseint searchforkeywords();/查關(guān)鍵字表,若此函數(shù)的返回值為0,則表示token中的字符串是標(biāo)識(shí)符,否則為關(guān)鍵字int searchf
4、ortoken();/查符號(hào)表,若此函數(shù)的返回值為0,則表示token中的字符串是新出現(xiàn)的,否則為已出現(xiàn)過的標(biāo)識(shí)符int searchfornum(); /查常數(shù)表,若此函數(shù)的返回值為0,則表示token中的數(shù)字是新出現(xiàn)的,否則為已出現(xiàn)的常數(shù)void inserttokenlist();/將標(biāo)識(shí)符插入符號(hào)表void insertnumlist();/將數(shù)字插入常數(shù)表void fillbuffer(int a);/ 填充buff的半?yún)^(qū)函數(shù)2程序源代碼#include#include#include#includeint pointer=0;/int i=0,j=0,c=0,appear,d=0,
5、num_location;/int row_num=0,letter_num=0,word_num=0;int z=1;char c= ;/char token30;/char buff4095;/char token_list20030;/char number20010;/char next_charac,charac,file_name20;char keywords3210=auto,break,case,char,const,continue,default,do,double,else,enum,extern,float,for,goto,if,int,long,register,
6、return,short,signed,sizeo,fstatic,struct,switch,typedef,union,unsigned,void,volatile,while;double num;file *file_pointer;void fillbuffer(int a)/ / i=0; while(!feof(file_pointer) & i=97 & c=65 & c=48 & c=57) return true; else return false; ;int searchforkeywords()/ for(int x=0;x32;x+) if(strcmp(token
7、,keywordsx)=0) return 0;/ return 1;int searchfortoken()/ int i=0; while(i=c-1) if(strcmp(token,token_listi)=0) appear=i; return 0; / i+; word_num+; return 1;int searchfornum()/ int i=0; while(i=d-1) if(strcmp(token,numberi)=0) num_location=i; return 0; / i+; word_num+; return 1;void inserttokenlist(
8、)/ strcpy(token_listc+,token);void insertnumlist()/ strcpy(numberd+,token);main() printf(輸入源文件的路徑:n); scanf(%s,file_name); file_pointer=fopen(file_name,r); if(file_pointer=null) printf(無法查找到文件,發(fā)生錯(cuò)誤!n);/ charac=fgetc(file_pointer); fillbuffer(0);/ while(c!=0) readchar();/ ignorespace();/ switch(c) /
9、case 65: case 66: case 67: case 68: case 69: case 70: case 71: case 72: case 73: case 74: case 75: case 76: case 77: case 78: case 79: case 80: case 81: case 82: case 83: case 84: case 85: case 86: case 87: case 88: case 89: case 90: case 97: case 98: case 99: case 100: case 101: case 102: case 103:
10、 case 104: case 105: case 106: case 107: case 108: case 109: case 110: case 111: case 112: case 113: case 114: case 115: case 116: case 117: case 118: case 119: case 120: case 121: case 122: case _:/ while(alphabet()|digit()|c=_)/ link();/ readchar();/ / tokenj=0;/ j=0;/ pointer-;/ if(searchforkeywo
11、rds()=1)/ if(searchfortoken()=1)/ inserttokenlist();/ printf(n,c-1); else printf(n,appear); / else printf(n,token);/ break; case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: num=(c-48); link(); readchar(); while(digit() link(); num=num*10+(c-48);/ readchar();
12、if(c=.) link(); readchar(); while(digit() link(); num=num+(c-48)*pow(0.1,z+);/ readchar(); tokenj=0;/ j=0;/ pointer-; if(searchfornum()=1)/ insertnumlist();/ printf(n,num,d-1); else printf(n,num,num_location); / break; case +: readchar(); if(c=) printf(n); else if(c=+) printf(n); else pointer-; prin
13、tf(n); break; case -: readchar(); if(c=-) printf(); else if(c=) printf(n); else pointer-; printf(n); break; case *: printf(n);break; case /: readchar(); if(c=*) readchar(); next_charac=buffpointer; while(c!=* & next_charac!=/) readchar(); next_charac=buffpointer; readchar(); printf(n); else if(c=/)
14、readchar(); while(c!=n) readchar(); printf(n); else pointer-; printf(n); break; case %: printf(n);break; case (: printf(n);break; case ): printf(n);break; case : printf(n);break; case : printf(n);break; case : printf(n);break; case : printf(n);break; case : printf(n);break; case ;: printf(n);break;
15、case ,: printf(n);break; case .: printf(n);break; case ?: printf(n);break; case : printf(n);break; case #: printf(n);break; case : readchar(); if(c=) printf(=, 大于等于 n); else pointer-; printf(, 大于 n); break; case : readchar(); if(c=) printf( n); else pointer-; printf( n); break; case =: readchar(); if(c=) printf(n); else pointer-; printf(n); break; case &: readchar(); if(c=&) printf(n); else pointer-; printf(n); break; case |: readchar()
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 水旱輪作稻茬田粘重土壤旋耕減粘脫附機(jī)理及裝置研究
- 基于屬性加密和區(qū)塊鏈的訪問控制研究及應(yīng)用
- 俄漢雙語者訪問心理詞庫的機(jī)制和影響因素研究
- Inconel625合金絲材組織性能及其加工工藝研究
- 纖維蛋白原行業(yè)行業(yè)發(fā)展趨勢(shì)及投資戰(zhàn)略研究分析報(bào)告
- 2025年中國非插電式混合動(dòng)力乘用車行業(yè)發(fā)展監(jiān)測(cè)及投資戰(zhàn)略研究報(bào)告
- 2025年中國劇院公共座椅行業(yè)市場深度評(píng)估及投資戰(zhàn)略規(guī)劃報(bào)告
- 電力行業(yè)投資項(xiàng)目成本管理與風(fēng)險(xiǎn)控制
- 管轄異議申請(qǐng)書
- LED廣告行業(yè)市場全景評(píng)估及發(fā)展戰(zhàn)略研究報(bào)告
- 燒掉數(shù)學(xué)書:重新發(fā)明數(shù)學(xué)
- 廈門弘愛醫(yī)院硼中子俘獲治療系統(tǒng)項(xiàng)目環(huán)境影響報(bào)告
- 酒店招標(biāo)文件內(nèi)容
- 員工調(diào)薪申請(qǐng)單模板
- 初中語文短語練習(xí)(附參考答案)
- 大學(xué)課程中的可持續(xù)發(fā)展目標(biāo)(SDGs)教育-第1篇
- 企業(yè)人員測(cè)評(píng)理論與方法
- 圖形創(chuàng)意(高職藝術(shù)設(shè)計(jì)類)PPT完整全套教學(xué)課件
- 環(huán)境空氣顆粒物(PM10、PM2.5)自動(dòng)監(jiān)測(cè)手工比對(duì)核查技術(shù)規(guī)范
- Jane-Eyre簡愛英文課件
- (小學(xué))人教版一年級(jí)下冊(cè)數(shù)學(xué)《認(rèn)識(shí)鐘表》課件
評(píng)論
0/150
提交評(píng)論