


版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
1、編譯原理(實驗部分)實驗 2_PL0 詞法分析一、實驗目的加深和鞏固對于詞法分析的了解和掌握;初步認識PL/0 語言的基礎和簡單的程序編寫;通過本實驗能夠初步的了解和掌握程序詞法分析的整個過程;提高自己上機和編程過程中處理具體問題的能力。二、實驗設備1、PC 兼容機一臺;操作系統(tǒng)為WindowsWindowsXP。2、 Visual C+ 6.0或以上版本,Windows 2000 或以上版本,匯編工具(在 Software子目錄下)。三、實驗原理PL/O 語言的編譯程序,是用高級語言PASCAL語言書寫的。整個編譯過程是由一些嵌套及并列的過程或函數(shù)完成。詞法分析程序是獨立的過程GETSYM完
2、成,供語法分析讀單詞時使用。四、實驗步驟閱讀所給出的詞法分析程序(pl0_lexical.c),搞懂程序中每一個變量的含義,以及每一個過程的作用,并在該過程中進行中文注釋;閱讀完程序后,畫出各過程的流程圖;給出的程序包含兩處輸入錯誤,利用所給的pl/0源程序(test.pl0)對程序進行調(diào)試,使其能正確對所給文件進行分析并能夠解釋運行;在閱讀懂所給出的詞法分析程序后,將你對詞法分析的理解寫在實驗報告上。實驗代碼#include<stdio.h>main() printf ("my name is 08061118 yuchaofeng ");主程序:#inclu
3、de <stdio.h>#include <ctype.h>#include <alloc.h>#include <stdlib.h>#include <string.h>#define NULL 0FILE *fp;char cbuffer;char *key8="DO","BEGIN","ELSE","END","IF","THEN","VAR","WHILE"char *
4、border6=",","",":=",".","(",")"char *arithmetic4="+","-","*","/"char *relation6="<","<=","=",">",">=","<>"char *consts20;c
5、har *label20;int constnum=0,labelnum=0;int search(char searchchar,int wordtype)int i=0;switch (wordtype) case 1:for (i=0;i<=7;i+)if (strcmp(keyi,searchchar)=0)return(i+1);case 2:for (i=0;i<=5;i+)if (strcmp(borderi,searchchar)=0)return(i+1);return(0);case 3:for (i=0;i<=3;i+) if (strcmp(arith
6、metici,searchchar)=0)return(i+1);return(0);case 4:for (i=0;i<=5;i+) if (strcmp(relationi,searchchar)=0)return(i+1);return(0);case 5:for (i=0;i<=constnum;i+)if (strcmp(constsi,searchchar)=0)return(i+1);constsi-1=(char *)malloc(sizeof(searchchar);strcpy(constsi-1,searchchar);constnum+;return(i);
7、case 6:for (i=0;i<=labelnum;i+) if (strcmp(labeli,searchchar)=0)return(i+1);labeli-1=(char *)malloc(sizeof(searchchar);strcpy(labeli-1,searchchar);labelnum+;return(i);char alphaprocess(char buffer)int atype;int i=-1;char alphatp20;while (isalpha(buffer)|(isdigit(buffer) alphatp+i=buffer; buffer=f
8、getc(fp);alphatpi+1='0'if (atype=search(alphatp,1)printf("%s (1,%d)n",alphatp,atype-1);elseatype=search(alphatp,6);printf("%s (6,%d)n",alphatp,atype-1);return(buffer);char digitprocess(char buffer)int i=-1;char digittp20;int dtype;while (isdigit(buffer) digittp+i=buffer;
9、buffer=fgetc(fp);digittpi+1='0'dtype=search(digittp,5);printf("%s (5,%d)n",digittp,dtype-1);return(buffer);char otherprocess(char buffer)int i=-1;char othertp20;int otype,otypetp;othertp0=buffer;othertp1='0'if (otype=search(othertp,3)printf("%s (3,%d)n",othertp,ot
10、ype-1);buffer=fgetc(fp);goto out;if (otype=search(othertp,4)buffer=fgetc(fp);othertp1=buffer;othertp2='0'if (otypetp=search(othertp,4)printf("%s (4,%d)n",othertp,otypetp-1);goto out;elseothertp1='0'printf("%s (4,%d)n",othertp,otype-1);goto out;if (buffer=':
11、9;) buffer=fgetc(fp); if (buffer='=')printf(":= (2,2)n");buffer=fgetc(fp);goto out;elseif (otype=search(othertp,2) printf("%s (2,%d)n",othertp,otype-1); buffer=fgetc(fp);goto out;if (buffer!='n')&&(buffer!=' ')printf("%c error,not a wordn",buffer);buffer=fgetc(fp);out:return(buffer);void main()int i;for (i=0;i<=20;i+)labeli=NULL;constsi=NULL;if (fp=fopen("skh.c","r")=NULL)printf("error");elsecbuffer = fgetc(fp);wh
溫馨提示
- 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屆高一下化學期末監(jiān)測試題含解析
- 2025屆安徽定遠示范高中高二下化學期末統(tǒng)考試題含解析
- 山東省鄒城市第一中學2025年化學高二下期末質(zhì)量跟蹤監(jiān)視模擬試題含解析
- 檔案收費存放管理辦法
- 酒體色澤穩(wěn)定性研究-洞察及研究
- 殘聯(lián)物資采購管理辦法
- 出差補貼管理制度
- 河池工業(yè)園區(qū)管理辦法
- 養(yǎng)老機構(gòu)管理辦法講座
- 體驗中心運營管理辦法
- 村振興產(chǎn)業(yè)融合發(fā)展示范區(qū)建設項目運營管理方案
- 2025年中考物理解題方法復習專題10力學壓軸題的常見解法
- 慈利一中選拔考試題及答案
- 殘疾人護理實操考試題及答案
- DB54∕T 0296-2023 文物古建筑消防安全評估規(guī)范
- 醫(yī)共體醫(yī)保管理工作制度
- 注塑模具保養(yǎng)維修培訓
- 商城周年慶活動方案方案
- 2025新課標教師培訓
- 檢驗科實習生培訓
- 幼兒教育畢業(yè)論文8000字
評論
0/150
提交評論