#include<iostream>
#include<stack>
#include <fstream>
#include <cassert>
#include <string>
using namespace std;
char vt[7]={'i','+','*','(',')','#','='};//终结符
char vn[6]={'E','G','T','S','F','A'};//非终结符
char M[]={'1','+','(','2','-','1',')','*','5','\0' };
//char R[20]={''};
char B[20];//输入流
char A[20];
int j=0,b=0,top=0,l;/*L为输入串长度*/
//char String[8]={"E->TG","T->FS","G->+TG","G->^","S->*FS","S->^","F->(E)","F->i","A->i=E"};
stack<char> S1;//分析栈
struct node
{
char Left;//产生式左部
char Right[5];//产生式右部
int length;//长度
}e,t,g,g1,s,s1,f,f1,a,C[6][7],temp;//定义结构体和建立几个类
void InitTable()//完成文法产生式的初始化
{
e.Left='E';/*E产生式左边*/
strcpy(e.Right,"TG");/*E产生式的右部*/
e.length=2;
t.Left='T';
strcpy(t.Right,"FS");
t.length=2;
g.Left='G';
strcpy(g.Right,"+TG");
g.length=3;
g1.Left='G';
g1.Right[0]='^';
g1.length=1;
s.Left='S';
strcpy(s.Right,"*FS");
s.length=3;
s1.Left='S';
s1.Right[0]='^';
s1.length=1;
f.Left='F';
strcpy(f.Right,"(E)");
f.length=3;
f1.Left='F';
f1.Right[0]='i';
f1.length=1;
a.Left='A';
strcpy(a.Right,"i=E");
a.length=3;
for(int i=0;i<6;i++)//预测分析表的初始化
for(int j=0;j<7;j++)
C[i][j].Left='N';
C[0][0]=e;C[0][3]=e;
C[1][1]=g;C[1][4]=g1;C[1][5]=g1;
C[2][0]=t;C[2][3]=t;
C[3][1]=s1;C[3][2]=s;C[3][4]=C[3][5]=s1;
C[4][0]=f1;C[4][3]=f;
C[5][0]=a;
cout<<"提示:本程序只能对由'i','+','*','(',')','='构成的以'#'结束的字符串进行分析,\n"<<endl;
cout<<"该文法的产生式如下所示:"<<endl;
cout<<e.Left<<"->"<<e.Right<<endl;
cout<<t.Left<<"->"<<t.Right<<endl;
cout<<g.Left<<"->"<<g.Right<<endl;
cout<<g1.Left<<"->"<<g1.Right<<endl ;
cout<<s.Left<<"->"<<s.Right<<endl;
cout<<s1.Left<<"->"<<s1.Right<<endl;
cout<<f.Left<<"->"<<f.Right<<endl;
cout<<f1.Left<<"->"<<f1.Right<<endl;
cout<<a.Left<<"->"<<a.Right<<endl;
}
int PriorityJudge(char sign)//优先级的判断
{
switch(sign){
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '%':
case '^':
return 3;
case '(':
case ')':
default:
return 0;
}
}
void changeM2R(char *s1,char *s2)/*中缀式转化为后缀式*/
{
stack<char> T;
int i=0,j=0;
T.push('@');//压入终止符
char ch;
ch=s1[i];
while(ch!='\0')
{
if(ch==' ')//空格直接跳下一个
{
ch=s1[++i];
}
else if(ch=='(')//左括号
{
T.push(ch);
ch=s1[++i];
}
else if(ch=')')
{
while(T.top()!='(')
{
s2[j++]=T.top();
T.pop();
ch=s1[++i];
}
}
else if(ch=='+'||ch=='-'||ch=='*'||ch=='^'||ch=='/'||ch=='%')
{
char w=T.top();
while(PriorityJudge(w)>PriorityJudge(ch)){
s2[j++]=w;
T.pop();
w=T.top();
}
T.push(ch);
ch=s1[++i];
}
else
{
while(ch>'0'&&ch<'9')//数字直接输出
{
s2[j++]=ch;
ch=s1[i++];
}
}
}
ch=T.top();
while(ch!='@'){
s2[j++]=ch;
ch=T.top();
}
s2[j++]='\0';
}
/*void Change(char* s1,char* s2){
seqstack T;
int i=0,j=0;
char ch;
SetNull(&T);
Push(&T,'@');
ch=s1[i];
while(ch!='\0'){
if(ch==' ')
ch=s1[++i];
else if(ch=='('){
Push(&T,ch);
ch=s1[++i];
}
else if(ch==')'){
while(GetTop(&T)!='(')
s2[j++]=Pop(&T);
Pop(&T);
ch=s1[++i];
}
else if(ch=='+' || ch=='-' ||ch=='*' ||ch=='/' ||ch=='^' ||ch=='%'){
char w=GetTop(&T);
while(Precedence(w)>=Precedence(ch)){
s2[j++]=w;
Pop(&T);
w=GetTop(&T);
}
Push(&T,ch);
ch=s1[++i];
}
else{
while((ch>='0' && ch<='9')||ch=='.'){
s2[j++]=ch;
ch=s1[++i];
}
s2[j++]=' ';
}
}
ch=Pop(&T);
while(
ch!='@'){
s2[j++]=ch;
ch=Pop(&T);
}
s2[j++]='\0';
}
*/
void printstack()
{
int a;/*指针*/
for(a=0;a<=top+1;a++)
cout<<A[a];
cout<<"\t\t";
}
void printstream()
{
int j;
for(j=0;j<b;j++)/*输出对齐符*/
//printf(" ");
cout<<" ";
for(j=b;j<=l;j++)
// printf("%c",B[j]);
cout<<B[j];
cout<<"\t\t\t";
//printf("\t\t\t");
}
void readTxt(string file)
{
ifstream infile;
infile.open(file.data()); //将文件流对象与文件连接起来
assert(infile.is_open()); //若失败,则输出错误消息,并终止程序运行
char c;
while (!infile.eof())
{
infile>>c;
//cout<<c<<endl;
}
infile.close(); //关闭文件输入流
}
int main()
{
//changeM2R(M,R);
//cout<<R;
char ch,x;int m,n,j=0,flag=0,finish=0,count=1;
InitTable();
cout<<"请输入要分析的字符串:"<<endl;
//fropen("input.txt),'r','stin');
string file="input.txt";
do
{
// readTxt("input.txt");
cin>>ch;
if((ch!='i')&&(ch!='+')&&(ch!='*')&&(ch!=')')&&(ch!='(')&&(ch!='#')&&(ch!='='))
{
cout<<"输入的字符中有非法字符请重新输入"<<endl;
//exit(1);
}
B[j]=ch;
j++;//保存到输入流中
}while(ch!='#');//完成预处理
cout<<B<<endl;
l=j;//分析串长度
ch=B[0];//取流中首字符
A[top]='#';
A[++top]='A';
//S1.push('#');
//S1.push('E');
cout<<"对符号串"<<B<<"的分析结果如下"<<endl;
cout<<"步骤\t\t分析栈\t\t剩余输入串\t\t使用的产生式或匹配"<<endl;
do//开始对流的分析
{
//x=S1.top();
x=A[top--];//当前栈顶字符
cout<<count++;//输出步骤
cout<<"\t\t";
for(j=0;j<7;j++)
{
if(x==vt[j])
{
flag=1;
break;
}
}
if(flag==1)//对终结符的处理
{
if(x=='#')
{
finish=1;//表示结束
cout<<"acc!\n";//接受
//cout<<"Analysis Success!"<<endl;
getchar();
getchar();
//exit(1);
}
if(x=ch)
{
printstack();
printstream();
//输出栈中元素
//输出流中元素
cout<<ch<<"匹配"<<endl;
ch=B[++b];
flag=0;
}
else
{
printstack();
printstream();
//输出栈中元素
//输出流中元素
cout<<"出错"<<ch<<endl;
continue;
//exit(1);
}
}/*id*/
else
{
for(j=0;j<6;j++)
if(x==vn[j])//非终结符的处理
{
m=j;//行号
break;
}
for(j=0;j<7;j++)
if(ch==vt[j])
{
n=j;
break;
}
temp=C[m][n];
if(temp.Left!='N')/*判断是否为空*/
{
printstack();
printstream();
//输出流
//输出栈
cout<<temp.Left<<"->";
for(j=0;j<temp.length;j++)
cout<<temp.Right[j];
cout<<"\n";//输出换行
for(j=(temp.length-1);j>=0;j--)/*产生式逆序入栈*/
A[++top]=temp.Right[j];
if(A[top]=='^')/*为空则不进栈*/
top--;
}
else/*出错处理*/
{
printstack();
printstream();
//输出流print();
//输出栈 print1();
cout<<x<<"出错\n";
cout<<"---------------------------------------------------------------"<<endl;
cout<<"Analysis Failed"<<endl;
finish=1;
//exit(1);
}/*else*/
}
}while(finish==0);
system("pause");
int a;
cin>>a;
}
#include<stack>
#include <fstream>
#include <cassert>
#include <string>
using namespace std;
char vt[7]={'i','+','*','(',')','#','='};//终结符
char vn[6]={'E','G','T','S','F','A'};//非终结符
char M[]={'1','+','(','2','-','1',')','*','5','\0' };
//char R[20]={''};
char B[20];//输入流
char A[20];
int j=0,b=0,top=0,l;/*L为输入串长度*/
//char String[8]={"E->TG","T->FS","G->+TG","G->^","S->*FS","S->^","F->(E)","F->i","A->i=E"};
stack<char> S1;//分析栈
struct node
{
char Left;//产生式左部
char Right[5];//产生式右部
int length;//长度
}e,t,g,g1,s,s1,f,f1,a,C[6][7],temp;//定义结构体和建立几个类
void InitTable()//完成文法产生式的初始化
{
e.Left='E';/*E产生式左边*/
strcpy(e.Right,"TG");/*E产生式的右部*/
e.length=2;
t.Left='T';
strcpy(t.Right,"FS");
t.length=2;
g.Left='G';
strcpy(g.Right,"+TG");
g.length=3;
g1.Left='G';
g1.Right[0]='^';
g1.length=1;
s.Left='S';
strcpy(s.Right,"*FS");
s.length=3;
s1.Left='S';
s1.Right[0]='^';
s1.length=1;
f.Left='F';
strcpy(f.Right,"(E)");
f.length=3;
f1.Left='F';
f1.Right[0]='i';
f1.length=1;
a.Left='A';
strcpy(a.Right,"i=E");
a.length=3;
for(int i=0;i<6;i++)//预测分析表的初始化
for(int j=0;j<7;j++)
C[i][j].Left='N';
C[0][0]=e;C[0][3]=e;
C[1][1]=g;C[1][4]=g1;C[1][5]=g1;
C[2][0]=t;C[2][3]=t;
C[3][1]=s1;C[3][2]=s;C[3][4]=C[3][5]=s1;
C[4][0]=f1;C[4][3]=f;
C[5][0]=a;
cout<<"提示:本程序只能对由'i','+','*','(',')','='构成的以'#'结束的字符串进行分析,\n"<<endl;
cout<<"该文法的产生式如下所示:"<<endl;
cout<<e.Left<<"->"<<e.Right<<endl;
cout<<t.Left<<"->"<<t.Right<<endl;
cout<<g.Left<<"->"<<g.Right<<endl;
cout<<g1.Left<<"->"<<g1.Right<<endl ;
cout<<s.Left<<"->"<<s.Right<<endl;
cout<<s1.Left<<"->"<<s1.Right<<endl;
cout<<f.Left<<"->"<<f.Right<<endl;
cout<<f1.Left<<"->"<<f1.Right<<endl;
cout<<a.Left<<"->"<<a.Right<<endl;
}
int PriorityJudge(char sign)//优先级的判断
{
switch(sign){
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '%':
case '^':
return 3;
case '(':
case ')':
default:
return 0;
}
}
void changeM2R(char *s1,char *s2)/*中缀式转化为后缀式*/
{
stack<char> T;
int i=0,j=0;
T.push('@');//压入终止符
char ch;
ch=s1[i];
while(ch!='\0')
{
if(ch==' ')//空格直接跳下一个
{
ch=s1[++i];
}
else if(ch=='(')//左括号
{
T.push(ch);
ch=s1[++i];
}
else if(ch=')')
{
while(T.top()!='(')
{
s2[j++]=T.top();
T.pop();
ch=s1[++i];
}
}
else if(ch=='+'||ch=='-'||ch=='*'||ch=='^'||ch=='/'||ch=='%')
{
char w=T.top();
while(PriorityJudge(w)>PriorityJudge(ch)){
s2[j++]=w;
T.pop();
w=T.top();
}
T.push(ch);
ch=s1[++i];
}
else
{
while(ch>'0'&&ch<'9')//数字直接输出
{
s2[j++]=ch;
ch=s1[i++];
}
}
}
ch=T.top();
while(ch!='@'){
s2[j++]=ch;
ch=T.top();
}
s2[j++]='\0';
}
/*void Change(char* s1,char* s2){
seqstack T;
int i=0,j=0;
char ch;
SetNull(&T);
Push(&T,'@');
ch=s1[i];
while(ch!='\0'){
if(ch==' ')
ch=s1[++i];
else if(ch=='('){
Push(&T,ch);
ch=s1[++i];
}
else if(ch==')'){
while(GetTop(&T)!='(')
s2[j++]=Pop(&T);
Pop(&T);
ch=s1[++i];
}
else if(ch=='+' || ch=='-' ||ch=='*' ||ch=='/' ||ch=='^' ||ch=='%'){
char w=GetTop(&T);
while(Precedence(w)>=Precedence(ch)){
s2[j++]=w;
Pop(&T);
w=GetTop(&T);
}
Push(&T,ch);
ch=s1[++i];
}
else{
while((ch>='0' && ch<='9')||ch=='.'){
s2[j++]=ch;
ch=s1[++i];
}
s2[j++]=' ';
}
}
ch=Pop(&T);
while(
ch!='@'){
s2[j++]=ch;
ch=Pop(&T);
}
s2[j++]='\0';
}
*/
void printstack()
{
int a;/*指针*/
for(a=0;a<=top+1;a++)
cout<<A[a];
cout<<"\t\t";
}
void printstream()
{
int j;
for(j=0;j<b;j++)/*输出对齐符*/
//printf(" ");
cout<<" ";
for(j=b;j<=l;j++)
// printf("%c",B[j]);
cout<<B[j];
cout<<"\t\t\t";
//printf("\t\t\t");
}
void readTxt(string file)
{
ifstream infile;
infile.open(file.data()); //将文件流对象与文件连接起来
assert(infile.is_open()); //若失败,则输出错误消息,并终止程序运行
char c;
while (!infile.eof())
{
infile>>c;
//cout<<c<<endl;
}
infile.close(); //关闭文件输入流
}
int main()
{
//changeM2R(M,R);
//cout<<R;
char ch,x;int m,n,j=0,flag=0,finish=0,count=1;
InitTable();
cout<<"请输入要分析的字符串:"<<endl;
//fropen("input.txt),'r','stin');
string file="input.txt";
do
{
// readTxt("input.txt");
cin>>ch;
if((ch!='i')&&(ch!='+')&&(ch!='*')&&(ch!=')')&&(ch!='(')&&(ch!='#')&&(ch!='='))
{
cout<<"输入的字符中有非法字符请重新输入"<<endl;
//exit(1);
}
B[j]=ch;
j++;//保存到输入流中
}while(ch!='#');//完成预处理
cout<<B<<endl;
l=j;//分析串长度
ch=B[0];//取流中首字符
A[top]='#';
A[++top]='A';
//S1.push('#');
//S1.push('E');
cout<<"对符号串"<<B<<"的分析结果如下"<<endl;
cout<<"步骤\t\t分析栈\t\t剩余输入串\t\t使用的产生式或匹配"<<endl;
do//开始对流的分析
{
//x=S1.top();
x=A[top--];//当前栈顶字符
cout<<count++;//输出步骤
cout<<"\t\t";
for(j=0;j<7;j++)
{
if(x==vt[j])
{
flag=1;
break;
}
}
if(flag==1)//对终结符的处理
{
if(x=='#')
{
finish=1;//表示结束
cout<<"acc!\n";//接受
//cout<<"Analysis Success!"<<endl;
getchar();
getchar();
//exit(1);
}
if(x=ch)
{
printstack();
printstream();
//输出栈中元素
//输出流中元素
cout<<ch<<"匹配"<<endl;
ch=B[++b];
flag=0;
}
else
{
printstack();
printstream();
//输出栈中元素
//输出流中元素
cout<<"出错"<<ch<<endl;
continue;
//exit(1);
}
}/*id*/
else
{
for(j=0;j<6;j++)
if(x==vn[j])//非终结符的处理
{
m=j;//行号
break;
}
for(j=0;j<7;j++)
if(ch==vt[j])
{
n=j;
break;
}
temp=C[m][n];
if(temp.Left!='N')/*判断是否为空*/
{
printstack();
printstream();
//输出流
//输出栈
cout<<temp.Left<<"->";
for(j=0;j<temp.length;j++)
cout<<temp.Right[j];
cout<<"\n";//输出换行
for(j=(temp.length-1);j>=0;j--)/*产生式逆序入栈*/
A[++top]=temp.Right[j];
if(A[top]=='^')/*为空则不进栈*/
top--;
}
else/*出错处理*/
{
printstack();
printstream();
//输出流print();
//输出栈 print1();
cout<<x<<"出错\n";
cout<<"---------------------------------------------------------------"<<endl;
cout<<"Analysis Failed"<<endl;
finish=1;
//exit(1);
}/*else*/
}
}while(finish==0);
system("pause");
int a;
cin>>a;
}