1. 程式人生 > >http://www.cnblogs.com/chenguangpeng/p/6188352.html 遞歸下降

http://www.cnblogs.com/chenguangpeng/p/6188352.html 遞歸下降

sca turn htm www har ring 次數 http %d

#include<stdio.h>
#include<string>
char str[50];
int index=0;
void E(); //E->TX;
void X(); //X->+TX | e
void T(); //T->FY
void Y(); //Y->*FY | e
void F(); //F->(E) | i

int main() /*遞歸分析*/
{
int len;
int m;
printf("請輸入要測試的次數:");
scanf("%d",&m);
while(m--)
{
printf("請輸入字符串(長度<50>):\n");
scanf("%s",str);
len=strlen(str);
//str[len]=‘#‘;
str[len+1]=‘\0‘;
E();
printf("%s為合法符號串!\n",str);
strcpy(str,"");
index=0;
} return 0;
}

void E()
{ T(); X();}
void X()
{
if(str[index]==‘+‘)
{ index++;
T();
X();
} }
void T()
{ F(); Y(); }
void Y()
{ if(str[index]==‘*‘) {
index++;
F();
Y();
} }
void F()
{
if(str[index]==‘i‘)
{ index++; }
else if (str[index]==‘(‘)
{
index++;
E();
if(str[index]==‘)‘)
{ index++; }else
{
printf("\n非法的符號串!\n");
exit (0);
} }
else
{
printf("非法的符號串!\n");
exit(0);
}
}

http://www.cnblogs.com/chenguangpeng/p/6188352.html 遞歸下降