免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
.一、 实验目的设计、编制并调试一个语法分析程序,加深对语法分析原理的理解。二实验要求要求语法分析器的输入是单词串(含词的字符串形式、在源文件中的起止位置、词的类别),输出是源程序中各句子的单词起止编号、句子的语法树。三实验内容以下不同语法分析器中任选一个:1. 递归下降分析器。可分解为:文法输入及解析、消除左递归、提取左公共因子、产生式匹配。2. LL(1)分析器。可分解为:文法输入及解析、分析表构造(含SELECT集求解)、主控程序、语法树展示。3. 算符优先文法分析器。可分解为:文法输入及解析、分析表构造、主控程序、语法树展示。4. LR(1)分析器。可分解为:文法输入及解析、分析表构造(含项目及项目簇集求解)、主控程序、语法树展示。四、实验步骤给定的文法GEE-TEE-+TE | T-FTT-*F T| F-(E) | i采用递归下降分析法编写语法分析程序及LL(1)语法分析法编写语法分析程序。实验代码:#include #include #include #define N 100 int seekProd(int stackTop,int inputstrTop); /char inputstr10=i*i+i#; char inputstr20; char stack10=; typedef struct production char leftChar; char rightChars4; char allChars8; Prod; Prod productions8; void init(); int stackPush(int *top, Prod prod); int matching(int *top, char *inputstr); int main() int len;/输入串的长度 int stackTop=1; int inputstrTop=0; int i; char *z=#; int index=0; init();/产生式初始化 stack0=#; stackstackTop=E; printf(请输入字符串:); gets(inputstr); len=strlen(inputstr); inputstrlen=#; while( stackTop=0 ) / printf(%d,%dn,stackTop,inputstrTop); printf(第%2d步:,+index); printf(当前栈:%-8s,stack); printf(输入字符串:%8s,inputstr); /根据栈定元素和字符串首字母 if(matching(&stackTop,inputstr) printf(n); else i=seekProd(stackTop,inputstrTop); stackPush(&stackTop,productionsi);/压栈 printf(进行下一步所用的产生式:%sn,productionsi.allChars); if(stackTop+1=0) printf(分析成功!n); return 0; /搜索分析表 int seekProd(int stackTop,int inputstrTop) / printf(stackstackTop=%cn,stackstackTop); if(stackstackTop=E) if(inputstrinputstrTop=i) return 0; else if(inputstrinputstrTop=() return 0; else return -1; else if(stackstackTop=X) if(inputstrinputstrTop=+) return 1; else if(inputstrinputstrTop=) return 2; else if(inputstrinputstrTop=#) return 2; else return -1; else if(stackstackTop=T) if(inputstrinputstrTop=i) return 3; else if(inputstrinputstrTop=() return 3; else return -1; else if(stackstackTop=Y) if(inputstrinputstrTop=+) return 5; else if(inputstrinputstrTop=*) return 4; else if(inputstrinputstrTop=) return 5; else if(inputstrinputstrTop=#) return 5; else return -1; else if(stackstackTop=F) if(inputstrinputstrTop=i) return 7; else if(inputstrinputstrTop=() return 6; else return -1; else printf(错误!); return -1; void init() productions0.leftChar=E;strcpy(productions0.rightChars,TX); strcpy(productions0.allChars,E-TX); productions1.leftChar=X;strcpy(productions1.rightChars,+TX);strcpy(productions1.allChars,E-+TX); productions2.leftChar=X;strcpy(productions2.rightChars,); strcpy(productions2.allChars,X-); productions3.leftChar=T;strcpy(productions3.rightChars,FY); strcpy(productions3.allChars,T-FY); productions4.leftChar=Y;strcpy(productions4.rightChars,*FY);strcpy(productions4.allChars,Y-*FY); productions5.leftChar=Y;strcpy(productions5.rightChars,); strcpy(productions5.allChars,Y-); productions6.leftChar=F;strcpy(productions6.rightChars,(E);strcpy(productions6.allChars,F-(E); productions7.leftChar=F;strcpy(productions7.rightChars,i); strcpy(productions7.allChars,F-i); int stackPush(int *top, Prod prod) int len; int i; char *c=; len=strlen(prod.rightChars); if(!strcmp(prod.rightChars,c) stack(*top)=0; else for(i=len-1;i=0;i-) stack(*top)+ = prod.rightCharsi; -(*top); return 0; int matching(int *top, char *inputstr) int len; int i; if(stack(*top)=inputstr0) stack(*top)-=0; len=strlen(inputstr); for(i=0;ilen-1;i+) inputstri=inputstri+1; inputstri=0; return 1; else return 0; 实验截图:五、实验总结通过这次的实验,我深
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年台风灾害应急防御规范测试题及答案
- 2026年浙江省嵊州市高三生物上册期末考试模拟卷带答案
- 2026年湖南省耒阳市高一化学上册期末考试模拟检测卷附答案【A卷】
- 2026年浙江省余姚市高三生物下册期末考试模拟检测卷(含答案)
- 2026年广东省英德市高一化学上册期末考试模拟考试卷及参考答案(预热题)
- 2026年福建省福鼎市高三生物上册期末考试考试卷(含答案)
- 2026年辽宁省盖州市高一化学上册期末考试检测卷附参考答案【达标题】
- 2026年黑龙江省抚远市高三生物下册期末考试模拟卷(含答案)
- 2026年吉林省龙井市高三历史上册期末考试模拟卷含答案【B卷】
- 2026年辽宁省瓦房店市高三生物上册期末考试考试卷及参考答案
- 2026年6月浙江高考选考技术试卷真题(含答案详解)
- 血液肿瘤相关肠梗阻护理专家共识(2026年版)
- 2026年电机学考试复习题库及答案详解
- QMS新版标准讲解新旧比照
- 第5课《中国人民站起来了》第二课时(教学课件)
- 大学专业课学习资料-文学理论知识点
- T∕CHCIA 014-2023 液体香氛安全要求
- 殡仪馆遗体处理突发公共卫生事件应急演练脚本
- 财务调账审批制度
- 人像摄影培训课件
- 2026中国电信广西公司春季校园招聘笔试模拟试题及答案解析
评论
0/150
提交评论