-
Notifications
You must be signed in to change notification settings - Fork 0
/
automata_standardize.cpp
162 lines (142 loc) · 4.42 KB
/
automata_standardize.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "automata_standardize.h"
#include "automata.h"
#include "automata_determize.h"
#include "comp_language.h"
using namespace std;
template<typename T> void printElement(T t, const int& width) {
const char separator = ' ';
cout << left << setw(width) << setfill(separator) << t;
}
Automata_standardize::Automata_standardize(string name ):Comp_language(name){
}
vector<string> Automata_standardize::list_transition_init_state(){
this->_transitions_table_standardize = this->_transitions_table_determiniaze;
return this->_transitions_table_standardize[this->_init_states_deterministic];
}
void Automata_standardize::create_state_i(){
string name = "i";
this->_transitions_table_standardize[name] = this->list_transition_init_state();
}
void Automata_standardize::define_state_i_as_init_state(){
this->_init_states_standardize = "i";
}
bool Automata_standardize::is_standardize(){
int tmp = 0;
// don't need to check mutiple entries because we use CDFA
for(map<string,vector<string> >::const_iterator at = this->_transitions_table_determiniaze.begin(); at != this->_transitions_table_determiniaze.end(); ++at) {
for(vector<string> ::const_iterator it = (at->second).begin(); it != (at->second).end(); ++it) {
if(*it == this->_init_states_deterministic){
cout <<"not standardized"<<endl;
tmp = 1;
}
}
}
if( tmp==0){
this->_init_states_standardize = this->_init_states_deterministic;
this->_transitions_table_standardize = this->_transitions_table_determiniaze;
std::cout << "already standardized" << std:: endl;
}
return tmp;
}
void Automata_standardize::standardize(){
this->create_state_i();
this->define_state_i_as_init_state();
}
void Automata_standardize::display() {
cout << "3: STANDARDIZE AUTOMATA" << endl;
cout << "initial state(s):" << endl;
cout << this->_init_states_standardize << endl;
cout << "final state(s):" << endl;
for(vector<string> ::const_iterator at = this-> _final_states_comp_language.begin(); at != this-> _final_states_comp_language.end(); ++at) {
cout << *at+" ";
}
cout << endl;
cout << "transition table:" << endl;
this->display_standardize_automaton();
}
void Automata_standardize::display_standardize_automaton() {
const int nameWidth = 10;
const int numWidth = 15;
cout << "display_standardize_automaton"<<endl;
// display banner with available states
printElement("S", nameWidth);
for(int i=0; i<this->_nb_transitions_available; i++) {
printElement(char(97+i), numWidth);
}
cout << endl;
// printElement is a template (function) to display data with good indentation
for(map<string,vector<string> >::const_iterator at = this->_transitions_table_standardize.begin(); at != this->_transitions_table_standardize.end(); ++at) {
printElement(at->first, nameWidth);
for(vector<string> ::const_iterator it = (at->second).begin(); it != (at->second).end(); ++it) {
string tmp;
tmp = *it;
if(tmp =="")
{
tmp="-";
}
printElement(tmp.erase(0,0), numWidth);
}
cout << endl;
}
}
bool Automata_standardize::find_letter_in_vector(string index)
{
for(vector<string> ::const_iterator at = this->_final_states_comp_language.begin(); at != this->_final_states_comp_language.end(); ++at) {
if (index == *at)
return 1;
}
return 0;
}
void Automata_standardize::read_word()
{
string word;
bool belong;
do
{
belong=1;
cout << "Please enter a word (type 'end' to quit)" << endl;
getline(cin, word);
for (int i=0;i<word.size();i++)
{
int nbr = (int)word[i]-97;
if (nbr>=this->_nb_transitions_available)
{
belong=0;
break;
}
}
if (word!="end" && belong)
{
if (recognize_word(word,this->_init_states_standardize,0))
cout << "The word '" << word << "' is well recognized by the automaton !" << endl;
else
cout << "The word '" << word << "' is not recognized by the automaton." << endl;
}
else if (!belong && word!="end")
cout << "The word '" << word << "' is not recognized by the automaton." << endl;
} while (word!="end");
}
bool Automata_standardize::recognize_word(string word, string state, int index)
{
if (index<=word.size()-1)
{
int nbr= (int)word[index]-97;
string value=this->_transitions_table_standardize[state][nbr];
if (value == "" && !find_letter_in_vector(value))
{
return 0;
}
else if (find_letter_in_vector(value) && index==word.size()-1)
{
return 1;
}
else
{
return recognize_word(word,value,index+1);
}
}
else
{
return 0;
}
}