



版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、.詞法分析程序設(shè)計(jì)一問(wèn)題描述1. 可以識(shí)別出用 C 語(yǔ)言編寫(xiě)的源程序中的每個(gè)單詞符號(hào),并以記號(hào)的形式輸出每個(gè)單詞符號(hào)。2. 可以并識(shí)別讀取源程序中的注釋。3. 可以統(tǒng)計(jì)源程序中的語(yǔ)句行數(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)行,通過(guò)一次詞法分析處理,可以檢查并報(bào)告源程序中存在的所有錯(cuò)誤。二算法思想編寫(xiě)一個(gè)詞法分析程序, 它從左到右逐個(gè)字符的對(duì)源程序進(jìn)行掃描, 產(chǎn)生一個(gè)個(gè)的單詞形成記號(hào)流文件輸出。其中,具體子問(wèn)題有:( 1)源程序文件讀入緩沖區(qū)中(注
2、意要?jiǎng)h除空格和無(wú)用符號(hào))( 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í)別出來(lái)并打印輸出結(jié)果( 4)對(duì)于源文件中出現(xiàn)的數(shù)字常量,不但能按要求加入常量表中,還進(jìn)行了字符型到float型數(shù)值的轉(zhuǎn)換,便于后續(xù)程序操作處理。(4)盡量精簡(jiǎn)整合各種情況,使算法復(fù)雜度降低,精簡(jiǎn)易讀。三、實(shí)驗(yàn)程序設(shè)計(jì)說(shuō)明1. 主要函數(shù)說(shuō)明void readChar();/讀字符過(guò)程,每調(diào)用一次,從輸入緩沖區(qū)讀一
3、個(gè)字符,并把它放入變量C中,且向前掃描指針pointer指向下一個(gè)字符void ignoreSpace();/每次調(diào)用時(shí),檢查C 中的字符是否為空字符,若是,則反復(fù)調(diào)用該過(guò)程,直到 C 進(jìn)入一個(gè)非空字符為止void link();/把 C 中的字符與 token 中的字符串連接起來(lái)bool alphabet();/布爾函數(shù),判斷 C 中的字符是否為字母,若是則返回true ,否則返回 falsebool digit();/布爾函數(shù),判斷 C 中的字符是否為數(shù)字,若是則返回true ,否則返回 falseint searchForKeywords();/查關(guān)鍵字表,若此函數(shù)的返回值為0,則表示
4、token 中的字符串是標(biāo)識(shí)符,否則為關(guān)鍵字int searchForToken();/查符號(hào)表,若此函數(shù)的返回值為0,則表示 token 中的字符串是新出現(xiàn)的,否則為已出現(xiàn)過(guò)的標(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<stdio.h>#include<
5、;stdlib.h>#include<string.h>#include<math.h>int pointer=0; /int i=0, j =0, c=0, appear , d=0, num_location; /int row_num=0, letter_num=0, word_num=0;int z=1;char C =' ' /char token 30; /char buff 4095; /char token_list 200 30; /char number 200 10; /char next_charac, charac , f
6、ile_name 20;charkeywords 32 10= "auto", "break", "case" , "char", "const", "continue", "default", "do" , "double" , "else", "enum" , "extern", "float", "for", &
7、quot;goto", "if", "int", "long", "register", "return", "short", "signed", "sizeo", "fstatic", "struct", "switch", "typedef", "union", "unsigned" , "vo
8、id", "volatile", "while"double num ;FILE * file_pointer;void fillBuffer( int a) /i=0;while(!feof ( file_pointer)&& i <2048 ) /buff a+i = charac ;if(charac != ' ') /if(charac ='n')row_num+; /elseletter_num+;charac=fgetc( file_pointer);i+;.if(feof (
9、file_pointer)buff a+i = '0'void readChar() /C=buff pointer;if(pointer=1023) /fillBuffer( 1024); /pointer+; /elseif(pointer=2047) /fillBuffer( 0); /pointer=0; /elsepointer+;void ignoreSpace() /if(C=' '| C='n'| C='t')C=buff pointer;if(pointer=1023) /fillBuffer( 1024); /
10、pointer+; /elseif(pointer=2047) /fillBuffer( 0); /pointer=0; /elsepointer+;ignoreSpace();void link() /token j += C;bool alphabet() /.if(C>=97 && C <=122)|(C>=65 && C<=90)returntrue;elsereturnfalse;bool digit() /if(C>=48 && C <=57)returntrue;elsereturnfalse;i
11、nt searchForKeywords() /for(int x=0; x<32; x+)if(strcmp ( token , keywords x )= 0)return0;/return1 ;int searchForToken() /int i=0;while(i <=c - 1)if(strcmp ( token , token_list i )= 0)appear=i ;return0 ; /i +;word_num+;return1 ;int searchForNum() /int i=0;while(i <=d- 1)if(strcmp ( token ,
12、number i )= 0)num_location=i ;.return0 ; /i +;word_num+;return1 ;void insertTokenList() /strcpy( token_list c+, token );void insertNumList() /strcpy( number d+, token );main ()printf( " 輸入源文件的路徑:n");scanf( "%s", file_name);file_pointer=fopen ( file_name, "r" );if(file_p
13、ointer=NULL) printf( " 無(wú)法查找到文件,發(fā)生錯(cuò)誤!n" ); /charac=fgetc( file_pointer);fillBuffer( 0); /while(C!= '0')readChar();/ignoreSpace();/switch(C)/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
14、:case 81 :case82: 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 : case 104: case 105 : case 106 : case 107 : case 108 : case 109 : case 110 : case 111 : case 112 : case 113 : case 114 : case115: ca
15、se 116 : case 117 : case 118 : case 119 : case 120 : case 121 : case 122 : case'_' : / ?while(alphabet()|digit()|C='_') /link();/readChar(); /.token j = '0' /j=0; /pointer-;/if(searchForKeywords()= 1) /if(searchForToken ()= 1) /insertTokenList();/printf( "< ID, %d >
16、;n", c- 1);elseprintf( "< ID, %d >n", appear ); /elseprintf( "< %s,?>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()
17、;if(C='.')link();readChar();while(digit()link();num=num+( C- 48)* pow( 0.1 , z +); /readChar();token j = '0' /j=0; /pointer-;if(searchForNum ()= 1) /insertNumList();/printf( "< %f, %d >n", num, d- 1);else.printf( "< %f, %d >n", num, num_location); / br
18、eak;case '+':readChar();if(C='=' )printf( "< +=,賦值運(yùn)算符>n");elseif(C='+')printf( "< +,自加 >n" );elsepointer-;printf( "< +,加號(hào) >n");break;case '-':readChar();if(C='-')printf( "< -,自減 >" );elseif(C='
19、;=')printf( "< -= ,賦值運(yùn)算符>n" );elsepointer-;printf( "< -,減號(hào) >n");break;case '*':printf( "< *,乘號(hào) >n");break;case '/':readChar();if(C='*')readChar();next_charac=buff pointer;while(C!= '*'&& next_charac!= '/&
20、#39;)readChar();next_charac=buff pointer;readChar();printf( "< /*,多行注釋>n");elseif(C='/').readChar();while(C!= 'n')readChar();printf( "< /,單行注釋 >n");elsepointer-;printf("< /,除號(hào) >n");break;case '%':printf("<%,取模 >n"
21、; );break;case'(':printf("<(,左小括號(hào) >n");break;case')':printf("< ),右小括號(hào) >n");break;case'':printf("< ,左中括號(hào) >n");break;case'':printf("< ,右中括號(hào) >n");break;case'':printf("< , -左大括號(hào) >n");b
22、reak;case'':printf("< ,右大括號(hào) >n");break;case':':printf("< :,冒號(hào) >n");break;case'':printf("< ;,分號(hào) >n");break;case',':printf("< ,逗號(hào) >n");break;case'.':printf("< .,句號(hào) >n");break;case
23、39;?':printf("< ?,問(wèn)號(hào) >n");break;case'"':printf( "< "",引號(hào) >n" );break;case '#':printf("< #,井號(hào) >n");break;case '>':readChar();if(C='=')printf( "< >=,大于等于 >n");.elsepointer-;printf( "< >,大于 >n");break;case '<':readChar();if(C='=' )printf( "< <=,小于等于>n");elsepoint
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 遼寧省2024年高考語(yǔ)文模擬試卷及答案2
- 2025年運(yùn)城市稅務(wù)系統(tǒng)遴選面試真題附詳細(xì)解析含答案
- 財(cái)務(wù)會(huì)計(jì)崗位勞動(dòng)合同書(shū)(含合規(guī)風(fēng)險(xiǎn)管理)
- 餐飲行業(yè)簽單掛賬財(cái)務(wù)管理合同
- 餐飲業(yè)廚師長(zhǎng)職務(wù)委托合同范本
- 車輛贈(zèng)與及車輛租賃服務(wù)合作協(xié)議
- 國(guó)際組織駐華代表處招聘代理合同
- 高科技產(chǎn)業(yè)園區(qū)廠房配套小賣部租賃管理規(guī)范協(xié)議
- 建筑工地安全文明施工協(xié)議
- 大一研學(xué)旅行考試題及答案
- 鎖骨下動(dòng)脈竊血綜合征 (2)PPT
- 2022年人教八級(jí)下英語(yǔ)單詞英譯漢
- 大班社會(huì)《愛(ài)發(fā)脾氣的菲菲》課件
- 公路工程項(xiàng)目代建制管理辦法(218頁(yè)編制詳細(xì))
- NX空間系統(tǒng)熱簡(jiǎn)介復(fù)習(xí)課程
- 納米氧化鋅實(shí)驗(yàn)報(bào)告(共9頁(yè))
- 自粘聚合物改性瀝青防水卷材施工
- 青少年科技創(chuàng)新PPT
- 美麗鄉(xiāng)村片區(qū)內(nèi)監(jiān)理規(guī)劃范本
- 曉明煤礦副井過(guò)卷緩沖裝置安裝措施
- 燃料油 MSDS(化學(xué)品安全技術(shù)說(shuō)明書(shū))
評(píng)論
0/150
提交評(píng)論