詞法分析程序實驗報告_第1頁
詞法分析程序實驗報告_第2頁
詞法分析程序實驗報告_第3頁
詞法分析程序實驗報告_第4頁
詞法分析程序實驗報告_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、精選優(yōu)質文檔-傾情為你奉上精選優(yōu)質文檔-傾情為你奉上專心-專注-專業(yè)專心-專注-專業(yè)精選優(yōu)質文檔-傾情為你奉上專心-專注-專業(yè)詞法分析程序實驗報告實驗內容設計并實現(xiàn)C語言的詞法分析程序,要求如下:可以識別C語言編寫的源程序中的每個單詞符號,并以記號的形式輸出每個單詞。可以統(tǒng)計并讀取源程序中的注釋??梢越y(tǒng)計源程序中的語句行數(shù)、單詞個數(shù)和字符個數(shù),并輸出統(tǒng)計結果。其中回車換行不計入字符個數(shù),空格不計算為單詞。檢查源程序中存在的錯誤,并可以報告錯誤所在的行列位置。發(fā)現(xiàn)源程序中存在錯誤后,進行適當?shù)幕謴?,使詞法分析可以繼續(xù)進行,通過一次詞法分析處理,可以檢查并報告源程序中存在的所有錯誤。程序特色全部實

2、現(xiàn)上述實驗要求標識符規(guī)則符合標準C語言要求,允許以下劃線開頭,并包含下劃線可識別C語言允許的所有無符號實數(shù)形式,包括整數(shù)和浮點數(shù)。其中浮點數(shù)可包含指數(shù)e或E,并遵循C語言允許省略整數(shù)部分(如.5表示0.5)或省略小數(shù)部分(如2.E2表示2.0*102)可識別幾乎C語言所有標點符號和運算符,共計50個可正確識別字符和字符串中的轉義字符,如和“abcd”均可識別為單詞不完整,并指出錯誤的行和列可識別C語言的兩種注釋,/和/*/,其中/*/按照C語言要求允許跨越多行語言說明標識符:以字母或下劃線開頭,后跟字母、數(shù)字或下劃線組成的字符串關鍵字:C語言所有32個關鍵字無符號數(shù):C語言所允許的所有十進制實

3、數(shù)形式,包括整數(shù)和浮點數(shù)。其中浮點數(shù)包括C語言允許的省略整數(shù)部分(如.5表示0.5)和省略小數(shù)部分(如2.E2表示2.0*102)算數(shù)運算符:+、+、-、-、*、/、%賦值運算符:=、+=、-=、*=、/=、%=、&=、|=、=關系運算符:、=、!=邏輯運算符:&、|、!位運算符:&、|、其他符號:-、.、#、(、)、?、:、,、;、字符:以單引號開始,以單引號結束,不能跨越多行字符串:以雙引號開始,以雙引號結束,不能跨越多行注釋:以/開始到行末,或者以/*開始,以*/結束,可以跨越多行運行環(huán)境CodeBlocks-13.12 with GCC compiler from TDM-GCC (4

4、.7.1, 32 bit)輸入形式命令行界面,指定C語言源文件作為輸入輸出形式所有標識符寫入指定的符號表文件分離出一個單詞后,對識別出的記號以二元式的形式輸出到用戶指定文件,其形式為記號屬性記號屬性IDkeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeynumarith-oparith-oparith-oparith-oparith-oparith-oparith-op符號表序號autodoubleintstructbreakelselongswitchc

5、aseenumregistertypedefcharexternreturnunionconstfloatshortunsignedcontinueforsignedvoiddefaultgotosizeofvolatiledoifstaticwhile實數(shù)值+-*/%asgn-opasgn-opasgn-opasgn-opasgn-opasgn-opasgn-opasgn-opasgn-oprel-oprel-oprel-oprel-oprel-oprel-oplog-oplog-oplog-opbit-opbit-opbit-opbit-opbit-opbit-opothersothers

6、othersothersothersothersothersothers others othersothers others others others charstringcomments=+=-=*=/=%=&=|=!=&|!&|-.#()?:,;字符字面量字符串字面量-主要數(shù)據(jù)結構vector table; /字符串向量,臨時存儲符號表set keywords; /字符串集合,存放所有關鍵字string buffer; /輸入緩沖區(qū),存放文件的一行string token; /字符串,存放當前單詞string:iterator forward; /字符串buffer的迭代器,向前掃描字

7、符核心算法識別無符號數(shù)的有限狀態(tài)自動機s0s23starts1s4s5s6Errordigite/Edigitdigitothers.digitothersdigitdigitdigit+/-/* 由上圖構造相應的無符號數(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;

8、else notEnd = fasle; break; case 23: if(*forward = 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

9、; break; case 6: if(isdigit(*forward) state = 5; elseError;notEnd = false; break; default:break; /* 讀取一個字符后,如果是語言所定義的單詞符號的開始字符,則轉移到相應的識別識別過程,否則進入錯誤處理狀態(tài) */int main() init; open infile, outfile, tablefile; while (true) if (getline(infile, buffer, n) = true) +line; cnt_char += column; column = 0;forwar

10、d = buffer.begin(); else cnt_char += column; write 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 key; else outfile ID; break; case 0.9: token.append;

11、 state = 1; get_digits(); state = 0; outfile num; break; case +,-,*,%: token.append; +forward; if(*forward = =) outfile asgn-op; else outfile arith-op; break; case /: token.append; +forward; if(*forward = =) outfile asgn-op; else if(*forward = /) while(forward != buffer.end() read comments; outfile

12、comments; else if(*forward = *) if(get_comments() outfile comments; else close infile, outfile, tablefile; else outfile arith-op; break; case &,|,: token.append; +forward; if(*forward = =) outfile asgn-op; else if(*forward = *(forward-1) outfile log-op; else outfile bit-op; break; case : token.appen

13、d; +forward; outfile bit-op; break; case : token.append; +forward; if(*forward = =) token.append; outfile rel-op; else if(*forward = *(forward-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; ca

14、se !: token.append; +forward; if(*forward = =) outfile rel-op; else outfile log-op; break; case : token.append; +forward; while(*forward != ) 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; +

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論