编译原理实验递归子程序法.doc
- 文档编号:386743
- 上传时间:2022-10-09
- 格式:DOC
- 页数:9
- 大小:48KB
编译原理实验递归子程序法.doc
《编译原理实验递归子程序法.doc》由会员分享,可在线阅读,更多相关《编译原理实验递归子程序法.doc(9页珍藏版)》请在冰豆网上搜索。
递归子程序法:
#include"stdio.h"
#include"stdlib.h"
voiddisplay();
charread();//读取字符函数
voidwrite();
charlookhead;
voidE();
voidT();
voidG();
voidS();
voidF();
voidmatch(charch);
voiderror();//出错处理
charstring[10];
inttop;
intmain()
{
display();
write();
lookhead=read();
E();
puts(string);
printf("为合法字符串!
\n");
system("pause");
return0;
}
voidE()
{
printf("E->TG\n");
T();
G();
}
voidG()
{
if(lookhead=='+')
{
printf("G->+TG\n");
match('+');
T();
G();
}
elseif(lookhead=='-')
{
printf("G->-TG\n");
match('-');
T();
G();
}
else
{
printf("G->ε\n");
}
}
voidT()
{
printf("T->FS\n");
F();
S();
}
voidS()
{
if(lookhead=='*')
{
printf("s->*FS\n");
match('*');
F();
S();
}
elseif(lookhead=='/')
{
printf("S->/FS\n");
match('/');
}
else
{
printf("S->ε\n");
}
}
voidF()
{
if(lookhead=='(')
{
printf("F->(E)\n");
match('(');
E();
match(')');
}
elseif(lookhead=='i')
{
match('i');
}
elseerror();
}
voidmatch(charch)
{
lookhead=read();
}
voidwrite()
{
charcha;
top=0;
printf("请输入待分析字符串:
\n");
do
{
scanf("%c",&cha);
string[top]=cha;
top++;
}
while(cha!
='#');
top=0;
}
charread()
{
charcha;
cha=string[top];
top++;
returncha;
}
voiderror()
{
puts(string);
printf("为非法字符串!
\n");
system("pause");
exit(0);
}
voiddisplay()
{
printf("班级:
10级计本一班\n");
printf("学号:
1004011026\n");
printf("姓名:
王晓龙\n");
}
LL
(1)分析方法:
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
charS,X,a;//S文法的开始符号,X当前栈顶符号的工作单元,a当前输入符号的工作单元
charstring[10];//存放输入的句型
intcount=0; //计数器
intstep=0;//记录步骤
charM[16][6];//存放产生式
intrecord;//记录当前找到的M中产生式的下标
charstack[10];//栈
inttop;
charVt[9]="i+-*/()";//终结符号
inti;
voiderror();//出错函数
voidinit();//初始化预测分析表
intjudge(charx,chara);
voidanalyse();
voidmain()
{
init();//初始化预测分析表
inti;
intmark1=0;
charch;
top=-1;
printf("请输入要分析的字符串:
\n");
string[0]='#';
i=1;
do
{
scanf("%c",&ch);
string[i]=ch;
i++;
}while(ch!
='#');
printf("步骤\t\t分析栈\t\t剩余字符串\n");
top++;
stack[top]=string[0];
count++;
top++;
stack[top]='E';
step++;
printf("%d\t\t",step);
for(inti=0;i<=top;i++)
printf("%c",stack[i]);
printf("\t\t");
i=1;
while(string[i]!
='\0')
{
printf("%c",string[i]);
i++;
}
printf("\n");
analyse();
system("pasue");
}
voiderror()
{
printf("分析失败!
\n");
system("pause");
exit(0);
}
voidinit()//初始化预测分析表-已经产生式右部反转过来
{
strcpy(M[0],"EiGT");
strcpy(M[1],"E(GT");
strcpy(M[2],"G+GT+");
strcpy(M[3],"G)$");
strcpy(M[4],"G#$");
strcpy(M[5],"G-GT-");
strcpy(M[6],"TiSF");
strcpy(M[7],"T(SF");
strcpy(M[8],"S+$");
strcpy(M[9],"S*SF*");
strcpy(M[10],"S)$");
strcpy(M[11],"S#$");
strcpy(M[12],"S/SF/");
strcpy(M[13],"S-$");
strcpy(M[14],"Fii");
strcpy(M[15],"F()E(");
}
intjudge(charx,charb)
{
for(inti=0;i<16;i++)
{
if(M[i][0]==x&&M[i][1]==b)
{
record=i;
return1;
}
}
return0;
}
voidanalyse()
{
intmark1=0;
a=string[count];
X=stack[top];
top--;
for(inti=0;i<=8;i++)
{
if(X==Vt[i])
{
if(X==a)
{
count++;
}
elseerror();
mark1=1;
break;
}
}
if(mark1==0)
{
if(X=='#')
{
if(X==a)
{
printf("分析成功!
\n");
system("pause");
exit(0);
}
elseerror();
}
if(X!
='#')
{
if(!
judge(X,a))
{
error();
}
else
{
for(inti=2;i<5;i++)
{
if(M[record][i]!
='\0'&&M[record][i]!
='$')
{
top++;
stack[top]=M[record][i];
}
}
}
}
}
step++;
printf("%d\t\t",step);
for(inti=0;i<=top;i++)
{
printf("%c",stack[i]);
}
printf("\t\t");
i=count;
while(string[i]!
='\0')
{
printf("%c",string[i]);
i++;
}
printf("\n");
analyse();
}
算符优先文法:
#include
#include
#include
chardata[9][9]={'','+','-','*','/','(',')','i','#',
'+','>','>','<','<','<','>','<','>',
'-','>','>','<','<','<','>','<','>',
'*','>','>','>','>','<','>','<','>',
'/','>','>','>','>','<','>','<','>',
'(','<','<','<','<','<','=','<','',
')','>','>','>','>','','>','','>',
'i','>','>','>','>','','>','','>',
'#','<','<','<','<','<','','<','='
};
voidanalyse(chara[],intn)
{
inti=0,j=0,k=0,t=0,h=0,g=0;
charch,r;//当前符号
charA[30];//符号栈
A[0]='#';
ch=a[0];
while(ch!
='
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 实验 递归 子程序