编译原理实验二 语法分析

直接上代码

#include <iostream>
#include <stdlib.h>
using namespace std;
#define STACKSIZE  50
#define STACKINCREMENT   10
char ch;
int i=0;//记录位置
char str[80];

typedef struct
{
  char *base;
  char *top;
  int stacksize;
}SqStack;
int InitStack(SqStack &S)
{
   S.base = (char * )malloc(STACKSIZE * sizeof(char));
   if (!S.base)
   exit(0);
   S.top=S.base;
   S.stacksize=STACKSIZE;
   return 1;
}
int Push(SqStack &S, char e)
{
   if (S.top - S.base >= S.stacksize)
  {
    S.base = (char * )realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(char));
    if (!S.base)
    exit(0);
    S.top = S.base + S.stacksize;
    S.stacksize += STACKINCREMENT;
  }
  (S.top)++;
  *(S.top) = e;


  return 1;
}
int Pop(SqStack &S, char &e)
{
   if (S.top == S.base)
   return 0;
   e = *(S.top);
   S.top--;

   return 1;

}
void recursionT(void);
void recursionF(void);
void error()                        //出错处理函数
{
	cout<<"该符号串不是文法的句子"<<endl;
	exit(0);
}
int isLetter(char ch)//判断字符是否为字母
{
    if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))
        return 1;
    else return 0;
}
int isDigit(char ch)//判断字符是否为数字
{
    if(ch>='0'&&ch<='9')return 1;
    else return 0;
}
void recursionE(void)                     //子程序E
{
	cout<<"进入E子程序"<<endl;
	recursionT();
	if(ch=='+'||ch=='-')
	{
	 i++;
	 ch=str[i];
	recursionE();
	}
}
void recursionT(void)                 //子程序T
{
	cout<<"进入T子程序"<<endl;
	recursionF();
	if(ch=='*'||ch=='/')
	{
		i++;
		ch=str[i];
		recursionT();
	}
}
void recursionF(void)                     //子程序F
{
    cout<<"进入F子程序"<<endl;
	if(ch=='(')
	{

		i++;
		ch=str[i];
		recursionE();
		if(ch==')')
		{
			 i++;
			 ch=str[i];
		}
		else
		{
			error();

		}
	}
	else if(isDigit(ch)||isLetter(ch))
	{

		i++;
		ch=str[i];
	}
	else
	{
		error();

	}
}
void recursion()//递归子程序法主程序
{

	ch=str[0];
	while(ch!='#')               //进行递归下降分析
	{
		recursionE();

	}
if(ch=='#')
		cout<<"该符号串是文法的句子"<<endl;
}

void LL1Analysis(SqStack &S, char *ch, char c)
{
      char e;
      Push(S, '#');
      Push(S, 'E');
      while (!((*(S.top) == '#') && (c == '#')))
    {
        Pop(S, e);//取栈顶元素
        if ((e == 'E') && ((isLetter(c)||isDigit(c))|| (c == '(')))
      {
          cout<<"选择E->TA产生式逆序压栈"<<endl;
        Push(S , 'A');
        Push(S , 'T');
      }
       else if ((e == 'A') && (c == '+'||c=='-'))
      {
        cout<<"选择A->w1TA产生式逆序压栈"<<endl;
        Push(S , 'A');
        Push(S , 'T');
         cout<<"匹配w1"<<endl;
        ch++;
        c = *ch;
      }
      else if ((e == 'A') && ((c == ')')||(c == '#')));
      else if ((e == 'T') && ((isLetter(c)||isDigit(c))|| (c == '(')))
     {
          cout<<"选择T->FB产生式逆序压栈"<<endl;
         Push(S , 'B');
         Push(S , 'F');
      }
      else if ((e == 'B') && (c == '*'||c=='/'))
     {
        cout<<"选择B->w2FB产生式逆序压栈"<<endl;
         Push(S , 'B');
         Push(S , 'F');
         cout<<"匹配w2"<<endl;
         ch++;
         c = *ch;
     }
      else if ((e == 'B') && ((c == '+'||c=='-')||(c == ')')||(c == '#')));
      else if ((e == 'F') && (isLetter(c)||isDigit(c)))
    {
      ch++;
      c=*ch;
      while (isLetter(c)||isDigit(c)){
        c=*ch;
         ch++;
      }
       cout<<"匹配i"<<endl;

    }
     else if ((e == 'F') && (c == '('))
     {
       cout<<"选择F->(E)产生式逆序压栈"<<endl;
       Push(S, ')');
       Push(S, 'E');
       ch++;
       c = *ch;
        cout<<"匹配("<<endl;

     }
     else if ((e == ')') && (c == ')'))
   {
        ch++;
        c = *ch;
         cout<<"匹配)"<<endl;
   }
     else  error();
   }
    cout<<"该符号串是文法的句子"<<endl;
}



int main(){
          SqStack S;
          InitStack(S);
          char c;int n;
          cout<<"输入表达式并以#结尾:"<<endl;
	      cin>>str;
	      c=*str;
	       cout<<"请选择语法分析方式1.递归子程序法2.LL(1)分析法"<<endl;
	       cin>>n;
	       if(n==1)recursion();
	       if(n==2)LL1Analysis(S, str, c);


}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值