语法分析器实验报告2Word格式文档下载.docx
《语法分析器实验报告2Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《语法分析器实验报告2Word格式文档下载.docx(6页珍藏版)》请在冰点文库上搜索。
T->
FT’
T’->
*FT’|ε
F->
(E)|i
采用递归下降分析法编写语法分析程序及LL
(1)语法分析法编写语法分析程序。
实验代码:
#include<
stdio.h>
#include<
string.h>
malloc.h>
#defineN100
intseekProd(intstackTop,intinputstrTop);
//charinputstr[10]="
i*i+i#"
;
charinputstr[20];
charstack[10]="
"
typedefstructproduction{charleftChar;
charrightChars[4];
charallChars[8];
}Prod;
Prodproductions[8];
voidinit();
intstackPush(int*top,Prodprod);
intmatching(int*top,char*inputstr);
intmain()
{
intlen;
//输入串的长度intstackTop=1;
intinputstrTop=0;
inti;
char*z="
#"
intindex=0;
init();
//产生式初始化
stack[0]='
#'
stack[stackTop]='
E'
printf("
请输入字符串:
);
gets(inputstr);
len=strlen(inputstr);
inputstr[len]='
while(stackTop>
=0)
// printf("
%d,%d\n"
stackTop,inputstrTop);
第%2d步:
++index);
printf("
当前栈:
%-8s"
stack);
输入字符串:
%8s"
inputstr);
//根据栈定元素和字符串首字母
if(matching(&
stackTop,inputstr)){printf("
\n"
}else{
i=seekProd(stackTop,inputstrTop);
stackPush(&
stackTop,productions[i]);
/压/栈
进行下一步所用的产生式:
%s\n"
productions[i].allChars);
}
if(stackTop+1==0)
分析成功!
return0;
6
精品文档交流
//搜索分析表
intseekProd(intstackTop,intinputstrTop)
stack[stackTop]=%c\n"
stack[stackTop]);
if(stack[stackTop]=='
){
if(inputstr[inputstrTop]=='
i'
)
}elseif(inputstr[inputstrTop]=='
('
){return0;
return-1;
}elseif(stack[stackTop]=='
X'
){if(inputstr[inputstrTop]=='
+'
return1;
)'
){return2;
T'
return3;
){return3;
Y'
return5;
*'
){return4;
){return5;
F'
return7;
){return6;
错误!
voidinit()
productions[0].leftChar='
strcpy(productions[0].rightChars,"
TX"
strcpy(productions[0].allChars,"
productions[1].leftChar='
strcpy(productions[1].rightChars,"
+TX"
strcpy(productions[1].allChars,"
productions[2].leftChar='
strcpy(productions[2].rightChars,"
ε"
strcpy(productions[2].allChars,"
X->
productions[3].leftChar='
strcpy(productions[3].rightChars,"
FY"
strcpy(productions[3].allChars,"
productions[4].leftChar='
strcpy(productions[4].rightChars,"
*FY"
strcpy(productions[4].allChars,"
Y->
productions[5].leftChar='
strcpy(productions[5].rightChars,"
strcpy(productions[5].allChars,"
productions[6].leftChar='
strcpy(productions[6].rightChars,"
(E)"
strcpy(productions[6].allChars
"
productions[7].leftChar='
strcpy(productions[7].rightChars,"
i"
strcpy(productions[7].allChars,"
intstackPush(int*top,Prodprod)
char*c="
len=strlen(prod.rightChars);
if(!
strcmp(prod.rightChars,c))
stack[(*top)]='
\0'
for(i=len-1;
i>
=0;
i--)
stack[(*top)++]=prod.rightChars[i];
--(*top);
return0;
intmatching(int*top,char*inputstr)
if(stack[(*top)]==inputstr[0])
stack[(*top)--]='
len=strlen(inputstr);
for(i=0;
i<
len-1;
i++)
inputstr[i]=inputstr[i+1];
inputstr[i]='
return1;
}else
实验截图:
五、实验总结
通过这次的实验,我深入了解了语法分析器和LL
(1)文法预测分析法设计和实现,增强了我的自学能力和独立思考能力,也让我对程序设计有了更大的兴趣,自己通过查找资料、复习课本、编程调试,写实验报告等环节,进一步掌握了以前学到的知识,并且还对编译原理应用有了更深入的认识与掌握。
在完成这个程序后,真的很开心,也了使我解到编译原理的魅
力所在,激发了我要解决更多更难问题的决心【。
下载本文档,可以自由复制内容或自由编辑修改内容,更多精彩文章,期待你的好评和关注,我将一如既往为您服务】