77范文网 - 专业文章范例文档资料分享平台

语法分析器实验报告2

来源:网络收集 时间:2018-12-06 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

一、实验目的

设计、编制并调试一个语法分析程序,加深对语法分析原理的理解。 二实验要求

要求语法分析器的输入是单词串(含词的字符串形式、在源文件中的起止位置、词的类别),输出是源程序中各句子的单词起止编号、句子的语法树。 三实验内容

以下不同语法分析器中任选一个:

1. 递归下降分析器。可分解为:文法输入及解析、消除左递归、提取左公

共因子、产生式匹配。

2. LL(1)分析器。可分解为:文法输入及解析、分析表构造(含SELECT集

求解)、主控程序、语法树展示。

3. 算符优先文法分析器。可分解为:文法输入及解析、分析表构造、主控

程序、语法树展示。

4. LR(1)分析器。可分解为:文法输入及解析、分析表构造(含项目及项目

簇集求解)、主控程序、语法树展示。

四、实验步骤

给定的文法G[E] E->TE’ E’->+TE’ | ε T->FT’ T’->*F T’| ε F->(E) | i

采用递归下降分析法编写语法分析程序及LL(1)语法分析法编写语法分析程序。 实验代码:

#include #include #include #define N 100

int seekProd(int stackTop,int inputstrTop); //char inputstr[10]=\ char inputstr[20]; char stack[10]=\

typedef struct production{ char leftChar;

char rightChars[4]; char allChars[8]; }Prod;

Prod productions[8]; 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();//产生式初始化

stack[0]='#';

stack[stackTop]='E';

printf(\请输入字符串:\ gets(inputstr);

len=strlen(inputstr); inputstr[len]='#'; while( stackTop>=0 ) {

// printf(\ printf(\第-步:\ printf(\当前栈:%-8s\

printf(\输入字符串:%8s\ //根据栈定元素和字符串首字母

if(matching(&stackTop,inputstr)){ printf(\ }else{

i=seekProd(stackTop,inputstrTop);

stackPush(&stackTop,productions[i]);//压栈

printf(\进行下一步所用的产生式:%s\\n\ } }

if(stackTop+1==0) {

printf(\分析成功!\\n\ }

return 0; }

//搜索分析表

int seekProd(int stackTop,int inputstrTop) {

// printf(\ if(stack[stackTop]=='E'){

if(inputstr[inputstrTop]=='i') {

return 0;

}else if(inputstr[inputstrTop]=='('){ return 0; }else{

return -1; }

}else if(stack[stackTop]=='X'){ if(inputstr[inputstrTop]=='+') {

return 1;

}else if(inputstr[inputstrTop]==')'){ return 2;

}else if(inputstr[inputstrTop]=='#'){ return 2; }else{

return -1; }

}else if(stack[stackTop]=='T'){ if(inputstr[inputstrTop]=='i') {

return 3;

}else if(inputstr[inputstrTop]=='('){ return 3; }else{

return -1; }

}else if(stack[stackTop]=='Y'){ if(inputstr[inputstrTop]=='+') {

return 5;

}else if(inputstr[inputstrTop]=='*'){ return 4;

}else if(inputstr[inputstrTop]==')'){ return 5;

}else if(inputstr[inputstrTop]=='#'){ return 5; }else{

return -1; }

}else if(stack[stackTop]=='F'){ if(inputstr[inputstrTop]=='i'){ return 7;

}else if(inputstr[inputstrTop]=='('){ return 6; }else{

return -1; } }else{

printf(\错误!\ }

return -1; }

void init() {

productions[0].leftChar='E';strcpy(productions[0].rightChars,\strcpy(productions[0].allChars,\

productions[1].leftChar='X';strcpy(productions[1].rightChars,\ars,\

productions[2].leftChar='X';strcpy(productions[2].rightChars,\ strcpy(productions[2].allChars,\>ε\

productions[3].leftChar='T';strcpy(productions[3].rightChars,\strcpy(productions[3].allChars,\

productions[4].leftChar='Y';strcpy(productions[4].rightChars,\rs,\

productions[5].leftChar='Y';strcpy(productions[5].rightChars,\ strcpy(productions[5].allChars,\>ε\

productions[6].leftChar='F';strcpy(productions[6].rightChars,\,\

productions[7].leftChar='F';strcpy(productions[7].rightChars,\ strcpy(productions[7].allChars,\ }

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.rightChars[i]; } }

--(*top); return 0; }

int matching(int *top, char *inputstr) {

int len; int i;

if(stack[(*top)]==inputstr[0]) {

stack[(*top)--]='\\0'; len=strlen(inputstr); for(i=0;i

inputstr[i]=inputstr[i+1]; }

inputstr[i]='\\0'; return 1; }else {

return 0; } }

实验截图:

五、实验总结

通过这次的实验,我深入了解了语法分析器和LL(1)文法预测分析法设计和实现,增强了我的自学能力和独立思考能力,也让我对程序设计有了更大的兴趣,自己通过查找资料、复习课本、编程调试,写实验报告等环节,进一步掌握了以前学到的知识,并且还对编译原理应用有了更深入的认识与掌握。在完成这个程序后,真的很开心,也了使我解到编译原理的魅力所在,激发了我要解决更多更难问题的决心。

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库语法分析器实验报告2在线全文阅读。

语法分析器实验报告2.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/346529.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: