-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3 лабораторная LFA.cpp
454 lines (383 loc) · 10.6 KB
/
3 лабораторная LFA.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include <iostream>
#include <math.h>
#include <string>
int length = 10;//количество правил
using namespace std;
//объект - правило
class Rule
{
public:
Rule() {
left = "";
right = "";
}
void operator=(Rule& other) {
this->left = other.left;
this->right = other.right;
}
string left;
string right;
};
ostream& operator<<(ostream& out, Rule obj) {
out << " " << obj.left << " -> " << obj.right << endl;
return out;
};
//УДАЛЕНИЕ ЦЕПНЫХ ПРАВИЛ
//*************************************************************************
int multiplier(char mark, string set) {
int count = 0;
for (int i = 0; i < set.length(); i++)
{
if (mark != set[i])
{
count++;
}
}
return count;
}
int count_rules(char symbol, Rule* rules) {
int count = 0;
for (int i = 0; i < length; i++)
{
if (symbol == rules[i].left[0]) { count++; }
}
return count;
}
int new_len(char symbol, string set, Rule* rules) {
if (set.length() != 1) {
return length + count_rules(set[0], rules) * multiplier(symbol, set); }
return length;
}
void add(Rule* rules, string set, char mark, int len) {
int k = len;
for (int i = 0; i < len; i++)
{
int j = 1;
if (rules[i].left[0] == mark)
{
while (j < set.length())
{
rules[k].left = set[j];
rules[k].right = rules[i].right;
k++;
j++;
}
}
}
}
Rule* delete_chain_rules(Rule* rules) {
string RS = "S"; string RA = "A";
string RB = "B"; string RC = "C";
int count = 0;
int pos[10] = { 0 };
//заполнение множеств цепнях правил
int j = 0;
for (int i = 0; i < length; i++)
{
if (rules[i].left.length() == rules[i].right.length() && (rules[i].right[0] >= 'A' && rules[i].right[0] <= 'Z'))
{
cout << " " << i + 1 <<". "<< rules[i]; count++;
pos[j++] = i;
if (rules[i].right[0] == RS[0] && RS.find(rules[i].left[0]) == string::npos) { RS += rules[i].left[0]; }
if (rules[i].right[0] == RA[0] && RA.find(rules[i].left[0]) == string::npos) { RA += rules[i].left[0]; }
if (rules[i].right[0] == RB[0] && RB.find(rules[i].left[0]) == string::npos) { RB += rules[i].left[0]; }
if (rules[i].right[0] == RC[0] && RC.find(rules[i].left[0]) == string::npos) { RC += rules[i].left[0]; }
}
}
//вывод результата на экран
cout << endl;
cout << "RS = " << RS << "\t RA = " << RA << endl;
cout << "RB = " << RB << "\t RC = " << RC << endl;
//расчет новой длины с учётом всех цепных правил
int len = length;
if (length != new_len('S', RS, rules)) { len = new_len('S', RS, rules); }
if (length != new_len('A', RA, rules)) { len = new_len('A', RA, rules); }
if (length != new_len('B', RB, rules)) { len = new_len('B', RB, rules); }
if (length != new_len('C', RC, rules)) { len = new_len('C', RC, rules); }
len -= count;
//выделение новой памяти
Rule* new_rules = new Rule[len];
int k = 0, i = 0; j = 0;
while (k<length)
{
if (k == pos[j]) { k++; j++; }
new_rules[i] = rules[k];
i++; k++;
}
int temp_len = length - count ;
length = len;
//добавление новых правил
add(new_rules, RS, 'S', temp_len);
add(new_rules, RA, 'A', temp_len);
add(new_rules, RB, 'B', temp_len);
add(new_rules, RC, 'C', temp_len);
delete[]rules;
return new_rules;
}
//*************************************************************************
//УДАЛЕНИЕ ЭПСИЛОН ПРОДУКЦИИ
//*************************************************************************
int find_len(char N_eps, string rule) {
int cout = 0;
for (int i = 0; i < rule.length(); i++)
{
if (N_eps == rule[i])
{
cout++;
}
}
return pow(2,cout)-1;
}
void add_rul(char N_eps, Rule* rules, int len) {
int k = len;
for (int i = 0; i < len; i++)
{
string temp = rules[i].right;
string save = temp;
while (temp.find(N_eps) != string::npos)
{
if (find_len(N_eps, temp) > 1) {
rules[k].left = rules[i].left;
rules[k].right = temp.replace(temp.rfind(N_eps), 1, "");
k++;
temp = save;
}
rules[k].left = rules[i].left;
rules[k].right = temp.replace(temp.find(N_eps), 1, "");
k++;
}
}
}
Rule* delete_eps(Rule* rules) {
string N_eps = "";
//поиск эпсилон продукций
for (int i = 0; i < length; i++)
{
if (rules[i].right == "eps")
{
N_eps = rules[i].left;
}
}
//расчёт новой длины правил
int new_len = length;
int count = 0;
for (int i = 0; i < length; i++)
{
count += find_len(N_eps[0], rules[i].right);
}
new_len += count-1;
//выделение памяти
Rule* new_rule = new Rule[new_len];
//перезапись существующих правил
int j = 0;
for (int i = 0; i < length; i++)
{
if (rules[i].right != "eps")
{
new_rule[j] = rules[i];
j++;
}
}
//сохранение и изменение общей длины
int temp_len = length-1;
length = new_len;
add_rul(N_eps[0], new_rule, temp_len);
delete[]rules;
return new_rule;
}
//*************************************************************************
//УДАЛЕНИЕ НЕ ПРОДУКТИВНЫХ СИМВОЛОВ
//*************************************************************************
bool find_ch(char search, string word) {
if (word.find(search) != string::npos)
{
return false;
}
return true;
}
Rule* accessibleness(Rule* rules) {
string access = "";
access += rules[0].left;
int len = count_rules(access[0], rules);
for (int i = 0; i < len; i++)
{
if (access.find(rules[i].left[0]) != string::npos)
{
access += rules[i].right;
}
}
int count = 0;
for (int i = 0; i < access.length(); i++)
{
char sign = access[i];
for (int j = i+1; j < access.length(); j++)
{
if (sign == access[j]) {
count++;
for (int k = j; k < access.length()-1; k++)
{
access[k] = access[k + 1];
}
}
}
}
int word_len = access.length() - count + 1;
while (word_len < access.length())
{
access[word_len++] = '\0';
}
string Vt = "ad"; string Vn = "SABC";
string Vn_and_Vt = Vn + Vt;
string inaccess;
for (int i = 0; i < Vn_and_Vt.length(); i++)
{
if (find_ch(Vn_and_Vt[i], access) )
{
inaccess += Vn_and_Vt[i];
}
}
int k = 0;
for (int i = 0; i < length - 1; i++)
{
if (rules[i].left == inaccess) { k++; }
rules[i] = rules[k];
k++;
}
Rule* new_rules = new Rule[length - 1];
for (int i = 0; i < length-1; i++)
{
new_rules[i] = rules[i];
}
length--;
delete[]rules;
return new_rules;
}
//*************************************************************************
//Приведение к нормальной форме Хомского
//*************************************************************************
Rule* resize(Rule* rules, int new_len) {
Rule* new_rules = new Rule[new_len];
for (int i = 0; i < length; i++)
{
new_rules[i] = rules[i];
}
length = new_len;
delete[]rules;
return new_rules;
}
Rule* new_char(Rule* rules, string one, char two, string new_name) {
for (int i = 0; i < length; i++)
{
if (rules[i].right.length() > 2)
{
int pos = rules[i].right.find(one);
if (pos != string::npos && rules[i].right[pos+one.length()] == two) { rules[i].right.replace(pos, one.length() + 1, new_name); }
}
}
int temp_len = length;
rules = resize(rules, length + 1);
string temp = "";
temp += one; temp += two;
rules[temp_len].left = new_name; rules[temp_len].right = temp;
return rules;
}
Rule* Homsk(Rule* rules) {
string Vt = "ad"; string Vn = "SABC";
int pos;
for (int k = 0; k < Vt.length(); k++)
{
for (int i = 0; i < length; i++)
{
if (rules[i].right.length() > 1)
{
for (int j = 0; j < rules[i].right.length(); j++)
{
if (rules[i].right[j] == 'a') { rules[i].right.replace(j, 1, "Y"); }
if (rules[i].right[j] == 'd') { rules[i].right.replace(j, 1, "X"); }
}
}
}
}
int temp_len = length;
rules = resize(rules, length + 2);
rules[temp_len].left = "X"; rules[temp_len].right = "d";
rules[temp_len+1].left = "Y"; rules[temp_len+1].right = "a";
rules = new_char(rules, "Y", 'B', "Z1");
rules = new_char(rules, "Y", 'X', "Z2");
rules = new_char(rules, "A", 'B', "Z3");
rules = new_char(rules, "Z1", 'X', "Z4");
return rules;
}
//*************************************************************************
void print(Rule* rules) {
for (int i = 0; i < length; i++)
{
cout << " " << i + 1 << "." << rules[i];
}
}
Rule* clean(Rule* rules) {
int cout = 0;
for (int i = 0; i < length; i++)
{
Rule temp = rules[i];
for (int j = i + 1; j < length; j++)
{
if (temp.left == rules[j].left && temp.right == rules[j].right)
{
cout++;
for (int k = j; k < length - 1; k++)
{
rules[k] = rules[k + 1];
}
}
}
}
int clean_pos = length - cout + 1;
length = clean_pos;
Rule* new_rules = new Rule[clean_pos];
for (int i = 0; i < length; i++)
{
new_rules[i] = rules[i];
}
delete[]rules;
return new_rules;
}
void init(Rule* rules) {
rules[0].left = "S"; rules[0].right = "dB";
rules[1].left = "S"; rules[1].right = "A";
rules[2].left = "A"; rules[2].right = "d";
rules[3].left = "A"; rules[3].right = "dS";
rules[4].left = "A"; rules[4].right = "aBdAB";
rules[5].left = "B"; rules[5].right = "a";
rules[6].left = "B"; rules[6].right = "dA";
rules[7].left = "B"; rules[7].right = "A";
rules[8].left = "B"; rules[8].right = "eps";
rules[9].left = "C"; rules[9].right = "Aa";
}
int main()
{
setlocale(LC_ALL, "ru");
//выделение памяти
Rule* rules = new Rule[length];
//заполнение массива объектов
init(rules);
//вывод на экран
cout << "Rules:" << endl;
print(rules);
rules = delete_eps(rules);
//вывод на экран
cout << "\nRules (после удаления эпсилон продукции):" << endl;
print(rules);
cout << endl;
rules = delete_chain_rules(rules);
rules = clean(rules);
//вывод на экран
cout << "\nRules (после удаления цепных правил):" << endl;
print(rules);
rules = accessibleness(rules);
cout << "\nRules (после удаления непродуктивных символов):" << endl;
print(rules);
rules = Homsk(rules);
cout << "\nHomski" << endl;
print(rules);
}