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

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

 
#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)正确
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值