编译原理实验的报告材料

上传人:无*** 文档编号:86636040 上传时间:2022-05-08 格式:DOC 页数:31 大小:122.50KB
收藏 版权申诉 举报 下载
编译原理实验的报告材料_第1页
第1页 / 共31页
编译原理实验的报告材料_第2页
第2页 / 共31页
编译原理实验的报告材料_第3页
第3页 / 共31页
资源描述:

《编译原理实验的报告材料》由会员分享,可在线阅读,更多相关《编译原理实验的报告材料(31页珍藏版)》请在装配图网上搜索。

1、word某某科技大学计算机科学与技术学院编译方法实验报告 某某 : 尹 绮 莉 班级: 计 算 机1003 指导教师:邱 谊 敏实验一 词法分析器设计 2012-4-17【实验目的】1熟悉词法分析的根本原理,词法分析的过程以与词法分析中要注意的问题。2复习高级语言,进一步加强用高级语言来解决实际问题的能力。3通过完成词法分析程序,了解词法分析的过程。【实验内容】用C语言编写一个PL/0词法分析器,为语法语义分析提供单词,使之能把输入的字符串形式的源程序分割成一个个单词符号传递给语法语义分析,并把分析结果根本字,运算符,标识符,常数以与界符输出。启动调用Init初始化从文件中读取一个非空白字符c

2、h文件是否为空 N判断chCh为标识符?Ch为关键字? N Y YCh为数字? Y将值传入相应组中 Default 完毕/对关键字等实现初始化void init() int i;for(i=0;i=255;i+)ssymi=nul;ssym+=plus;ssym-=minus;ssym*=times;ssym/=slash;ssym(=lparen;ssym)=rparen;ssym=eql;ssym,=ma;ssym.=period;ssym#=neq;ssym;=semicolon;strcpy(&(word00),begin);strcpy(&(word10),call);strcpy(

3、&(word20),const);strcpy(&(word30),do);strcpy(&(word40),end);strcpy(&(word50),if);strcpy(&(word60),odd);strcpy(&(word70),procedure);strcpy(&(word80),read);strcpy(&(word90),program);strcpy(&(word100),var);strcpy(&(word110),while);strcpy(&(word120),write);strcpy(&(word130),then);wsym0=beginsym;wsym1=ca

4、llsym;wsym2=constsym;wsym3=dosym;wsym4=endsym;wsym5=ifsym;wsym6=oddsym;wsym7=procsym;wsym8=readsym;wsym9=programsym;wsym10=varsym;wsym11=whilesym;wsym12=writesym;wsym13=thensym;/读取源文件并判断单词类别int Key(char *word) for(int i=0;i num;i+) if(!strcmp(KeyWordsi,word) return 1; return 0;/确定是否为关键字 int sign(cha

5、r word) for(int i=0;inum0;i+) if(opei=word) return 1; return 0;/确定是否为运算符int tap(char word) for(int i=0;inum1;i+) if(edgesigni=word) return 1; return 0;/确定 是否为分隔符符void Takenwrite2(char word) /4为运算符,5为分隔符 if(sign(word) printf(4,%c)n,word); if(tap(word) printf(5,%c)n,word); void Takenwrite1(char *word,

6、int Len) /3为字符或者数字,2为标识符,1为关键字 int a; if(Key(word) printf(1,%s)n,word); else if( word0= & wordLen-1=) /字符常量 printf(3,%s)n,word); else if(a=atoi(word)/字符串转化为数字 printf(3,%d)n,a); else if(word0!=0) printf(2,%s)n,word); int Taken() FILE *fp; char temp10,ch=1,temp1; char filename20; int i,j; printf(请输入文件

7、名); scanf(%s,filename); if(fp=fopen(filename,r)=NULL) printf(文件打开失败); return 0 ; while(ch!=EOF) j=0; i=0; ch=fgetc(fp); while(!tap(ch)&!sign(ch)&ch!= &ch!=n&ch!=EOF) tempi=ch; tempi+1=0; ch=fgetc(fp); i+; Takenwrite1(temp,i); temp0=0;/去除字串 temp1=ch; Takenwrite2(temp1); temp1=0;/去除字符 fclose(fp);retur

8、n 1;运行结果实验二 LL1语法分析程序设计2012-4-24【实验目的】1熟悉判断LL1文法的方法与对某一输入串的分析过程。2学会构造表达式文法的预测分析表。【实验内容】编写一个语法分析程序,对于给定的输入串,能够判断识别该串是否为给定文法的句型。【实验局部代码】struct pRNode /*产生式右部结构*/ int rCursor; /*右部序号*/struct pRNode *next;struct pNode /*产生式结点结构*/ int lCursor; /*左部符号序号*/ int rLength; /*右部长度*/ /*注当rLength = 1 时,rCursor =

9、-1为空产生式*/ struct pRNode *rHead; /*右部结点头指针*/;struct collectNode /*集合元素结点结构*/ int nVt; /*在终结符集中的下标*/ struct collectNode *next;/*返回Vn在Vn表中的位置+100、Vt在Vt表中的位置,-1表示未找到*/int IndexCh(char ch) int n; n = 0; while(ch != Vnn & 0 != Vnn)n+; if(0 != Vnn) return 100 + n; n = 0; while(ch != Vtn & 0 != Vtn) n+; if(

10、0 != Vtn)return n; return -1;/*输出Vn或Vt的内容*/void ShowChArray(char* collect) int k = 0; while(0 != collectk) printf( %c , collectk+);printf(n); Vnn = #; /*以#标志完毕用于判断长度是否合法*/ k = n; /*k用于记录n以便改Vnn=0*/ if(# != ch) if( # != (ch = getchar() while(# != (ch = getchar(); printf(n符号数目超过限制!n); inErr = 1; conti

