




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
编译原理(实验部分)实验2_PL0词法分析一、实验目的加深和巩固对于词法分析的了解和掌握;初步认识PL/0语言的基础和简单的程序编写;通过本实验能够初步的了解和掌握程序词法分析的整个过程;提高自己上机和编程过程中处理具体问题的能力。二、实验设备1、PC 兼容机一台;操作系统为WindowsWindowsXP。2、Visual C+ 6.0 或以上版本, Windows 2000 或以上版本,汇编工具(在Software 子目录下)。三、实验原理PL/O语言的编译程序,是用高级语言PASCAL语言书写的。整个编译过程是由一些嵌套及并列的过程或函数完成。词法分析程序是独立的过程GETSYM完成,供语法分析读单词时使用。四、实验步骤阅读所给出的词法分析程序(pl0_lexical.c),搞懂程序中每一个变量的含义,以及每一个过程的作用,并在该过程中进行中文注释;阅读完程序后,画出各过程的流程图;给出的程序包含两处输入错误,利用所给的pl/0源程序(test.pl0)对程序进行调试,使其能正确对所给文件进行分析并能够解释运行;在阅读懂所给出的词法分析程序后,将你对词法分析的理解写在实验报告上。实验代码#includemain() printf (my name is 08061118 yuchaofeng );主程序:#include #include #include #include #include #define NULL 0FILE *fp;char cbuffer;char *key8=DO,BEGIN,ELSE,END,IF,THEN,VAR,WHILE;char *border6=,;,:=,.,(,);char *arithmetic4=+,-,*,/;char *relation6=,=,;char *consts20;char *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(arithmetici,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); ; 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=fgetc(fp); ; alphatpi+1=0; if (atype=search(alphatp,1) printf(%s (1,%d)n,alphatp,atype-1); else atype=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; 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,otype-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; else othertp1=0; printf(%s (4,%d)n,othertp,otype-1); goto out; ; if (buffer=:) buffer=fgetc(fp); if (buffer=) printf(:= (2,2)n); buffer=fgetc(fp); goto out; else if (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); else cbuffer = fgetc(fp); while (cbuffer!=EOF) if (isa
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 生活药学考试题及答案
- 中交安全考试试题及答案
- 2025年国家电投集团福建核电招聘考试笔试试题(含答案)
- 北京知识型直播培训课件
- 2025年甘肃烟草公司招聘考试笔试试题(含答案)
- 2025年恩施州鹤峰县县直教育单位选调教师考试笔试试题(含答案)
- 2024年云南省社区《网格员》考前冲刺训练(含答案)
- 干燥综合征及护理试题(含答案)
- 消防工程师模拟题(含答案)
- 卫生院医院感染相关知识考试试题(附答案)
- 物业管理三标体系整合培训纲要
- 2025年新反洗钱知识竞赛题库(附含答案)
- 融媒体中心媒资管理办法
- 2025年一建机电工程管理与实务考试机电工程质量通病防治实战模拟试题库含答案
- 肩袖损伤护理课件
- 高速轮轨噪声主动控制技术-洞察阐释
- 2025至2030肉牛行业发展趋势分析与未来投资战略咨询研究报告
- 输尿管结石病例分析
- 社会组织薪酬管理制度
- 失语症及治疗方法讲课件
- 热射病病人的急救护理
评论
0/150
提交评论