编译原理 (递归下降分析程序)
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
int i=0;
string nexttoken;
char ch;
void A();
void A1();
void read()
{
ch= nexttoken[++i];
}
void error()
{
cout<<"不是本文法的句型!!"<<endl;
exit(1);
}
void S()
{
read();
if(ch=='(')
{
read();
A();
if (ch==')') goto L;
else error();
}
else if(ch!='a') error();
else { read();A();
if(ch=='b') goto L;
else error();}
L:read();
return;
}
void A()
{
if(ch== 'e') {read();goto L;}
if(ch!= 'd') error();
S();
L:A1();
return;
}
void A1()
{
L:if(ch=='d'){ read();goto L;}
else if(ch=='b') goto L1;
else if(ch==')') goto L1;
else error();
L1:return;
}
void main()
{
try
{
cout<<"Please input the string: "<<endl;
cin>>nexttoken;
ch=nexttoken[i];
S();
cout<<"为本文法的句型!"<<endl;
}
catch(char)
{
cout<<"输入出错!"<<endl;
}
}
//(aeb)正确