版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、詞法分析程序?qū)嶒?yàn)報(bào)告 班級(jí):xxxx211305姓名:戈策學(xué)號(hào):xxxx211221詞法分析程序?qū)嶒?yàn)報(bào)告實(shí)驗(yàn)內(nèi)容設(shè)計(jì)并實(shí)現(xiàn)C語言的詞法分析程序,要求如下:可以識(shí)別C語言編寫的源程序中的每個(gè)單詞符號(hào),并以記號(hào)的形式輸出每個(gè)單詞??梢越y(tǒng)計(jì)并讀取源程序中的注釋。可以統(tǒng)計(jì)源程序中的語句行數(shù)、單詞個(gè)數(shù)和字符個(gè)數(shù),并輸出統(tǒng)計(jì)結(jié)果。其中回車換行不計(jì)入字符個(gè)數(shù),空格不計(jì)算為單詞。檢查源程序中存在的錯(cuò)誤,并可以報(bào)告錯(cuò)誤所在的行列位置。發(fā)現(xiàn)源程序中存在錯(cuò)誤后,進(jìn)行適當(dāng)?shù)幕謴?fù),使詞法分析可以繼續(xù)進(jìn)行,通過一次詞法分析處理,可以檢查并報(bào)告源程序中存在的所有錯(cuò)誤。程序特色全部實(shí)現(xiàn)上述實(shí)驗(yàn)要求標(biāo)識(shí)符規(guī)則符合標(biāo)準(zhǔn)C語言要求
2、,允許以下劃線開頭,并包含下劃線可識(shí)別C語言允許的所有無符號(hào)實(shí)數(shù)形式,包括整數(shù)和浮點(diǎn)數(shù)。其中浮點(diǎn)數(shù)可包含指數(shù)e或E,并遵循C語言允許省略整數(shù)部分(如.5表示0.5)或省略小數(shù)部分(如2.E2表示2.0*102)可識(shí)別幾乎C語言所有標(biāo)點(diǎn)符號(hào)和運(yùn)算符,共計(jì)50個(gè)可正確識(shí)別字符和字符串中的轉(zhuǎn)義字符,如和“abcd”均可識(shí)別為單詞不完整,并指出錯(cuò)誤的行和列可識(shí)別C語言的兩種注釋,/和/*/,其中/*/按照C語言要求允許跨越多行語言說明標(biāo)識(shí)符:以字母或下劃線開頭,后跟字母、數(shù)字或下劃線組成的字符串關(guān)鍵字:C語言所有32個(gè)關(guān)鍵字無符號(hào)數(shù):C語言所允許的所有十進(jìn)制實(shí)數(shù)形式,包括整數(shù)和浮點(diǎn)數(shù)。其中浮點(diǎn)數(shù)包括C
3、語言允許的省略整數(shù)部分(如.5表示0.5)和省略小數(shù)部分(如2.E2表示2.0*102)算數(shù)運(yùn)算符:+、+、-、-、*、/、%賦值運(yùn)算符:=、+=、-=、*=、/=、%=、&=輸入形式命令行界面,指定C語言源文件作為輸入輸出形式所有標(biāo)識(shí)符寫入指定的符號(hào)表文件分離出一個(gè)單詞后,對(duì)識(shí)別出的記號(hào)以二元式的形式輸出到用戶指定文件,其形式為記號(hào)屬性記號(hào)屬性IDkeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeynumarith-oparith-oparith-opa
4、rith-oparith-oparith-oparith-op符號(hào)表序號(hào)autodoubleintstructbreakelselongswitchcaseenumregistertypedefcharexternreturnunionconstfloatshortunsignedcontinueforsignedvoiddefaultgotosizeofvolatiledoifstaticwhile實(shí)數(shù)值+-*/%asgn-opasgn-opasgn-opasgn-opasgn-opasgn-opasgn-opasgn-opasgn-oprel-oprel-oprel-oprel-oprel
5、-oprel-oplog-oplog-oplog-opbit-opbit-opbit-opbit-opbit-opbit-opothersothersothersothersothersothersothersothers others othersothers others others others charstringcomments=+=-=*=/=string token; /字符串,存放當(dāng)前單詞string:iterator forward; /字符串buffer的迭代器,向前掃描字符核心算法識(shí)別無符號(hào)數(shù)的有限狀態(tài)自動(dòng)機(jī)ss0s23starts1s4s5s6Errordigite/
6、Edigitdigitothers.digitothersdigitdigitdigit+/-/* 由上圖構(gòu)造相應(yīng)的無符號(hào)數(shù)識(shí)別子程序 */void get_digits() while(forward != buffer.end() & notEnd) switch(state) case 1: if(*forward = .) state = 23; else if(*forward = E|e) state = 4; else if(isdigit(*forward) state = 1; else notEnd = fasle; break; case 23: if(*forward
7、= E|e) state = 4; else if(isdigit(*forward) state =23; else notEnd = false; break; case 4: if(*forward = +|-) state = 6; else if(isdigit(*forward) state = 5; else Error;notEnd = false; break; case 5: if(isdigit(*forward) state = 5; else notEnd = false; break; case 6: if(isdigit(*forward) state = 5;
8、elseError;notEnd = false; break; default:break; /* 讀取一個(gè)字符后,如果是語言所定義的單詞符號(hào)的開始字符,則轉(zhuǎn)移到相應(yīng)的識(shí)別識(shí)別過程,否則進(jìn)入錯(cuò)誤處理狀態(tài) */int main() init; open infile, outfile, tablefile; while (true) if (getline(infile, buffer, n) = true) +line; cnt_char += column; column = 0;forward = buffer.begin(); else cnt_char += column; writ
9、e table to tablefile; close infile, outfile, tablefile; output to console; get_nbc; switch (C) case a.z,A.Z,_: token.append; while(digit | letter | _) token.append; if(iskey) table_insert(); outfile : token.append; +forward; if(*forward = =) token.append; outfile rel-op; else if(*forward = *(forward
10、-1) token.append; outfile bit-op; else outfile rel-op; break; case =: token.append; +forward; if(*forward = =) outfile rel-op; else outfile asgn-op; break; case !: token.append; +forward; if(*forward = =) outfile rel-op; else outfile log-op; break; case : token.append; +forward; while(*forward != )
11、token.append; +forward; if(*(forward-1) = ) token.append; +forward; if(forward = buffer.end() Error; else outfile string; break; case : token.append; +forward; while(*forward != ) token.append; +forward; if(*(forward-1) = ) token.append; +forward; if(forward = buffer.end() Error; else outfile char; break; case .: token.append; +forward; if(isdigit) get_digits(); outfile num; break
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年智能建筑設(shè)備的導(dǎo)入與推廣策略
- 急性腸炎的護(hù)理措施
- 公司治理第4版 課件 第2章公司治理基礎(chǔ)
- 現(xiàn)場(chǎng)急救 課件 01.掌握現(xiàn)場(chǎng)急救的理念和原則 -10.兒童與嬰兒心肺復(fù)蘇技術(shù)
- 2-19兩級(jí)放大電路動(dòng)態(tài)仿真
- 導(dǎo)管室護(hù)理應(yīng)急預(yù)案與演練
- 2026年電氣傳動(dòng)系統(tǒng)中的反饋控制策略
- 2026年綠色建筑技術(shù)的國(guó)際經(jīng)驗(yàn)
- 2026年智能監(jiān)控在路基建設(shè)中的應(yīng)用實(shí)例
- 賀家李課件教學(xué)課件
- 人教版(PEP)六年級(jí)英語上冊(cè)復(fù)習(xí)知識(shí)點(diǎn)大全
- JJG 633-2024氣體容積式流量計(jì)
- 咨詢服務(wù)風(fēng)險(xiǎn)管理策略-洞察及研究
- 船舶閥件基礎(chǔ)知識(shí)培訓(xùn)課件
- 立體停車設(shè)備質(zhì)量控制管理文件范例
- 教學(xué)能力大賽備賽講解
- 六年級(jí)語文閱讀理解之托物言志(知識(shí)梳理技法點(diǎn)撥例文分析)(含答案)
- DB31∕T 1564-2025 企業(yè)實(shí)驗(yàn)室危險(xiǎn)化學(xué)品安全管理規(guī)范
- 維修基金管理辦法新疆
- 企業(yè)安全生產(chǎn)培訓(xùn)檔案
- 工程機(jī)械安全操作規(guī)程
評(píng)論
0/150
提交評(píng)論