#include <stdio.h>
#include <string>
#include <stack>
#include <iostream>
using namespace std;
#define acc 0
#define err 15
int Action[12][6] = {
{ 5, err, err, 4, err, err },
{ err, 6, err, err, err, acc },
{ err, -2, 7, err, -2, -2 },
{ err, -4, -4, err, -4, -4 },
{ 5, err, err, 4, err, err },
{ err, -6, -6, err, -6, -6 },
{ 5, err, err, 4, err, err },
{ 5, err, err, 4, err, err },
{ err, 6, err, err, 11, err },
{ err, -1, 7, err, -1, -1 },
{ err, -3, -3, err, -3, -3 },
{ err, -5, -5, err, -5, -5 }
};
int GoTo[12][3] = {
{ 1, 2, 3 },
{ err, err, err },
{ err, err, err },
{ err, err, err },
{ 8, 2, 3 },
{ err, err, err },
{ err, 9, 3 },
{ err, err, 10 },
{ err, err, err },
{ err, err, err },
{ err, err, err },
{ err, err, err }
};
string str = "@+*()#";
string nt = "ETF";
int lookup(char c, int state){
int index = str.find_first_of(c);
return Action[state][index];
}
int go(char c, int state){
int index = nt.find_first_of(c);
return GoTo[state][index];
}
void parse(FILE *fp){
int state = 0, temp = 1;
char c;
stack<int> ss;
stack<char> sts;
ss.push(state);
fread(&c, sizeof(char), 1, fp);
while (temp){
state = ss.top();
temp = lookup(c, state);
if (temp > 0){
ss.push(temp);
sts.push(c);
fread(&c, sizeof(char), 1, fp);
}else if (temp < 0){
switch (-temp){
case 1:
printf("E->E+T.\n");
ss.pop();
ss.pop();
ss.pop();
sts.pop();
sts.pop();
sts.pop();
sts.push('E');
break;
case 2:
printf("E->T.\n");
ss.pop();
sts.pop();
sts.push('E');
break;
case 3:
printf("T->T*F.\n");
ss.pop();
ss.pop();
ss.pop();
sts.pop();
sts.pop();
sts.pop();
sts.push('T');
break;
case 4:
printf("T->F.\n");
ss.pop();
sts.pop();
sts.push('T');
break;
case 5:
printf("F->(E).\n");
ss.pop();
ss.pop();
ss.pop();
sts.pop();
sts.pop();
sts.pop();
sts.push('F');
break;
case 6:
printf("F->id.\n");
ss.pop();
sts.pop();
sts.push('F');
break;
default:
printf("error action.\n");
break;
}
int r = go(sts.top(), ss.top());
if (r != err){
ss.push(r);
}else {
temp = 0;
printf("error goto.\n");
}
}else{
printf("pass.\n");
}
}
}