直接上代码
#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);
}