您的位置:首页 > 其它

编译原理(递归下降分析程序)

2009-11-11 19:10 375 查看
编译原理 (递归下降分析程序)
#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)正确
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: