编译原理实验代码(有Bug)
#include "pch.h"
#include <iostream>
#include <vector>
#include <iostream>
#include <fstream>
#include <cassert>
#include <string>
#include <list>
#include <iomanip>
#include <sstream>
#include<memory>
using namespace std;
string string_keyword[] = { "int","main","if","return","char","else" };
int string_keyword_len = 5;
string string_operator[] = { "(",")","\"","\'","+","-","*",",",";","\." ,"=", "/", "{","}","<",">",">=", "<=","&&","||" };
int string_operator_len = 20;
struct A
{
string name;
int id;
};
struct B
{
char kind;
int id;
};
class word_kind
{
private:
ifstream infile;
int class_i = 0, class_C = 0, class_s = 0, class_c = 0, class_k = 0, class_p = 0;
public:
list<struct A> token_i, token_C, token_s, token_c, token_k, token_p;
list<struct B> out_put;
list<string> List_get_word;
string reverse(char name, int id);
void read(string file);
word_kind(string s);
void fengli();
};
string word_kind::reverse(char name, int id)
{
list<struct A>::iterator s_i;
id--;
if (name == 'i')
{
s_i = token_i.begin();
for (int i = 0; i < id; i++)
s_i++;
return (*s_i).name;
}
if (name == 'C')
{
s_i = token_C.begin();
for (int i = 0; i < id; i++)
s_i++;
return (*s_i).name;
}
if (name == 's')
{
s_i = token_s.begin();
for (int i = 0; i < id; i++)
s_i++;
return (*s_i).name;
}
if (name == 'c')
{
s_i = token_c.begin();
for (int i = 0; i < id; i++)
s_i++;
return (*s_i).name;
}
if (name == 'k')
{
s_i = token_k.begin();
for (int i = 0; i < id; i++)
s_i++;
return (*s_i).name;
}
if (name == 'p')
{
s_i = token_p.begin();
for (int i = 0; i < id; i++)
{
s_i++;
}
return (*s_i).name;
}
}
void word_kind::fengli()
{
bool flag_opt = true;
while (!List_get_word.empty())
{
string tmp = List_get_word.front();
List_get_word.pop_front();
char tmp_c = tmp[0];
if (tmp_c == '\"')
{
flag_opt = false;
struct A strct_s;
strct_s.name = tmp;
strct_s.id = ++class_s;
token_s.push_back(strct_s);
struct B struct_out_put;
struct_out_put.kind = 's';
struct_out_put.id = class_s;
out_put.push_back(struct_out_put);
continue;
}
if (tmp_c == '\'')
{
flag_opt = false;
struct A strct_s;
strct_s.name = tmp;
strct_s.id = ++class_C;
token_C.push_back(strct_s);
struct B struct_out_put;
struct_out_put.kind = 'C';
struct_out_put.id = class_C;
out_put.push_back(struct_out_put);
continue;
}
if (tmp_c >= '0' && tmp_c <= '9')
{
for (int j = 0; j < tmp.length(); j++)
{
if (!(tmp[j] >= '0'&&tmp[j] <= '9'))
{
cout << "error not a number" << endl;
continue;
}
}
flag_opt = false;
struct A strct_s;
strct_s.name = tmp;
strct_s.id = ++class_c;
token_c.push_back(strct_s);
struct B struct_out_put;
struct_out_put.kind = 'c';
struct_out_put.id = class_c;
out_put.push_back(struct_out_put);
continue;
}
if (tmp_c >= 'a' && tmp_c <= 'z' || tmp_c >= 'A' && tmp_c <= 'Z' || tmp_c == '_')
{
bool flag = true;
for (int i = 0; i < string_keyword_len; i++)
{
if (!tmp.compare(string_keyword[i]))
{
flag_opt = false;
flag = false;
struct A strct_s;
strct_s.name = tmp;
strct_s.id = ++class_k;
token_k.push_back(strct_s);
struct B struct_out_put;
struct_out_put.kind = 'k';
struct_out_put.id = class_k;
out_put.push_back(struct_out_put);
}
}
if (flag)
{
flag_opt = false;
struct A strct_s;
strct_s.name = tmp;
strct_s.id = ++class_i;
token_i.push_back(strct_s);
struct B struct_out_put;
struct_out_put.kind = 'i';
struct_out_put.id = class_i;
out_put.push_back(struct_out_put);
}
continue;
}
for (int i = 0; i < string_operator_len; i++)
{
if (!(tmp.compare(string_operator[i])))
{
flag_opt = false;
struct A strct_s;
strct_s.name = tmp;
strct_s.id = ++class_p;
token_p.push_back(strct_s);
struct B struct_out_put;
struct_out_put.kind = 'p';
struct_out_put.id = class_p;
out_put.push_back(struct_out_put);
}
}
if (flag_opt)
{
cout << "无法识别的字符串" << tmp << endl;
}
}
}
void word_kind::read(string file)
{
string word = "";
char c;
List_get_word.clear();
infile.open(file.data());
string string_opt;
string_opt.clear();
list<char> list_word_all;
infile >> noskipws;
while (!infile.eof())
{
infile >> c;
list_word_all.push_back(c);
}
list_word_all.pop_back();
infile.close();
list_word_all.push_front('(');
list_word_all.push_back(')');
while (!list_word_all.empty())
{
c = list_word_all.front();
list_word_all.pop_front();
if (c == ' ' || c == '\n')
{
if (word != "")
{
List_get_word.push_back(word);
word.clear();
}
if (string_opt != "")
{
List_get_word.push_back(string_opt);
string_opt.clear();
}
continue;
}
if ((c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z' || c == '_' || c >= '0'&&c <= '9'))
{
if (string_opt != "")
{
List_get_word.push_back(string_opt);
string_opt.clear();
}
word += c;
}
else
{
if (word != "")
{
List_get_word.push_back(word);
word.clear();
}
bool flag = true;
for (int i = 0; i < string_operator_len - 6; i++)
{
if (c == string_operator[i][0])
{
string string_const;
if (c == '\"')
{
string_const = c;
char tmp_c = c;
list_word_all.pop_front();
while (1)
{
tmp_c = c;
c = list_word_all.front();
list_word_all.pop_front();
string_const += c;
if (c == '\"')
{
if (tmp_c != '\\')
break;
}
}
}
if (string_const != "")
{
List_get_word.push_back(string_const);
string_const.clear();
break;
}
if (c == '\'')
{
string_const = c;
c = list_word_all.front();
list_word_all.pop_front();
if (c == '\\')
{
string_const += c;
c = list_word_all.front();
list_word_all.pop_front();
string_const += c;
c = list_word_all.front();
list_word_all.pop_front();
if (c == '\'')
{
string_const += c;
c = list_word_all.front();
list_word_all.pop_front();
}
else
cout << "error 识别常量字符时" << endl;
}
else
{
string_const += c;
c = list_word_all.front();
list_word_all.pop_front();
if (c == '\'')
{
string_const += c;
}
else
cout << "error '" << endl;
}
}
if (string_const != "")
{
List_get_word.push_back(string_const);
string_const.clear();
break;
}
if (word != "")
{
List_get_word.push_back(word);
word.clear();
}
string_opt = c;
List_get_word.push_back(string_opt);
string_opt.clear();
flag = false;
}
}
bool flag_f = true;
if (flag)
for (int i = string_operator_len - 6; i < string_operator_len; i++)
{
if (c == string_operator[i][0])
{
if (word != "")
{
List_get_word.push_back(word);
word.clear();
}
char tmp_;
tmp_ = c;
c = list_word_all.front();
list_word_all.pop_front();
if (c == ' ' || c == '_' || c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z' || c >= '0'&&c <= '9')
{
flag_f = false;
string_opt = tmp_;
List_get_word.push_back(string_opt);
string_opt.clear();
list_word_all.push_front(c);
}
else {
bool flag_tow = true;
string tmp_tow_op;
tmp_tow_op = tmp_;
tmp_tow_op += c;
for (int j = string_operator_len - 6; j < string_operator_len; j++)
if (tmp_tow_op._Equal(string_operator[j]))
{
flag_f = false;
flag_tow = false;
List_get_word.push_back(tmp_tow_op);
tmp_tow_op.clear();
goto goto_end_two_op;
}
if (flag_tow)
cout << "识别二目 界定符 在第二个符号上出错:" << tmp_tow_op << endl;
}
}
}
goto_end_two_op:
if (flag_f&& flag)
cout << "判断一元界符出错" << c << endl;
int x = 0;
}
}
}
word_kind::word_kind(string s)
{
read(s);
fengli();
out_put.pop_back();
out_put.pop_front();
}
int string_Q_len;
string string_Q[] = { "T","E","F" ,"E" ,"E" ,"E" ,"E" ,"E" };
string string_q[] = { "","" };
void show_list_string(list<string> R)
{
cout << endl;
list<string>::iterator i = R.begin();
for (; i != R.end(); i++)
cout << (*i) << " ";
cout << endl;
}
struct struct_QT
{
string op, source1, source2, res;
}typedef strcut_QT;
struct struct_exe2
{
int number;
string start_un;
list<list<string>> SYN;
}typedef struct_exe;
struct state_sentence_index
{
int sentence;
int index;
}typedef state_sentence_index;
struct tmp_table_foresee
{
string not_terminator;
int number;
list<string> content;
}typedef tmp_table_foresee;
string trans_number_to_string(int num)
{
string res;
stringstream ss;
ss << num;
ss >> res;
return res;
}
void show_list_QT(list<struct_QT> r)
{
list<struct_QT>::iterator list_string_i = r.begin();
for (; list_string_i != r.end(); list_string_i++)
{
cout << "(" << (*list_string_i).op << "," << (*list_string_i).source1 << "," << (*list_string_i).source2 << "," << (*list_string_i).res << ")" << endl;
}
}
class exe2
{
private:
string SEM_tmp;
int SEM_tmp_int;
int size_empty;
int sentence_number;
string start_not_terminatroe;
int cur_state;
list<struct_exe> list_all_grammer;
list<state_sentence_index> list_stack_state;
string cur_w;
list<string> now_find_first_left;
list<string> list_can_empty;
list<string> list_SYN;
list<string> list_stack_grammar;
list<string> list_stack_SEM;
list<struct_QT> list_stack_QT;
list<string> list_string_terminator;
list<string> list_string_not_terminator;
list<string> wait_trans_expression;
list<tmp_table_foresee> list_tmp_table_foresee;
public:
list<string> list_equal_i;
exe2(list<string> VN, list<string> VT);
bool if_is_not_terminator(string x);
bool if_is_terminator(string x);
void add_struct_grammer(string fun_stat_un, list<list<string>> x);
void add_expression(string string_express);
void test_show_after_left_change();
void show_foresee_table();
void print();
void change_left_grammer();
void test_left_change();
string get_w();
void find_empty();
void show_empty_list();
bool not_terminator_if_is_empty(string x);
void find_can_empty_not_terminator();
list<string> find_first(string Left, string A);
list<string> find_follow(string a);
void generate_table();
list<string> get_grammer(string left_not_terminator);
void done_LL1(string start_not_terminator);
void done_one_opter(string opteration);
void done_two_opter(string opteration);
void add_express_from_file();
string read_txt(string filename);
bool if_is_equal_i(string x);
void fun_start();
void fun_E();
void fun_E1();
void fun_T();
void fun_T1();
void fun_F();
void start_done();
void test_digui();
bool sort_struct_exe2_start_un(const struct_exe & m1, const struct_exe& m2);
};
string exe2::read_txt(string filename)
{
ifstream infile;
infile.open(filename.data());
assert(infile.is_open());
string s;
while (getline(infile, s))
{
cout << s << endl;
}
infile.close();
return s;
}
bool exe2::if_is_equal_i(string x)
{
list<string>::iterator list_i;
for (list_i = list_equal_i.begin(); list_i != list_equal_i.end(); list_i++)
if ((*list_i) == x)
return true;
return false;
}
void exe2::add_express_from_file()
{
string content;
content = read_txt("D:\\编译原理\\编译原理\\");
string::iterator string_i = content.begin();
while (string_i != content.end())
{
if ((*string_i) == ' ')
content.erase(string_i);
string_i++;
}
}
void exe2::done_two_opter(string opteration)
{
cout << "done_two_opter() " << opteration << endl;
struct_QT tmp_struct;
tmp_struct.op = opteration;
tmp_struct.source2 = list_stack_SEM.back();
list_stack_SEM.pop_back();
tmp_struct.source1 = list_stack_SEM.back();
list_stack_SEM.pop_back();
SEM_tmp_int++;
tmp_struct.res = SEM_tmp + trans_number_to_string(SEM_tmp_int);
list_stack_SEM.push_back(SEM_tmp + trans_number_to_string(SEM_tmp_int));
list_stack_QT.push_back(tmp_struct);
}
void exe2::done_one_opter(string opteration)
{
struct_QT tmp_struct;
tmp_struct.op = opteration;
tmp_struct.source1 = list_stack_SEM.back();
list_stack_SEM.pop_back();
tmp_struct.source2 = "";
SEM_tmp_int++;
tmp_struct.res = SEM_tmp + trans_number_to_string(SEM_tmp_int);
list_stack_SEM.push_back(SEM_tmp + trans_number_to_string(SEM_tmp_int));
list_stack_QT.push_back(tmp_struct);
}
exe2::exe2(list<string> F_VN, list<string> F_VT)
{
list_string_not_terminator = F_VN;
list_string_terminator = F_VT;
sentence_number = 0;
list_tmp_table_foresee.clear();
SEM_tmp = "t";
}
bool exe2::not_terminator_if_is_empty(string x)
{
list<string>::iterator tmp_iter;
for (tmp_iter = list_can_empty.begin(); tmp_iter != list_can_empty.end(); tmp_iter++)
{
if ((*tmp_iter) == x)
return true;
}
return false;
}
void exe2::show_empty_list()
{
list<string>::iterator x;
cout << "show_empty_list() 显示可推出空的非终结符为" << endl;
for (x = list_can_empty.begin(); x != list_can_empty.end(); x++)
{
cout << (*x) << " ";
}
cout << endl;
}
void exe2::find_can_empty_not_terminator()
{
list_can_empty.clear();
list<struct_exe>::iterator list_stru_iter;
for (list_stru_iter = list_all_grammer.begin(); list_stru_iter != list_all_grammer.end(); list_stru_iter++)
{
list<list<string>>::iterator iter_express;
for (iter_express = (*list_stru_iter).SYN.begin(); iter_express != (*list_stru_iter).SYN.end(); iter_express++)
{
if ((*iter_express).front() == "$")
{
list_can_empty.push_back((*list_stru_iter).start_un);
}
}
}
list_can_empty.unique();
int tmp_size_empty = 0;
size_empty = list_can_empty.size();
while (tmp_size_empty != size_empty)
{
tmp_size_empty = size_empty;
find_empty();
size_empty = list_can_empty.size();
}
}
void exe2::find_empty()
{
list<struct_exe>::iterator iter_list_struct;
bool flag;
for (iter_list_struct = list_all_grammer.begin(); iter_list_struct != list_all_grammer.end(); iter_list_struct++)
{
if (not_terminator_if_is_empty((*iter_list_struct).start_un))
continue;
list<list<string>>::iterator iter_list_list_string;
for (iter_list_list_string = (*iter_list_struct).SYN.begin(); iter_list_list_string != (*iter_list_struct).SYN.end(); iter_list_list_string++)
{
flag = true;
list<string>::iterator iter_list_string;
for (iter_list_string = (*iter_list_list_string).begin(); iter_list_string != (*iter_list_list_string).end(); iter_list_string++)
{
if (!not_terminator_if_is_empty(*iter_list_string))
{
flag = false;
break;
}
}
if (flag)
{
list_can_empty.push_back((*iter_list_struct).start_un);
break;
}
}
}
list_can_empty.unique();
}
void exe2::show_foresee_table()
{
list<tmp_table_foresee>::iterator x;
for (x = list_tmp_table_foresee.begin(); x != list_tmp_table_foresee.end(); x++)
{
list<string>::iterator tmp_i;
for (tmp_i = (*x).content.begin(); tmp_i != (*x).content.end(); tmp_i++)
{
cout << "栈顶为 " << (*x).not_terminator << " 当前字符为" << (*tmp_i) << " 期望编号为" << (*x).number << "的表达式" << endl;
}
}
}
void exe2::print()
{
list<string>::iterator x;
for (x = list_SYN.begin(); x != list_SYN.end(); x++)
{
cout << (*x) << " ";
}
cout << setw(40) << cur_w << endl;
}
list<string> exe2::get_grammer(string left_not_terminator)
{
string thorw_x = "error :get_grammer() can't find linked terminator";
list<tmp_table_foresee>::iterator x;
int state_num = 0;
string tmp_cur_w=cur_w;
if (if_is_equal_i(cur_w))
{
tmp_cur_w = cur_w;
cur_w = "i";
}
for (x = list_tmp_table_foresee.begin(); x != list_tmp_table_foresee.end(); x++)
{
if ((*x).not_terminator == left_not_terminator)
{
list<string>::iterator list_iter_content;
for (list_iter_content = (*x).content.begin(); list_iter_content != (*x).content.end(); list_iter_content++)
{
if (cur_w == (*list_iter_content))
{
state_num = (*x).number;
goto lable_break;
}
}
}
}
throw thorw_x;
lable_break: list<string> tmp;
list<struct_exe>::iterator list_struct_iterator;
list<list<string>>::iterator list_list_iter_string;
for (list_struct_iterator = list_all_grammer.begin(); list_struct_iterator != list_all_grammer.end(); list_struct_iterator++)
{
if ((*list_struct_iterator).start_un == left_not_terminator)
{
list_list_iter_string = (*list_struct_iterator).SYN.begin();
for (int i = 0; i < state_num - 1; i++)
{
list_list_iter_string++;
}
list<string>::iterator R;
for (R = (*list_list_iter_string).begin(); R != (*list_list_iter_string).end(); R++)
{
tmp.push_back(*R);
}
break;
}
}
cur_w = tmp_cur_w;
return tmp;
}
void exe2::generate_table()
{
change_left_grammer();
test_show_after_left_change();
list<struct_exe>::iterator list_grammmer_iterator;
for (list_grammmer_iterator = list_all_grammer.begin(); list_grammmer_iterator != list_all_grammer.end(); list_grammmer_iterator++)
{
int number = 0;
list<list<string>>::iterator list_list_iterator;
for (list_list_iterator = (*list_grammmer_iterator).SYN.begin(); list_list_iterator != (*list_grammmer_iterator).SYN.end(); list_list_iterator++)
{
tmp_table_foresee tmp_struct_x;
tmp_struct_x.not_terminator = (*list_grammmer_iterator).start_un;
tmp_struct_x.number = ++number;
tmp_struct_x.content = find_first((*list_grammmer_iterator).start_un, (*list_list_iterator).front());
list_tmp_table_foresee.push_back(tmp_struct_x);
}
}
}
list<string> exe2::find_first(string Left, string A)
{
list<string> acc_list_string;
acc_list_string.clear();
if (A != "" && A[0] == '^')
return acc_list_string;
if (if_is_terminator(A))
{
acc_list_string.push_back(A);
return acc_list_string;
}
if (A == "$" || not_terminator_if_is_empty(A))
{
now_find_first_left.push_back(Left);
acc_list_string.merge(find_follow(Left));
now_find_first_left.pop_back();
return acc_list_string;
}
if (if_is_not_terminator(A))
{
list<struct_exe>::iterator tmp_iterator;
for (tmp_iterator = list_all_grammer.begin(); tmp_iterator != list_all_grammer.end(); tmp_iterator++)
{
if ((*tmp_iterator).start_un == A)
{
list<list<string>>::iterator list_list_iterator;
list<string> tmp_cotent;
tmp_cotent.clear();
for (list_list_iterator = (*tmp_iterator).SYN.begin(); list_list_iterator != (*tmp_iterator).SYN.end(); list_list_iterator++)
{
string F = (*list_list_iterator).front();
tmp_cotent = find_first(A, F);
acc_list_string.merge(tmp_cotent);
}
goto lable_break;
}
}
}
else
{
string x = "erro find_first() the \"" + A + "\" is not a not_terminator";
throw x;
}
lable_break: acc_list_string.unique();
return acc_list_string;
}
list<string> exe2::find_follow(string a)
{
list<struct_exe>::iterator list_all_grammer_iterator;
list<string> acc_list_string, list_tmp;
acc_list_string.clear();
if (a == "E")
{
acc_list_string.push_back("#");
}
for (list_all_grammer_iterator = list_all_grammer.begin(); list_all_grammer_iterator != list_all_grammer.end(); list_all_grammer_iterator++)
{
list<list<string>>::iterator list_grammer_SYN_iterator;
for (list_grammer_SYN_iterator = (*list_all_grammer_iterator).SYN.begin(); list_grammer_SYN_iterator != (*list_all_grammer_iterator).SYN.end(); list_grammer_SYN_iterator++)
{
list<string>::iterator list_string_iterator;
for (list_string_iterator = (*list_grammer_SYN_iterator).begin(); list_string_iterator != (*list_grammer_SYN_iterator).end(); list_string_iterator++)
{
string now_find = (*list_string_iterator);
if (now_find == a && now_find_first_left.back() != (*list_all_grammer_iterator).start_un)
{
list<string>::iterator iter_next = list_string_iterator;
iter_next++;
if (iter_next == (*list_grammer_SYN_iterator).end())
{
now_find_first_left.push_back((*list_all_grammer_iterator).start_un);
acc_list_string.merge(find_follow((*list_all_grammer_iterator).start_un));
now_find_first_left.pop_back();
acc_list_string.unique();
continue;
}
if (if_is_terminator((*iter_next)))
{
list_tmp.push_back((*iter_next));
acc_list_string.merge(list_tmp);
acc_list_string.unique();
continue;
}
{
acc_list_string.merge(find_first((*list_all_grammer_iterator).start_un, (*iter_next)));
acc_list_string.unique();
}
}
}
}
}
acc_list_string.unique();
return acc_list_string;
}
void exe2::done_LL1(string start_not_terminator)
{
generate_table();
show_foresee_table();
find_can_empty_not_terminator();
show_empty_list();
cout << "开始翻译" << endl;
cout << "栈\t\t\t\t\t\t" << "当前符号\t\t\t\t\t\t" << "生成的四元式" << endl;
cur_w = get_w();
list_SYN.push_back("#");
list_SYN.push_back(start_not_terminator);
while (!list_SYN.empty())
{
if ( if_is_equal_i( list_SYN.back()) )
{
list_stack_SEM.push_back(cur_w);
}
if (list_SYN.back()[0] == '^')
{
string a = list_SYN.back();
string::iterator string_i = a.begin();
a.erase(string_i);
done_two_opter(a);
list_SYN.pop_back();
continue;
}
print();
if (list_SYN.back() == "$")
{
list_SYN.pop_back();
if (list_SYN.empty() || wait_trans_expression.empty())
break;
cur_w = get_w();
}
if (list_SYN.back()=="i" && if_is_equal_i(cur_w) || list_SYN.back() ==cur_w )
{
list_SYN.pop_back();
if (list_SYN.empty() || wait_trans_expression.empty())
break;
cur_w = get_w();
}
else
{
list<string> intermidate_list;
intermidate_list.clear();
intermidate_list = get_grammer(list_SYN.back());
if (intermidate_list.front() == "$")
intermidate_list.pop_front();
list_SYN.pop_back();
while (!intermidate_list.empty())
{
list_SYN.push_back(intermidate_list.back());
intermidate_list.pop_back();
}
}
}
if (wait_trans_expression.size() != 0)
cout << "翻译失败" << endl;
else
cout << "翻译完成" << endl;
show_list_QT(list_stack_QT);
}
bool exe2::sort_struct_exe2_start_un(const struct_exe & m1, const struct_exe& m2)
{
return m1.start_un < m2.start_un;
}
void exe2::change_left_grammer()
{
list<struct_exe>::iterator testiterator;
for (testiterator = list_all_grammer.begin(); testiterator != list_all_grammer.end(); ++testiterator)
{
string expression_left = (*testiterator).start_un;
list<list<string>>::iterator list_iterator;
bool if_exist_left = false;
for (list_iterator = (*testiterator).SYN.begin(); list_iterator != (*testiterator).SYN.end(); ++list_iterator)
{
if (expression_left == (*list_iterator).front())
{
if_exist_left = true;
break;
}
}
if (if_exist_left)
{
struct_exe new_not_termi;
new_not_termi.start_un = expression_left + "1";
list_string_not_terminator.push_back(expression_left + "1");
list<list<string>> list_new_list_string;
list<string> list_tmp;
for (list_iterator = (*testiterator).SYN.begin(); list_iterator != (*testiterator).SYN.end(); ++list_iterator)
{
if (expression_left != (*list_iterator).front())
{
(*list_iterator).push_back(expression_left + "1");
}
}
for (list_iterator = (*testiterator).SYN.begin(); list_iterator != (*testiterator).SYN.end();)
{
list_tmp.clear();
if (expression_left == (*list_iterator).front())
{
list_tmp = (*list_iterator);
list_tmp.pop_front();
list_tmp.push_back(expression_left + "1");
list_new_list_string.push_back(list_tmp);
list_iterator = (*testiterator).SYN.erase(list_iterator);
continue;
}
++list_iterator;
}
list_tmp.clear();
list_tmp.push_back("$");
list_new_list_string.push_back(list_tmp);
new_not_termi.SYN = list_new_list_string;
list_all_grammer.push_back(new_not_termi);
}
}
}
void exe2::test_show_after_left_change()
{
list<struct_exe>::iterator show_iter;
for (show_iter = list_all_grammer.begin(); show_iter != list_all_grammer.end(); show_iter++)
{
list<list<string>>::iterator tmp;
for (tmp = (*show_iter).SYN.begin(); tmp != (*show_iter).SYN.end(); tmp++)
{
cout << (*show_iter).start_un << "->";
list<string>::iterator x;
for (x = (*tmp).begin(); x != (*tmp).end(); x++)
{
cout << (*x) << " ";
}
cout << endl;
}
}
}
void exe2::add_expression(string string_express)
{
wait_trans_expression.push_back(string_express);
}
void exe2::add_struct_grammer(string fun_stat_un, list<list<string>> x)
{
struct_exe se;
se.start_un = fun_stat_un;
int len = x.size();
if (len == 0)
{
string err = "err the grammer is error,length is zero please input again";
throw err;
}
se.SYN = x;
se.number = ++sentence_number;
list_all_grammer.push_back(se);
}
void exe2::start_done()
{
try {
fun_E();
}
catch (string x)
{
cout << x << endl;
cout << "程序结束" << endl;
return;
}
if (!wait_trans_expression.size())
cout << "识别完成 字符串正确" << endl;
else
cout << "识别失败" << endl;
}
string exe2::get_w()
{
string x = wait_trans_expression.front();
wait_trans_expression.pop_front();
return x;
}
void exe2::fun_start()
{
cur_w = get_w();
fun_E();
if (cur_w != "#")
throw "erro at main process #";
else
return;
}
void exe2::fun_E()
{
fun_T();
fun_E1();
}
void exe2::fun_T()
{
fun_F();
fun_T1();
}
void exe2::fun_T1()
{
if (cur_w == "*")
{
cur_w = get_w();
fun_F();
fun_T1();
}
else
{
if (cur_w == "/")
{
cur_w = get_w();
fun_F();
fun_T1();
}
}
}
void exe2::fun_E1()
{
if (cur_w == "+")
{
cur_w = get_w();
fun_T();
fun_E1();
}
else
{
if (cur_w == "-")
{
cur_w = get_w();
fun_T();
fun_E1();
}
}
}
void exe2::fun_F()
{
if (if_is_terminator(cur_w))
{
}
else
{
if (cur_w == "(")
{
cur_w = get_w();
fun_E();
try
{
if (cur_w != ")")
{
string x = "err 3 at process F )";
throw x;
}
}
catch (char c)
{
cout << c << endl;
}
}
else
{
string x = "err 2 at Process F (";
throw x;
}
}
cur_w = get_w();
}
bool exe2::if_is_not_terminator(string x)
{
list<string>::iterator FindIterator;
for (FindIterator = list_string_not_terminator.begin(); FindIterator != list_string_not_terminator.end(); FindIterator++)
{
if (x == (*FindIterator))
{
return true;
}
}
return false;
}
bool exe2::if_is_terminator(string x)
{
list<string>::iterator FindIterator;
for (FindIterator = list_string_terminator.begin(); FindIterator != list_string_terminator.end(); FindIterator++)
{
if (x == (*FindIterator))
return true;
}
return false;
}
void exe2::test_left_change()
{
cout << "+++++++++++++++++++++++++++++++++++++++调整左递归文法 测试+++++++++++++++" << endl;
cout << "测试一个文法 T->b+c; T->T+R;" << endl;
cout << "变换后的结果应为 T->b + c T1; T1->+R T;" << endl;
list<list<string>> R;
list<string> r;
r.push_back("b");
r.push_back("+");
r.push_back("c");
R.push_back(r);
r.clear();
r.push_back("T");
r.push_back("+");
r.push_back("R");
R.push_back(r);
add_struct_grammer("T", R);
change_left_grammer();
test_show_after_left_change();
cout << "+++++++++++++++++++++++++++++++++++++++调整左递归文法 测试结束+++++++++++++++" << endl;
}
void exe2::test_digui()
{
wait_trans_expression.push_back("a");
wait_trans_expression.push_back("*");
wait_trans_expression.push_back("b");
wait_trans_expression.push_back("*");
wait_trans_expression.push_back("(");
wait_trans_expression.push_back("a");
wait_trans_expression.push_back("/");
wait_trans_expression.push_back("c");
wait_trans_expression.push_back("-");
wait_trans_expression.push_back("b");
wait_trans_expression.push_back(")");
wait_trans_expression.push_back("#");
try {
fun_E();
}
catch (string x)
{
cout << x << endl;
cout << "程序结束" << endl;
return;
}
if (!wait_trans_expression.size())
cout << "识别完成 字符串正确" << endl;
else
cout << "识别失败" << endl;
}
void add_grammer(exe2 &wkn)
{
list<list<string>> R;
list<string> exp;
exp.push_back("T");
R.push_back(exp);
exp.clear();
exp.push_back("E");
exp.push_back("+");
exp.push_back("T");
exp.push_back("^+");
R.push_back(exp);
exp.clear();
exp.push_back("E");
exp.push_back("-");
exp.push_back("T");
exp.push_back("^-");
R.push_back(exp);
wkn.add_struct_grammer("E", R);
R.clear();
exp.clear();
exp.push_back("F");
R.push_back(exp);
exp.clear();
exp.push_back("T");
exp.push_back("*");
exp.push_back("F");
exp.push_back("^*");
R.push_back(exp);
exp.clear();
exp.push_back("T");
exp.push_back("/");
exp.push_back("F");
exp.push_back("^/");
R.push_back(exp);
wkn.add_struct_grammer("T", R);
R.clear();
exp.clear();
exp.push_back("I");
R.push_back(exp);
exp.clear();
exp.push_back("(");
exp.push_back("E");
exp.push_back(")");
R.push_back(exp);
wkn.add_struct_grammer("F", R);
R.clear();
exp.clear();
exp.push_back("i");
R.push_back(exp);
wkn.add_struct_grammer("I", R);
}
int main()
{
word_kind data_cifa("D:\\编译原理\\编译原理\\wkn.txt");
list<struct B>::iterator s_i = data_cifa.out_put.begin();
list<string> tmp_terminator;
list<string> equal_i;
equal_i.clear();
equal_i.push_back("i");
tmp_terminator.clear();
for (list<struct A>::iterator r = data_cifa.token_i.begin(); r != data_cifa.token_i.end(); r++)
{
tmp_terminator.push_back((*r).name);
equal_i.push_back((*r).name);
}
tmp_terminator.unique();
for (list<struct A>::iterator r = data_cifa.token_c.begin(); r != data_cifa.token_c.end(); r++)
{
tmp_terminator.push_back((*r).name);
equal_i.push_back((*r).name);
}
tmp_terminator.unique();
for (list<struct A>::iterator r = data_cifa.token_p.begin(); r != data_cifa.token_p.end(); r++)
{
tmp_terminator.push_back((*r).name);
}
tmp_terminator.unique();
list<string> not_terminator;
not_terminator.push_back("E");
not_terminator.push_back("H");
not_terminator.push_back("T");
not_terminator.push_back("F");
not_terminator.push_back("W0");
not_terminator.push_back("W1");
not_terminator.push_back("I");
tmp_terminator.push_back("(");
tmp_terminator.push_back(")");
tmp_terminator.push_back("+");
tmp_terminator.push_back("-");
tmp_terminator.push_back("*");
tmp_terminator.push_back("/");
tmp_terminator.push_back("#");
tmp_terminator.push_back("i");
tmp_terminator.unique();
exe2 exe2_LL1(not_terminator,tmp_terminator);
add_grammer(exe2_LL1);
exe2_LL1.list_equal_i = equal_i;
list<struct A>::iterator s_a_i;
for (; s_i != data_cifa.out_put.end(); s_i++)
{
exe2_LL1.add_expression(data_cifa.reverse((*s_i).kind, (*s_i).id));
}
exe2_LL1.add_expression("#");
try {
exe2_LL1.done_LL1("E");
}
catch (string r)
{
cout << r << endl;
}
return 0;
}