#include<iostream>
#include<map>
#include<set>
#include<string.h>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
int n1,n2,n3; //分别是文法的长度,非终结符的长度,终结符的长度
const int N=100; //输入规模为小于等于100的文法行
string input_grammer[N]; //输入文法
map<string,vector<string> > cutting_input; //整理完的文法
string is_terminal[N]; //终结符
string not_terminal[N]; //非终结符
bool Is_terminal(string s);
bool is_empty(string s);
bool Not_terminal(string s);
map<pair<string,string> , string> table; //分析表的输出
//table[make_pair("s","T")]="t->s"; 输出格式
//set<string> first_set,fellow_set;
map<string,set<string> > first_set,fellow_set;//first集 fellow集
bool isempty(string s){ //判断文法是否为空(以@为空)
if(s=="@") return true;
vector<string> tmp =cutting_input[s];
for(int i=0;i<tmp.size();i++){
if(tmp[i]=="@") return true;
}
return false;
}
void dfs_one(string t,string s){ //查找单个的终结符
if(Is_terminal(s)){
first_set[t].insert(s);
//cout<<s<<endl;
return ;
}
else if(Not_terminal(s)) {
vector<string> tmp =cutting_input[s];
for(int i=0;i<tmp.size();i++){
for(int j=0;j<tmp[i].size();j++){
string txt="";
txt+=tmp[i][j];
dfs_one(s,txt);
//set_union(first_set[t].begin(), first_set[t].end(), first_set[s].begin(), first_set[s].end(), inserter(first_set[t], first_set[t].begin()));
if(!isempty(txt)) break;
}
}
set_union(first_set[t].begin(), first_set[t].end(), first_set[s].begin(), first_set[s].end(), inserter(first_set[t], first_set[t].begin()));
}
}
void dfs_all(){
for(int i=0;i<n2;i++){
vector<string> tmp = cutting_input[not_terminal[i]];
for(int j=0;j<tmp.size();j++){
int flag=1;
for(int k=0;k<tmp[j].size();k++){
string txt="";
txt+=tmp[j][k];
dfs_one(not_terminal[i],txt);
if(!isempty(txt)){
flag=0;
break;
}
}
//if(!flag) break;
}
}
}
set<string> cal_large(string s){
//cout<<s<<endl;
set<string> tmp;
bool flag=1;
for(int i=0;i<s.size();i++){
string t ="";
t+=s[i];
if(Is_terminal(t)){
tmp.insert(t);
break;
}
else{
set_union(tmp.begin(), tmp.end(), first_set[t].begin(), first_set[t].end(), inserter(tmp, tmp.begin()));
if(!isempty(t)) break;
}
}
return tmp;
}
void Fellow(){
int cir=5;
while(cir--){
for(int i=0;i<n2;i++){
vector<string> tmp = cutting_input[not_terminal[i]];
for(int j=0;j<tmp.size();j++){
//cout<<tmp[j]<<endl;
for(int k=0;k<tmp[j].size()-1;k++){
string tt="";
tt+=tmp[j][k];
string tt_f="";
tt_f+=tmp[j][tmp[j].size()-1];
//cout<<tmp[j][k]<<endl;
//cout<<tt<<endl;
if(Not_terminal(tt)){
set<string> ttmp = cal_large(tmp[j].substr(k+1));
set_union(fellow_set[tt].begin(), fellow_set[tt].end(), ttmp.begin(), ttmp.end(), inserter(fellow_set[tt], fellow_set[tt].begin()));
bool flag=0;
for(set<string>::iterator it=ttmp.begin();it!=ttmp.end();it++){
if((*it)=="@") flag=1;
//cout<<"here"<<endl;
}
if(flag){
// cout<<tt<<' '<<not_terminal[i]<<endl;
set_union(fellow_set[tt].begin(), fellow_set[tt].end(), fellow_set[not_terminal[i]].begin(), fellow_set[not_terminal[i]].end(), inserter(fellow_set[tt], fellow_set[tt].begin()));
set_union(fellow_set[tt_f].begin(), fellow_set[tt_f].end(), fellow_set[not_terminal[i]].begin(), fellow_set[not_terminal[i]].end(), inserter(fellow_set[tt_f], fellow_set[tt_f].begin()));
}
}
}
}
}
}
}
bool Is_terminal(string s){ //判断是否为终结符
bool flag=0;
for(int i=0;i<n3;i++)
if(s==is_terminal[i])
flag=1;
return flag;
}
bool Not_terminal(string s){ //判断是否是非终结符
bool flag=0;
for(int i=0;i<n3;i++)
if(s==not_terminal[i])
flag=1;
return flag;
}
void input(){
cout<<"请输入有几个文法行:";
cin>>n1;
cout<<"请接着输入上述的文法行:"<<endl;
for(int i=0;i<n1;i++) cin>>input_grammer[i];
//cout<<input_grammer[0][0];
for(int i=0;i<n1;i++){
string terminal=input_grammer[i];
string tmp="";
tmp+=terminal[0];
//cutting_input[tmp]=input_grammer[i].substr(3);
cutting_input[tmp].push_back(input_grammer[i].substr(3));
}
cout<<"请输入非终结符的个数:";
cin>>n2;
cout<<"请输入上述的非终结符:";
for(int i=0;i<n2;i++) cin>>not_terminal[i];
cout<<"请输入终结符的个数:";
cin>>n3;
cout<<"请输入上述的终结符(最后一个为空):";
for(int i=0;i<n3;i++) cin>>is_terminal[i];
for(int i=0;i<n2;i++){
fellow_set[not_terminal[i]].insert("#");
}
}
void output(){
cout<<"\t\t\t分析表"<<endl;
for(int i=0;i<n3;i++){
if(!i) cout<<"\t"<<is_terminal[i]<<"\t";
else{
if(is_terminal[i]=="@")
cout<<"#\t";
else
cout<<is_terminal[i]<<"\t";
}
}
cout<<endl;
for(int i=0;i<n2;i++){
cout<<not_terminal[i]<<"\t";
for(int j=0;j<n3;j++){
if(table[make_pair(not_terminal[i],is_terminal[j])]!=""){
cout<<table[make_pair(not_terminal[i],is_terminal[j])];
}
cout<<"\t";
}
cout<<endl;
}
//最后输出栈
stack<char> input_s; //输入栈
stack<char> match_s; //匹配栈
string I;
cout<<"请输入要预测分析的字符串:";
cin>>I;
input_s.push('#');
for(int i=0;i<I.size();i++){
input_s.push(I[I.size()-i-1]);
}
match_s.push('#');
match_s.push('E');
cout<<"\t栈\t输入\t动作\t"<<endl;
while(1){
stack<char> tmp_i = input_s;
stack<char> tmp_m = match_s;
string s_i="";
string s_m="";
while(!tmp_i.empty()){
s_i+=tmp_i.top();
tmp_i.pop();
}
while(!tmp_m.empty()){
s_m+=tmp_m.top();
tmp_m.pop();
}
//cout<<"\t"+s_i+"\t";
cout<<"\t";
for(int i=0;i<s_i.size();i++)
cout<<s_i[s_i.size()-i-1];
cout<<"\t";
cout<<"\t"+s_m+"\t";
string m_top = "";
m_top+=match_s.top();
// match_s.pop();
if(input_s.top()==match_s.top()){
cout<<"匹配"<<input_s.top()<<"\t";
match_s.pop();
input_s.pop();
}
else{
match_s.pop();
string i_top="";
i_top+=input_s.top();
string match = table[make_pair(m_top,i_top)].substr(3);
if(match!="@"){
for(int i=0;i<match.size();i++){
match_s.push(match[match.size()-i-1]);
}
//cout<<"输出"<<table[make_pair(m_top,i_top)]<<'\t';
}
cout<<"输出"<<table[make_pair(m_top,i_top)]<<'\t';
}
cout<<endl;
if(match_s.top()==input_s.top()&&input_s.top()=='#') break;
}
}
void create_Table(){
for(int i=0;i<n2;i++){
vector<string> tmp = cutting_input[not_terminal[i]];
//first table
for(int j=0;j<tmp.size();j++){
set<string> tmp_set = cal_large(tmp[j]);
for(set<string>::iterator it=tmp_set.begin();it!=tmp_set.end();it++){
table[make_pair(not_terminal[i],(*it))]=not_terminal[i]+"->"+tmp[j];
}
}
//fellow table
set<string> tmp_set = first_set[not_terminal[i]];
bool flag=0;
for(set<string>::iterator it=tmp_set.begin();it!=tmp_set.end();it++){
if((*it)=="@"){
flag=1;
break;
}
}
if(flag){
set<string> fellow_tmp = fellow_set[not_terminal[i]];
for(set<string>::iterator it=fellow_tmp.begin();it!=fellow_tmp.end();it++){
string t = not_terminal[i];
t+="->@";
table[make_pair(not_terminal[i],(*it))]=t;
}
}
}
}
int main(){
input();
//for(int i=0;i<n2;i++) cout<<not_terminal[i];
//cout<<endl;
dfs_all();
Fellow();
create_Table();
output();
//cout<<table[make_pair("G","#")]<<endl;
return 0;
}