简易版:
/*
E->TE1;
E1->+TE1|-TE1|ε;
T->FT1;
T1->*FT1|/FT1|ε;
F->(E)|i
*/
#include <iostream>
#include <string.h>
using namespace std;
char a[10];
int flag=0;
void E1();
void T();
void T1();
void F();
//void PrintString(){}
//void PrintRemain(){}
//bool march(){}
//void GetSymbol(){}
void E(){
T();
E1();
cout<<"E->TE1"<<endl;
//flag = 1;
//PrintString();
//PrintRemain();
}
void E1(){
if(a[flag]=='+'||a[flag]=='-')
{
cout<<"E1->"<<a[flag]<<"TE1"<<endl;
flag++;
T();
E1();
}
else
cout<<"E->ε"<<endl;
}
void T()
{
F();
T1();
cout<<"T->FT1"<<endl;
}
void T1()
{
if (a[flag]=='*'||a[flag]=='/'){
cout<<"T1->"<<a[flag]<<"T1F"<<endl;
flag++;
F();
T1();
}
else
cout<<"T1->ε"<<endl;
}
void F(){
if(a[flag]=='i'){
cout<<"F->i"<<endl;
flag++;
}
else if(a[flag]=='('){
flag++;
E();
if(a[flag]==')'){
cout<<"F->(E)"<<endl;
flag++;
}
else
{
cout<<"括号不匹配!"<<endl;
exit(0);
}
}
else
{
cout<<"非法字符!"<<endl;
exit(0);
}
}
int main()
{
cout<<"请输入算术表达式!(请以#结尾)"<<endl;
cin>>a;
E();
if(a[flag]=='#')
cout<<"句子结构正确!"<<endl;
else
cout<<"无结束标志,分析失败!"<<endl;
return 0;
}