11、nue; /*输入非终结符*/ /*输入终结符*/void InputVt() int inErr = 1; int n,k; char ch; while(inErr) printf(n请输入所有的终结符,注意:); printf(以#号完毕:n); ch = ; n = 0; /*初始化数组*/ while(n MaxVtNum) Vtn+ = 0; n = 0; while(# != ch) & (n MaxVtNum) if( != ch & n != ch & -1 = IndexCh(ch) Vtn+ = ch; vtNum+; ch = getchar(); Vtn = #; /

12、*以#标志完毕*/ k = n; /*k用于记录n以便改Vtn=0*/ if(# != ch) if( # != (ch = getchar() while(# != (ch = getchar() printf(n符号数目超过限制!n); inErr = 1; continue; /*正确性确认,正确如此,执行下下面,否如此重新输入*/ Vtk = 0; ShowChArray(Vt); ch = ; while(y != ch & n != ch) if(n != ch) printf(输入正确确认?(y/n):); scanf(%c, &ch); if(n = ch) printf(录入

13、错误重新输入!n); inErr = 1; else inErr = 0; /*产生式输入*/void InputP() char ch; int i = 0, n,num; printf(请输入文法产生式的个数:); scanf(%d, &num); PNum = num; getchar(); /*消除回车符*/ printf(n请输入文法的%d个产生式,并以回车分隔每个产生式:, num); printf(n); while(i num) printf(第%d个:, i); /*初始化*/ for(n =0; n MaxPLength; n+) buffern = 0; /*输入产生式串

14、*/ ch = ; n = 0; while(n != (ch = getchar() & n rCursor = IndexCh(buffer3); pt-next = NULL; Pi.rHead = pt; n = 4; while(0 != buffern) qt = (pRNode*)malloc(sizeof(pRNode); qt-rCursor = IndexCh(buffern); qt-next = NULL; pt-next = qt; pt = qt; n+; Pi.rLength = n - 3; i+; /*调试时使用*/ else printf(输入符号含非法在成

15、分,请重新输入!n); /*判断产生式正确性*/bool CheckP(char * st) int n; if(100 IndexCh(st0) return false; if(- != st1) return false; if( != st2) return false; for(n = 3; 0 != stn; n +) if(-1 = IndexCh(stn) return false; return true;/*=first & follow=*/*计算first集,U-xx.*/void First(int U) int i,j; for(i = 0; i PNum; i+)

16、 if(Pi.lCursor = U) struct pRNode* pt; pt = Pi.rHead; j = 0; while(j pt-rCursor) /*注:此处因编程出错,使空产生式时 rlength同样是1,故此处同样可处理空产生式*/ AddFirst(U, pt-rCursor); break; else if(NULL = firstpt-rCursor - 100) First(pt-rCursor); AddFirst(U, pt-rCursor); if(!HaveEmpty(pt-rCursor) break; else pt = pt-next; j+; if(

17、j = Pi.rLength) /*当产生式右部都能推出空时*/ AddFirst(U, -1); /*参加first集*/void AddFirst(int U, int nCh) /*当数值小于100时nCh为Vt*/*当处理非终结符时,AddFirst不添加空项(-1)*/ struct collectNode *pt, *qt; int ch; /*用于处理Vn*/ pt = NULL; qt = NULL; if(nCh nVt = nCh) break; else qt = pt; pt = pt-next; if(NULL = pt) pt = (struct collectNo

18、de *)malloc(sizeof(struct collectNode); pt-nVt = nCh; pt-next = NULL; if(NULL = firstU - 100) firstU - 100 = pt; else qt-next = pt; /*qt指向first集的最后一个元素*/ pt = pt-next; else pt = firstnCh - 100; while(NULL != pt) ch = pt-nVt; if(-1 != ch) AddFirst(U, ch); pt = pt-next; /*判断first集中是否有空(-1)*/bool HaveE

19、mpty(int nVn) if(nVn nVt) return true; pt = pt-next; return false;/*计算follow集,例:U-xVy,U-xV.(注:初始符必含#-1)*/void Follow(int V) int i; struct pRNode *pt ; if(100 = V) /*当为初始符时*/ AddFollow(V, -1, 0 ); for(i = 0; i rCursor != V) pt = pt-next; if(NULL != pt) pt = pt-next; /*V右侧的符号*/ if(NULL = pt) /*当V后为空时V

20、-xV,将左符的follow集并入V的follow集中*/ if(NULL = followPi.lCursor - 100 & Pi.lCursor != V) Follow(Pi.lCursor); AddFollow(V, Pi.lCursor, 0); else /*不为空时V-xVy,(注意:y-),调用AddFollow参加Vt或y的first集*/ while(NULL != pt & HaveEmpty(pt-rCursor) AddFollow(V, pt-rCursor, 1); /*y的前缀中有空时,加如first集*/ pt = pt-next; if(NULL = p

21、t) /*当后面的字符可以推出空时*/ if(NULL = followPi.lCursor - 100 & Pi.lCursor != V) Follow(Pi.lCursor); AddFollow(V, Pi.lCursor, 0); else /*发现不为空的字符时*/ AddFollow(V, pt-rCursor, 1); /*当数值小于100时nCh为Vt*/*#用-1表示,kind用于区分是并入符号的first集,还是follow集kind = 0表参加follow集,kind = 1参加first集*/void AddFollow(int V, int nCh, int ki

22、nd) struct collectNode *pt, *qt; int ch; /*用于处理Vn*/ pt = NULL; qt = NULL; if(nCh nVt = nCh) break; else qt = pt; pt = pt-next; if(NULL = pt) pt = (struct collectNode *)malloc(sizeof(struct collectNode); pt-nVt = nCh; pt-next = NULL; if(NULL = followV - 100) followV - 100 = pt; else qt-next = pt; /*q

23、t指向follow集的最后一个元素*/ pt = pt-next; else /*为非终结符时,要区分是加first还是follow*/ if(0 = kind) pt = follownCh - 100; while(NULL != pt) ch = pt-nVt; AddFollow(V, ch, 0); pt = pt-next; else pt = firstnCh - 100; while(NULL != pt) ch = pt-nVt; if(-1 != ch) AddFollow(V, ch, 1); pt = pt-next; /*输出first或follow集*/void S

24、howCollect(struct collectNode *collect) int i; struct collectNode *pt; i = 0; while(NULL != collecti) pt = collecti; printf(n%c:t, Vni); while(NULL != pt) if(-1 != pt-nVt) printf( %c, Vtpt-nVt); else printf( #); pt = pt-next; i+; printf(n);/*计算first和follow*/void FirstFollow() int i; i = 0; while(0 !

25、= Vni) if(NULL = firsti) First(100 + i); i+; i = 0; while(0 != Vni) if(NULL = followi) Follow(100 + i); i+; /*=构造预测分析表,例:U:xyz=*/void CreateAT() int i; struct pRNode *pt; struct collectNode *ct; for(i = 0; i rCursor) /*处理非终结符,当为终结符时,定含空为假跳出*/ ct = firstpt-rCursor - 100; while(NULL != ct)if(-1 != ct-

26、nVt) analyseTablePi.lCursor - 100ct-nVt = i; ct = ct-next; pt = pt-next; if(NULL = pt) /*NULL = pt,说明xyz-,用到follow中的符号*/ ct = followPi.lCursor - 100; while(NULL != ct) if(-1 != ct-nVt) analyseTablePi.lCursor - 100ct-nVt = i; else /*当含有#号时*/ analyseTablePi.lCursor - 100vtNum = i; ct = ct-next; else i

27、f(100 rCursor) /*不含空的非终结符*/ ct = firstpt-rCursor - 100; while(NULL != ct) analyseTablePi.lCursor - 100ct-nVt = i; ct = ct-next; else /*终结符或者空*/ if(-1 = pt-rCursor) /*-1为空产生式时*/ ct = followPi.lCursor - 100; while(NULL != ct) if(-1 != ct-nVt) analyseTablePi.lCursor - 100ct-nVt = i; else /*当含有#号时*/ ana

28、lyseTablePi.lCursor - 100vtNum = i; ct = ct-next; else /*为终结符*/ analyseTablePi.lCursor - 100pt-rCursor = i; /*输出分析表*/void ShowAT() int i,j; printf(构造预测分析表如下:n); printf(t|t); for(i = 0; i vtNum; i+) printf(%ct, Vti);printf(#tn); printf(- - -t|- - -t); for(i = 0; i = vtNum; i+) printf(- - -t); printf(

29、n); for(i = 0; i vnNum; i+) printf(%ct|t, Vni); for(j = 0; j analyseStacktopAnalyse) /*当为终结符时*/ if(analyseStacktopAnalyse = IndexCh(stcurrent) /*匹配出栈,指示器后移*/ Pop(); current+; step+; printf(%dt, step); ShowStack(); printf(tt%ctt出栈、后移n, stcurrent); else printf(%c-%c不匹配!, analyseStacktopAnalyse, stcurr

30、ent); printf(此串不是此文法的句子!n); return; else /*当为非终结符时*/ r = analyseTableanalyseStacktopAnalyse - 100IndexCh(stcurrent); if(-1 != r) Push(r); /*产生式右部代替左部,指示器不移动*/ step+; printf(%dt, step); ShowStack(); printf(tt%ctt%dn, stcurrent, r); else printf(无可用产生式,此串不是此文法的句子!n); return; if(# = stcurrent) if(0 = to

31、pAnalyse & # = stcurrent) step+; printf(%dt, step); ShowStack(); printf(tt%ctt分析成功!n, stcurrent); printf(%s是给定文法的句子!n, st); else while(topAnalyse 0) if(100 analyseStacktopAnalyse) /*当为终结符时*/ printf(无可用产生式,此串不是此文法的句子!n); return; else r = analyseTableanalyseStacktopAnalyse - 100vtNum; if(-1 != r) Push

32、(r); /*产生式右部代替左部,指示器不移动*/ step+; printf(%dt, step); ShowStack(); if(0 = topAnalyse & # = stcurrent) printf(tt%ctt分析成功!n, stcurrent); printf(%s是给定文法的句子!n, st); else printf(tt%ctt%dn, stcurrent, r); else printf(无可用产生式,此串不是此文法的句子!n); return; /*初始化栈与符号串*/void InitStack() int i; /*分析栈的初始化*/ for(i = 0; i

33、MaxStLength; i+) sti = 0; analyseStack0 = -1; /*#(-1)入栈*/ analyseStack1 = 100; /*初始符入栈*/ topAnalyse = 1;/*显示符号栈中内容*/void ShowStack() int i; for(i = 0; i = topAnalyse; i+) if(100 rCursor) /*为空产生式时*/ return; topAnalyse += Pr.rLength; for(i = 0; i rCursor;/*逆序入栈*/ pt = pt-next; /*循环未完时pt为空,如此说明rLength记录等出错*/【实验步骤和要求】实验截图:31 / 31

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!