-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPDA.cpp
219 lines (200 loc) · 9.02 KB
/
PDA.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
//
// Created by Cédric Leclercq on 01/11/2021.
//
#include "PDA.h"
#include "Utils.h"
#include <iostream> // TODO remove not needed
using namespace std;
using json = nlohmann::json;
bool comparePairWords2(const pair<string,vector<string>>& a, const pair<string,vector<string>>& b) {
if (a.first == b.first) {
string one;
for (const auto& elem: a.second)
one += elem;
string two;
for (const auto& elem: b.second)
two += elem;
return one < two;
}
return a.first < b.first;
}
void PDA::parser(const json &j) {
// Setting new Alphabets
vector<string> nAlphabet = j["Alphabet"];
this->alphabet = nAlphabet;
vector<string> nStackAlphabet = j["StackAlphabet"];
this->stackAlphabet = nStackAlphabet;
// Creating new states and assigning them their transitions
for (const auto& state: j["States"]) {
auto *newState = new State(state, false, false);
this->states.push_back(newState);
}
// Assigning all the transitions to the states
for (auto state: this->states) {
for (auto transition: j["Transitions"]) {
State * nFrom = findState(transition["from"]);
State * nTo = findState(transition["to"]);
if (nFrom == state or nTo == state) {
string nStackTop = transition["stacktop"];
string nInput = transition["input"];
vector<string> nReplacement = transition["replacement"];
auto *nTransition = new Transition(nFrom, nTo, nInput, nStackTop, nReplacement);
this->allTransitions.push_back(nTransition);
if (nFrom == state)
state->addTransition(nTransition);
if (nTo == state)
state->addOrigin(nTransition);
}
}
}
// Setting start state and start stack symbol
auto * nStartState = this->findState(j["StartState"]);
nStartState->setStarting(true);
string nStartStack = j["StartStack"];
this->startState = nStartState;
this->currentState = nStartState;
this->startStack = nStartStack;
}
State * PDA::findState(const string &name) {
for (auto state: this->states) {
if (state->getName() == name)
return state;
}
cerr << "A state was searched that does not exist!" << endl;
cerr << "Problem occurred in: State * PDA::findState(const string &name);" << endl;
return nullptr;
}
CFG PDA::toCFG() {
// Finding all the variables
vector<vector<string>> variablesVector{};
for (auto state: this->states) {
for (const auto& stackSymbol: this->stackAlphabet) {
for (auto stateOne: this->states) {
variablesVector.push_back({state->getName(),stackSymbol,stateOne->getName()});
}
}
}
// Finding all the terminals
vector<string> terminals = this->alphabet;
// Defining productions
vector<pair<string,vector<string>>> productions;
// Finding replacement rules S
for (auto var: variablesVector) {
// Fixing productions for S
if (this->findState(var[0])->getStarting() and this->startStack == var[1]) {
vector<string> result = {Utils::vectorToBracketsString(var)};
productions.emplace_back("S", result);
}
// Fixing other productions
}
// Looping over all transitions
for (auto tr: this->allTransitions) {
for (auto state: this->states) {
vector<string> var = {tr->getFrom()->getName(), tr->getStackTop(), state->getName()};
// The vector pro will be used for everything after the -> ...
vector<string> pro;
// No replacements, we have epsilon
if (tr->getReplacement().empty() and state == tr->getTo()) {
pro = {tr->getInput()};
if (find(productions.begin(),productions.end(),
make_pair(Utils::vectorToBracketsString(var),pro)) == productions.end()) {
productions.emplace_back(Utils::vectorToBracketsString(var),pro);
}
break;
} else {
//productions.emplace_back(Utils::vectorToBracketsString(var), pro);
int initialStateSelect = 0;
for (auto itB = 0; itB < tr->getReplacement().size(); itB++) {
if (initialStateSelect >= this->states.size()) {
initialStateSelect = 0;
}
int stateSelect = initialStateSelect;
pro = {tr->getInput()};
State * prev = tr->getTo();
for (auto itA = 0; itA < tr->getReplacement().size(); itA++) {
// Doing this for selecting states, but preventing selecting states that don't exist
if (stateSelect >= this->states.size())
stateSelect = 0;
vector<string> proElem;
if (itA + 1 == tr->getReplacement().size())
proElem = {prev->getName(), tr->getReplacement()[itA], state->getName()};
else proElem = {prev->getName(), tr->getReplacement()[itA], this->states[stateSelect]->getName()};
prev = this->states[stateSelect];
pro.emplace_back(Utils::vectorToBracketsString(proElem));
stateSelect++;
}
initialStateSelect++;
productions.emplace_back(Utils::vectorToBracketsString(var), pro);
}
/*
for (auto toGo: this->states) {
pro = {tr->getInput()};
for (auto rep: tr->getReplacement()) {
vector<string> proElem = {tr->getTo()->getName(),tr->getStackTop(),toGo->getName()};
pro.emplace_back(Utils::vectorToBracketsString(proElem));
}
productions.emplace_back(Utils::vectorToBracketsString(var),pro);
}
// Now making the more fancy productions
*/
/*
* At this point, we have our var, so the first part of the -> ...
* We only need to calc the second part
*/
// TODO productions.emplace_back(...)
}
}
}
sort(productions.begin(),productions.end(), comparePairWords2); // TODO change that the empty string is last
/*
for (auto tr: this->allTransitions) {
State * prev = tr->getTo(); // Previously used state, needed for the algorithm (see later)
if (not tr->getReplacement().empty()) {
for (auto state: this->states) { // We need to iterate over all the (q,x,**STATES**) and create all the productions
vector<string> var;
if (state == this->states.front()) {
var = {tr->getFrom()->getName(), tr->getStackTop(), tr->getTo()->getName()}; // variable
prev = tr->getTo();
} else {
var = {prev->getName(), tr->getStackTop(), state->getName()}; // variable
prev = state;
}
// everything after the var -> ... . Starts with its input symbol (as it should)
vector<string> pro = {tr->getInput()};
productions.emplace_back(Utils::vectorToBracketsString(var), pro);
}
} else {
cerr << "here" << endl;
}
}
*/
/*
for (auto transition: this->allTransitions) {
for (auto state: this->states) {
vector<string> replacements = transition->getReplacement();
string temp = "[" + transition->getFrom()->getName() + ","+ transition->getStackTop() + "," + transition->getTo()->getName() + "]";
vector<string> result = {transition->getInput()};
for (int i = 0; i < this->states.size() and i < transition->getReplacement().size(); i++) {
vector<string> vectorReplacement;
if (replacements[i] != replacements.back()) {
vectorReplacement = {transition->getTo()->getName(), replacements[i],
this->states[i]->getName()};
} else if (not transition->getReplacement().empty()) {
vectorReplacement = {this->states[i]->getName(),replacements.back(),transition->getTo()->getName()};
}
string replacement = Utils::vectorToBracketsString(vectorReplacement);
result.push_back(replacement);
}
productions.emplace_back(temp,result);
}
}
*/
// Parsing var vector
vector<string> variables = {"S"};
variables.reserve(variablesVector.size());
for (const auto& item: variablesVector)
variables.push_back(Utils::vectorToBracketsString(item));
//auto * cfg = new CFG(variables,terminals,productions,"S");
CFG cfg(variables,terminals,productions,"S");
return cfg;
}