《編譯原理》實(shí)驗(yàn)2_第1頁(yè)
《編譯原理》實(shí)驗(yàn)2_第2頁(yè)
《編譯原理》實(shí)驗(yàn)2_第3頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、編譯原理(實(shí)驗(yàn)部分)實(shí)驗(yàn) 2_PL0 詞法分析一、實(shí)驗(yàn)?zāi)康募由詈挽柟虒?duì)于詞法分析的了解和掌握;初步認(rèn)識(shí)PL/0 語(yǔ)言的基礎(chǔ)和簡(jiǎn)單的程序編寫;通過(guò)本實(shí)驗(yàn)?zāi)軌虺醪降牧私夂驼莆粘绦蛟~法分析的整個(gè)過(guò)程;提高自己上機(jī)和編程過(guò)程中處理具體問(wèn)題的能力。二、實(shí)驗(yàn)設(shè)備1、PC 兼容機(jī)一臺(tái);操作系統(tǒng)為WindowsWindowsXP。2、 Visual C+ 6.0或以上版本,Windows 2000 或以上版本,匯編工具(在 Software子目錄下)。三、實(shí)驗(yàn)原理PL/O 語(yǔ)言的編譯程序,是用高級(jí)語(yǔ)言PASCAL語(yǔ)言書寫的。整個(gè)編譯過(guò)程是由一些嵌套及并列的過(guò)程或函數(shù)完成。詞法分析程序是獨(dú)立的過(guò)程GETSYM完

2、成,供語(yǔ)法分析讀單詞時(shí)使用。四、實(shí)驗(yàn)步驟閱讀所給出的詞法分析程序(pl0_lexical.c),搞懂程序中每一個(gè)變量的含義,以及每一個(gè)過(guò)程的作用,并在該過(guò)程中進(jìn)行中文注釋;閱讀完程序后,畫出各過(guò)程的流程圖;給出的程序包含兩處輸入錯(cuò)誤,利用所給的pl/0源程序(test.pl0)對(duì)程序進(jìn)行調(diào)試,使其能正確對(duì)所給文件進(jìn)行分析并能夠解釋運(yùn)行;在閱讀懂所給出的詞法分析程序后,將你對(duì)詞法分析的理解寫在實(shí)驗(yàn)報(bào)告上。實(shí)驗(yàn)代碼#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. 本站所有資源如無(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論