




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
實驗1-4S語言詞法分析器設計PAGEPAGE4實驗1-3S語言詞法分析器設計一、實驗名稱:S語言詞法分析器設計二、實驗目的:通過手工編寫簡化C語言詞法分析器,熟悉并深入理解編譯程序詞法分析器的工作原理。三、實驗內(nèi)容:1.根據(jù)保留字和特殊符號表能區(qū)分出源文件中的保留字、普通標識符和特殊符號,并能進行簡單的錯誤處理。2.設計詞法分析器模塊調(diào)用結(jié)構(gòu)圖和各模塊流程圖。3.程序源代碼。4.程序的執(zhí)行結(jié)果:輸入文件,輸出結(jié)果文件及屏幕信息。四、實驗中出現(xiàn)的問題及解決方法。遇到問題及解決:1、關(guān)于注釋//和除號/。需要區(qū)分,在isanotation函數(shù)中。2、關(guān)于遇到空格時回退,一開始沒有懂,后來經(jīng)過同學講解,明白了如何回退的。3、關(guān)于詞法分析器的思想過程,經(jīng)過老師再三講解,已經(jīng)大致明白,具體步驟,在最后體會部分中。五、程序結(jié)構(gòu)和代碼程序結(jié)構(gòu):代碼:#include<stdio.h>#include<stdlib.h>#include<string>#include<iostream>usingnamespacestd;//關(guān)鍵字表stringkeywords[20]={"include","void","main","int","char","float","double","if","else","then","break","continue","for","do","while","printf","scanf","begin","end","return"};charaa[99999]="";//關(guān)鍵字表初始為空stringid[10000];intpp=0;//常數(shù)表stringnu[10000];intqq=0;//初始化函數(shù)voidinitscanner(){inti=0;FILE*fp;if((fp=fopen("源程序.txt","r"))==NULL)//打開源程序文件{printf("沒有找到此文件!");exit(0);}charch=fgetc(fp);while(ch!=EOF){aa[i]=ch;i++;ch=fgetc(fp);}fclose(fp);}intdecide1(chara)//判斷是否是字母{if((a>='a'&&a<='z')||(a>='A'&&a<='Z'))return1;elsereturn0;}intdecide2(chara)//判斷是否是數(shù)字{if(a>='0'&&a<='9')return1;elsereturn0;}intisalpha(intst)//識別保留字和標識符{charwordbuf[20]="";intn=0;for(;;){wordbuf[n]=aa[st];st++;n++;if((decide2(aa[st])==1)||(decide1(aa[st])==1)||(aa[st]=='_'))wordbuf[n]=aa[st];elsebreak;}intflag=0;for(intk=0;k<20;k++){if(strcmp(keywords[k].c_str(),wordbuf)==0)flag=1;}if(flag==0){intflagg=-1;for(intt=0;t<pp;t++){if(strcmp(id[t].c_str(),wordbuf)==0){flagg=t;}}if(flagg!=-1)printf("(id,%d)",flagg);else{id[pp]=wordbuf;printf("(id,%d)",pp);pp++;}}else{printf("(");for(inti=0;i<n;i++){printf("%c",wordbuf[i]);}printf(",-)");}returnst;}intisnumber(intst)//識別數(shù)字{charnumbuf[20]="";intn=0;}elseif(aa[st]=='='){st++;printf("(/*,-)");}elseprintf("(/,-)");returnst;}intisother(intst)//函數(shù)識別其他特殊字符{switch(aa[st]){case'=':st++;if(aa[st]=='='){st++;printf("(rlop,==)");}elseprintf("(rlop,=)");break;case'+':st++;if(aa[st]=='='){st++;printf("(+=,-)");}elseif(aa[st]=='+'){st++;printf("(++,-)");}elseprintf("(+,-)");break;case'-':st++;if(aa[st]=='='){st++;printf("(-=,-)");}elseif(aa[st]=='-'){st++;printf("(--,-)");}elseprintf("(-,-)");break;case'*':st++;if(aa[st]=='='){st++;printf("(*=,-)");}else printf("(*,-)");break;case'>':st++;if(aa[st]=='='){st++;printf("(rlop,>=)");}elseprintf("(rlop,>)");break;case'<':st++;if(aa[st]=='='){st++;printf("(rlop,<=)");}elseprintf("(rlop,<)");break;case'%':st++;if(aa[st]=='='){st++;printf("(\%=,-)");}elseprintf("(\%,-)");break;case'!':st++;if(aa[st]=='='){st++;printf("(!=,-)");}else printf("(!,錯誤!)");break;case'&':st++;if(aa[st]=='&'){st++;printf("(&&,-)");}elseprintf("(&,錯誤)");break;case'|':st++;if(aa[st]=='|'){st++;printf("(||,-)");}elseprintf("(|,錯誤)");break;case'{':st++;printf("({,-)");break;case'}':st++;printf("(},-)");break;case'(':st++;printf("((,-)");break;case')':st++;printf("(),-)");break;case'[':st++;printf("([,-)");break;case']':st++;printf("(],-)");break;case':':st++;printf("(:,-)");break;case'#':st++;printf("(#,-)");break;case';':st++;printf("(;,-)");break;case'.':st++;printf("(.,-)");break;case',':st++;printf("(,,-)");break;case'':st++;break;case' ':st++;break;case10:st++;printf("\n");break;case34:st++;printf("(\",-)");break;case39:st++;printf("(',-)");break;default:printf("(%c,錯誤)",aa[st]);st++;}returnst;}intlexscan(intst)//根據(jù)讀入的單詞的第一個字符確定調(diào)用不同的單詞識別函數(shù){if(decide1(aa[st])==1)//如果是字母st=isalpha(st);elseif(decide2(aa[st])==1)//如果是數(shù)字st=isnumber(st);elseif(aa[st]=='/')//如果是標識符或關(guān)鍵字st=isanotation(st);elsest=isother(st);//其他特殊字符returnst;}voidscanner()//若文件未結(jié)束,反復調(diào)用lexscan函數(shù)識別單詞{inti=0;while(aa[i]!=NULL)i=lexscan(i);}voidprint(){cout<<endl;cout<<endl<<"關(guān)鍵字、標示符表如下:"<<endl;cout<<"============================================="<<endl;for(inti=0;i<pp;i++)cout<<i<<""<<id[i]<<endl;cout<<"============================================="<<endl;cout<<endl;cout<<"常數(shù)表如下:"<<endl;cout<<"============================================="<<endl;for(intj=0;j<qq;j++)cout<<j<<"
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年吉林省建筑安全員C證考試(專職安全員)題庫及答案
- 2025-2030年中國衣物柔順劑行業(yè)發(fā)展狀況及營銷戰(zhàn)略研究報告
- 2025-2030年中國薺藍油市場發(fā)展現(xiàn)狀規(guī)劃研究報告
- 2025-2030年中國硅酸鋯行業(yè)前景趨勢及發(fā)展規(guī)劃分析報告
- 2025-2030年中國礦物棉市場營運狀況及發(fā)展策略研究報告
- 2025波蘭數(shù)學奧林匹克(第二輪)試題
- 2025遼寧省建筑安全員B證考試題庫
- 合肥幼兒師范高等??茖W?!稘h字文化與創(chuàng)新設計》2023-2024學年第二學期期末試卷
- 河北美術(shù)學院《中小學教學名師論壇》2023-2024學年第二學期期末試卷
- 湖南電氣職業(yè)技術(shù)學院《現(xiàn)代辦公技能訓練A》2023-2024學年第二學期期末試卷
- 《中國人民站起來了》課件+2024-2025學年統(tǒng)編版高中語文選擇性必修上冊
- DB11-T 825-2021綠色建筑評價標準
- 醫(yī)院招聘醫(yī)護人員報名登記表
- 完整解讀2022年《義務教育課程方案》2022年《義務教育課程方案(2022版)》新課標PPT
- 央企最新版員工手冊vvv
- 新生兒科出科考試試卷試題
- 信息化教學設計教案大學語文
- 植物的營養(yǎng)器官:根、莖、葉匯總
- 會議、匯報材料排版格式
- 華為公司產(chǎn)品線獎金分配暫行辦法
- 兒童能力評估量表(PEDI拍迪)
評論
0/150
提交評論