#include<stdio.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
int num;
stack<char> A;
stack<char> B;
int f(char x,char y)
{
if(x == 'E'&&(y == 'i' || y=='('))
{
cout<<num<<" "<<"E->TG"<<endl;
A.pop();
A.push('G');
A.push('T');
num++;
return 1;
}
else if(x == 'G'&&y=='+')
{
cout<<num<<" "<<"G->+TG"<<endl;
A.pop();
A.push('G');
A.push('T');
A.push('+');
num++;
return 1;
}
else if(x == 'G'&&(y ==')' || y == '#'))
{
cout<<num<<" "<<"G->^"<<endl;
A.pop();
num++;
return 1;
}
else if(x == 'T'&&(y=='i'||y=='('))
{
cout<<num<<" "<<"T->FS"<<endl;
A.pop();
A.push('S');
A.push('F');
num++;
return 1;
}
else if(x =='S' && y == '*')
{
cout<<num<<" "<<"S->*FS"<<endl;
A.pop();
A.push('S');
A.push('F');
A.push('*');
num++;
return 1;
}
else if(x =='S' && (y ==')' || y == '#'||y=='+'))
{
cout<<num<<" "<<"S->^"<<endl;
A.pop();
num++;
return 1;
}
else if(x == 'F' && y == 'i')
{
cout<<num<<" "<<"F->i"<<endl;
A.pop();
A.push('i');
num++;
return 1;
}
else if(x == 'F' && y == '(')
{
cout<<num<<" "<<"F->(E)"<<endl;
A.pop();
A.push(')');
A.push('E');
A.push('(');
num++;
return 1;
}
else
{
if(x == y)
{
A.pop();
B.pop();
return 1;
}
else
{
return 0;
}
}
}
void reset()
{
num = 1;
while(!A.empty())
{
A.pop();
}
A.push('#');
A.push('E');
while(!B.empty())
{
B.pop();
}
}
int main()
{
string s;
while(cin>>s)
{
reset();
int len =s.length();
for(int i = len -1;i >= 0;i--)
{
B.push(s[i]);
}
while(1)
{
if(A.top() == '#'&& B.top() == '#')
{
cout<<"acc!"<<endl;
break;
}
else
{
int key = f(A.top(),B.top());
if(key == 0)
{
cout<<"error!"<<endl;
break;
}
}
}
}
return 0;
}
PS:这个题和D题的代码其实相同,改下输出就行,但是我的D题的代码有BUG,D题没测出来,这是修改后的,感觉改完可以过D题(改输出);BUG 在对G的判断和对S判断,没有加后面的条件,其实,在E和T的判断后面不用加条件了,也能过,但是感觉有问题,题目不完善吧。
本文介绍了一种使用预测分析法进行SDUT表达式语法分析的方法。通过示例代码展示了如何处理不同字符的匹配情况,并在遇到错误时输出错误信息。文章最后提到了代码的一个历史BUG及其修复,指出对G和S的判断条件改进后的代码可能适用于另一道题目。
1809

被折叠的 条评论
为什么被折叠?



