-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_8.cpp
More file actions
88 lines (75 loc) · 2.32 KB
/
5_8.cpp
File metadata and controls
88 lines (75 loc) · 2.32 KB
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
#include <algorithm>
#include <iostream>
#include <vector>
#include "5_5.cpp"
// Construction of V_k_G
V_k_G GOTO(V_k_G A_old, symbol X, Grammar G, size_t k) {
V_k_G A;
A.active_prefix = A_old.active_prefix;
A.active_prefix.str_.push_back(X);
for (auto elem : A_old.set) {
if (elem.rule.right_part.str_.size() && elem.rule.right_part.str_[elem.point].name_ == X.name_) { // может быть сег фолт
A.set.insert({elem.rule, elem.point + 1, elem.u});
}
}
V_k_G A_prev = A;
while (true) {
for (auto elem : A_prev.set) {
if (elem.rule.right_part.str_.size() && elem.rule.right_part.str_[elem.point].type_) {
for (auto rule : G.rules) {
if (elem.rule.right_part.str_[elem.point].name_ == rule.left_part) {
sentential_form alpha_u = elem.rule.right_part;
alpha_u.str_.erase(alpha_u.str_.begin(), alpha_u.str_.begin() + elem.point + 1);
for (size_t i = 0; i < elem.u.size(); i++) {
alpha_u.str_.push_back({elem.u[i], false});
}
First_k f = FIRST(k, alpha_u, G);
for (auto fir : f.set) {
A.set.insert({rule, 0, fir});
}
}
}
}
}
if (A.set.size() == A_prev.set.size()) {
break;
}
A_prev = A;
}
return A;
}
V_k_G V_eps(Grammar G, size_t k) {
V_k_G A;
A.active_prefix = {};
for (CFL_rule elem : G.rules) {
if (elem.left_part == G.start) {
LR_k_situation s = {elem, 0, std::vector<char>()};
A.set.insert(s);
}
}
V_k_G A_prev = A;
while (true) {
for (auto elem : A_prev.set) {
if (elem.rule.right_part.str_.size() && elem.rule.right_part.str_[0].type_) {
for (auto rule : G.rules) {
if (elem.rule.right_part.str_[0].name_ == rule.left_part) {
sentential_form alpha_u = elem.rule.right_part;
alpha_u.str_.erase(alpha_u.str_.begin());
for (size_t i = 0; i < elem.u.size(); i++) {
alpha_u.str_.push_back({elem.u[i], false});
}
First_k f = FIRST(k, alpha_u, G);
for (auto fir : f.set) {
A.set.insert({rule, 0, fir});
}
}
}
}
}
if (A.set.size() == A_prev.set.size()) {
break;
}
A_prev = A;
}
return A;
}