This commit is contained in:
MaxanRus 2021-10-05 16:11:24 +03:00
parent 32f8a3dcc6
commit 1fd4158840
22 changed files with 175 additions and 574 deletions

View file

@ -18,14 +18,12 @@ set(CMAKE_CXX_STANDARD 17)
set(SOURCE_FILES set(SOURCE_FILES
src/regular/RegularTree.cpp src/regular/RegularTree.cpp
src/regular/RegularTreeNode.cpp src/regular/RegularTreeNode.cpp
src/NFA/NFATree.cpp src/NFA/NFAGraph.cpp
src/NFA/NFATreeVertex.cpp src/NFA/NFATreeVertex.cpp
src/converters/RegularToNFA.cpp src/converters/RegularToNFA.cpp
src/converters/NFAToDFA.cpp src/converters/NFAToDFA.cpp
src/DFA/DFAGraph.cpp src/DFA/DFAGraph.cpp
src/DFA/DFAGraphVertex.cpp src/DFA/DFAGraphVertex.cpp
# src/FDFA/FDFAGraph.cpp
# src/FDFA/FDFAGraphVertex.cpp
src/converters/DFAToFDFA.cpp src/converters/DFAToFDFA.cpp
src/converters/DFAToMinDFA.cpp src/converters/DFAToMinDFA.cpp
src/converters/DFAToRegular.cpp src/converters/DFAToRegular.cpp

View file

@ -1,67 +0,0 @@
#pragma once
#include <map>
#include <vector>
#include <set>
#include <memory>
namespace FDFA {
class FDFAGraph {
public:
class Vertex {
public:
Vertex(FDFAGraph* owner);
bool IsFinal() const;
bool IsStart() const;
size_t GetNumber() const;
const std::map<char, size_t>& GetTransitions() const;
const std::map<char, size_t>& GetBackTransitions() const;
void AddEdge(char, size_t);
void RemoveEdge(char);
void SetOwner(FDFAGraph* owner);
void SetFinal(bool status);
void SetStart(bool status);
private:
FDFAGraph* owner_;
std::map<char, size_t> transitions_;
std::map<char, size_t> back_transitions_;
size_t number_;
bool is_final_ = false;
bool is_start_ = false;
friend class FDFAGraph;
};
FDFAGraph() = default;
FDFAGraph(const FDFAGraph&) = delete;
FDFAGraph(FDFAGraph&&) = default;
FDFAGraph& operator=(const FDFAGraph&) = delete;
FDFAGraph& operator=(FDFAGraph&&) = default;
size_t AddNewVertex();
void AddFinalVertex(size_t number);
void AddStartVertex(size_t number);
void RemoveVertex(size_t number);
void RemoveFinalVertex(size_t number);
void RemoveStartVertex(size_t number);
std::shared_ptr<Vertex> GetVertex(size_t number);
size_t GetCountVertexes() const;
const std::vector<size_t>& GetFinalVertexes() const;
const std::vector<size_t>& GetStartVertexes() const;
void Print() const;
bool Accepted(const std::string&) const;
private:
size_t count_vertexes_ = 0;
std::map<size_t, std::shared_ptr<Vertex>> vertexes_;
std::vector<size_t> final_vertexes_;
std::vector<size_t> start_vertexes_;
};
}

View file

@ -6,11 +6,11 @@
#include <memory> #include <memory>
namespace NFA { namespace NFA {
class NFATree { class NFAGraph {
public: public:
class Vertex { class Vertex {
public: public:
Vertex(NFATree* owner); Vertex(NFAGraph* owner);
bool IsFinal() const; bool IsFinal() const;
bool IsStart() const; bool IsStart() const;
@ -21,11 +21,11 @@ class NFATree {
void AddEdge(char, size_t); void AddEdge(char, size_t);
void RemoveEdge(char, size_t); void RemoveEdge(char, size_t);
void SetOwner(NFATree* owner); void SetOwner(NFAGraph* owner);
void SetFinal(bool status); void SetFinal(bool status);
void SetStart(bool status); void SetStart(bool status);
private: private:
NFATree* owner_; NFAGraph* owner_;
std::map<char, std::set<size_t>> transitions_; std::map<char, std::set<size_t>> transitions_;
std::map<char, std::set<size_t>> back_transitions_; std::map<char, std::set<size_t>> back_transitions_;
@ -33,15 +33,15 @@ class NFATree {
bool is_final_ = false; bool is_final_ = false;
bool is_start_ = false; bool is_start_ = false;
friend class NFATree; friend class NFAGraph;
}; };
NFATree() = default; NFAGraph() = default;
NFATree(const NFATree&) = delete; NFAGraph(const NFAGraph&) = delete;
NFATree(NFATree&&); NFAGraph(NFAGraph&&);
NFATree& operator=(const NFATree&) = delete; NFAGraph& operator=(const NFAGraph&) = delete;
NFATree& operator=(NFATree&&); NFAGraph& operator=(NFAGraph&&);
size_t AddNewVertex(); size_t AddNewVertex();
void AddFinalVertex(size_t number); void AddFinalVertex(size_t number);
@ -51,7 +51,7 @@ class NFATree {
void RemoveFinalVertex(size_t number); void RemoveFinalVertex(size_t number);
void RemoveStartVertex(size_t number); void RemoveStartVertex(size_t number);
void Composition(NFATree&&, void Composition(NFAGraph&&,
std::vector<size_t> start_vertexes, std::vector<size_t> start_vertexes,
std::vector<size_t> final_vertexes); std::vector<size_t> final_vertexes);

View file

@ -1,10 +1,7 @@
#pragma once #pragma once
#include "DFA/DFAGraph.hpp" #include "DFA/DFAGraph.hpp"
#include "FDFA/FDFAGraph.hpp"
namespace converters { namespace converters {
using namespace DFA; using namespace DFA;
using namespace FDFA;
DFAGraph DFAGraphToFDFAGraph(DFAGraph&&, const std::vector<char>& alphabet); DFAGraph DFAGraphToFDFAGraph(DFAGraph&&, const std::vector<char>& alphabet);
// FDFAGraph DFAGraphToFDFAGraph(DFAGraph&&, const std::vector<char>& alphabet);
} }

View file

@ -3,5 +3,5 @@
namespace converters { namespace converters {
using namespace DFA; using namespace DFA;
DFAGraph DFAToMinDFA(DFAGraph&&); DFAGraph DFAGraphToMinDFAGraph(DFAGraph&&);
} }

View file

@ -1,13 +1,13 @@
#pragma once #pragma once
#include "NFA/NFATree.hpp" #include "NFA/NFAGraph.hpp"
#include "DFA/DFAGraph.hpp" #include "DFA/DFAGraph.hpp"
namespace converters { namespace converters {
using namespace NFA; using namespace NFA;
using namespace DFA; using namespace DFA;
NFATree AddAllEpsilonTransitions(NFATree&&); NFAGraph AddAllEpsilonTransitions(NFAGraph&&);
NFATree AddAllPossibleStartFilnal(NFATree&&); NFAGraph AddAllPossibleStartFinal(NFAGraph&&);
NFATree DeleteEpsilonTransitions(NFATree&&); NFAGraph DeleteEpsilonTransitions(NFAGraph&&);
NFATree DeleteTransitionsByOneLetter(NFATree&&); NFAGraph DeleteTransitionsByOneLetter(NFAGraph&&);
DFAGraph NFATreeToDFAGraph(NFATree&&); DFAGraph NFAGraphToDFAGraph(NFAGraph&&);
} }

View file

@ -1,10 +1,10 @@
#pragma once #pragma once
#include "regular/RegularTree.hpp" #include "regular/RegularTree.hpp"
#include "NFA/NFATree.hpp" #include "NFA/NFAGraph.hpp"
namespace converters { namespace converters {
using namespace NFA; using namespace NFA;
using namespace regular; using namespace regular;
NFATree RegularToNFA(RegularTree&&); NFAGraph RegularToNFAGraph(RegularTree&&);
} }

View file

@ -1,5 +1,4 @@
#pragma once #pragma once
#include <variant>
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <string> #include <string>

View file

@ -1,5 +1,4 @@
#include "DFA/DFAGraph.hpp" #include "DFA/DFAGraph.hpp"
#include <iostream>
using Vertex = DFA::DFAGraph::Vertex; using Vertex = DFA::DFAGraph::Vertex;

View file

@ -1,98 +0,0 @@
#include "FDFA/FDFAGraph.hpp"
#include <algorithm>
#include <iostream>
namespace FDFA {
size_t FDFAGraph::AddNewVertex() {
vertexes_[count_vertexes_] = std::make_shared<Vertex>(this);
vertexes_[count_vertexes_]->number_ = count_vertexes_;
return count_vertexes_++;
}
void FDFAGraph::AddFinalVertex(size_t number) {
if (!vertexes_[number]->is_final_) {
vertexes_[number]->is_final_ = true;
final_vertexes_.push_back(number);
}
}
void FDFAGraph::AddStartVertex(size_t number) {
if (!vertexes_[number]->is_start_) {
vertexes_[number]->is_start_ = true;
start_vertexes_.push_back(number);
}
}
void FDFAGraph::RemoveVertex(size_t number) {
RemoveFinalVertex(number);
RemoveStartVertex(number);
vertexes_.erase(number);
}
void FDFAGraph::RemoveFinalVertex(size_t number) {
for (size_t i = 0; i < final_vertexes_.size(); ++i) {
if (final_vertexes_[i] == number) {
vertexes_[number]->is_final_ = false;
std::swap(final_vertexes_[i], final_vertexes_.back());
final_vertexes_.pop_back();
break;
}
}
}
void FDFAGraph::RemoveStartVertex(size_t number) {
for (size_t i = 0; i < start_vertexes_.size(); ++i) {
if (start_vertexes_[i] == number) {
vertexes_[number]->is_start_ = false;
std::swap(start_vertexes_[i], start_vertexes_.back());
start_vertexes_.pop_back();
break;
}
}
}
std::shared_ptr<FDFAGraph::Vertex> FDFAGraph::GetVertex(size_t number) {
if (vertexes_.count(number))
return vertexes_[number];
return nullptr;
}
size_t FDFAGraph::GetCountVertexes() const {
return count_vertexes_;
}
const std::vector<size_t>& FDFAGraph::GetFinalVertexes() const {
return final_vertexes_;
}
const std::vector<size_t>& FDFAGraph::GetStartVertexes() const {
return start_vertexes_;
}
void FDFAGraph::Print() const {
for (auto i: vertexes_) {
std::cout << i.second->number_ << " " << "f-" << i.second->is_final_ << " s-" <<
i.second->is_start_ << std::endl;
}
for (auto& i: vertexes_) {
for (auto& j: i.second->transitions_) {
std::cout << i.second->number_ << "->" << j.second << " <" << j.first << ">" <<
std::endl;
}
}
std::cout << std::endl;
}
bool FDFAGraph::Accepted(const std::string& str) const {
std::shared_ptr<const Vertex> current = vertexes_.at(start_vertexes_[0]);
for (auto i: str) {
if (current->GetTransitions().count(i)) {
current = vertexes_.at(current->GetTransitions().at(i));
} else {
return false;
}
}
return current->IsFinal();
}
}

View file

@ -1,58 +0,0 @@
#include "FDFA/FDFAGraph.hpp"
#include <iostream>
using Vertex = FDFA::FDFAGraph::Vertex;
Vertex::Vertex(FDFAGraph* owner) : owner_(owner) {}
bool Vertex::IsFinal() const {
return is_final_;
}
bool Vertex::IsStart() const {
return is_start_;
}
size_t Vertex::GetNumber() const {
return number_;
}
const std::map<char, size_t>& Vertex::GetTransitions() const {
return transitions_;
}
const std::map<char, size_t>& Vertex::GetBackTransitions() const {
return back_transitions_;
}
void Vertex::AddEdge(char symbol, size_t number) {
transitions_[symbol] = number;
owner_->GetVertex(number)->back_transitions_[symbol] = number_;
}
void Vertex::RemoveEdge(char symbol) {
owner_->GetVertex(transitions_[symbol])->back_transitions_.erase(symbol);
transitions_.erase(symbol);
}
void Vertex::SetOwner(FDFAGraph* owner) {
owner_ = owner;
}
void Vertex::SetFinal(bool status) {
if (status != is_final_) {
if (status)
owner_->AddFinalVertex(number_);
else
owner_->RemoveFinalVertex(number_);
}
}
void Vertex::SetStart(bool status) {
if (status != is_start_) {
if (status)
owner_->AddStartVertex(number_);
else
owner_->RemoveStartVertex(number_);
}
}

View file

@ -1,9 +1,9 @@
#include "NFA/NFATree.hpp" #include "NFA/NFAGraph.hpp"
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
namespace NFA { namespace NFA {
NFATree::NFATree(NFATree&& another) { NFAGraph::NFAGraph(NFAGraph&& another) {
std::swap(count_vertexes_, another.count_vertexes_); std::swap(count_vertexes_, another.count_vertexes_);
std::swap(vertexes_, another.vertexes_); std::swap(vertexes_, another.vertexes_);
std::swap(final_vertexes_, another.final_vertexes_); std::swap(final_vertexes_, another.final_vertexes_);
@ -15,7 +15,7 @@ NFATree::NFATree(NFATree&& another) {
i.second->owner_ = &another; i.second->owner_ = &another;
} }
NFATree& NFATree::operator=(NFATree&& another) { NFAGraph& NFAGraph::operator=(NFAGraph&& another) {
std::swap(count_vertexes_, another.count_vertexes_); std::swap(count_vertexes_, another.count_vertexes_);
std::swap(vertexes_, another.vertexes_); std::swap(vertexes_, another.vertexes_);
std::swap(final_vertexes_, another.final_vertexes_); std::swap(final_vertexes_, another.final_vertexes_);
@ -28,33 +28,33 @@ NFATree& NFATree::operator=(NFATree&& another) {
return *this; return *this;
} }
size_t NFATree::AddNewVertex() { size_t NFAGraph::AddNewVertex() {
vertexes_[count_vertexes_] = std::make_shared<Vertex>(this); vertexes_[count_vertexes_] = std::make_shared<Vertex>(this);
vertexes_[count_vertexes_]->number_ = count_vertexes_; vertexes_[count_vertexes_]->number_ = count_vertexes_;
return count_vertexes_++; return count_vertexes_++;
} }
void NFATree::AddFinalVertex(size_t number) { void NFAGraph::AddFinalVertex(size_t number) {
if (!vertexes_[number]->is_final_) { if (!vertexes_[number]->is_final_) {
vertexes_[number]->is_final_ = true; vertexes_[number]->is_final_ = true;
final_vertexes_.push_back(number); final_vertexes_.push_back(number);
} }
} }
void NFATree::AddStartVertex(size_t number) { void NFAGraph::AddStartVertex(size_t number) {
if (!vertexes_[number]->is_start_) { if (!vertexes_[number]->is_start_) {
vertexes_[number]->is_start_ = true; vertexes_[number]->is_start_ = true;
start_vertexes_.push_back(number); start_vertexes_.push_back(number);
} }
} }
void NFATree::RemoveVertex(size_t number) { void NFAGraph::RemoveVertex(size_t number) {
RemoveFinalVertex(number); RemoveFinalVertex(number);
RemoveStartVertex(number); RemoveStartVertex(number);
vertexes_.erase(number); vertexes_.erase(number);
} }
void NFATree::RemoveFinalVertex(size_t number) { void NFAGraph::RemoveFinalVertex(size_t number) {
for (size_t i = 0; i < final_vertexes_.size(); ++i) { for (size_t i = 0; i < final_vertexes_.size(); ++i) {
if (final_vertexes_[i] == number) { if (final_vertexes_[i] == number) {
vertexes_[number]->is_final_ = false; vertexes_[number]->is_final_ = false;
@ -65,7 +65,7 @@ void NFATree::RemoveFinalVertex(size_t number) {
} }
} }
void NFATree::RemoveStartVertex(size_t number) { void NFAGraph::RemoveStartVertex(size_t number) {
for (size_t i = 0; i < start_vertexes_.size(); ++i) { for (size_t i = 0; i < start_vertexes_.size(); ++i) {
if (start_vertexes_[i] == number) { if (start_vertexes_[i] == number) {
vertexes_[number]->is_start_ = false; vertexes_[number]->is_start_ = false;
@ -76,15 +76,15 @@ void NFATree::RemoveStartVertex(size_t number) {
} }
} }
void NFATree::Composition(NFATree&& tree, void NFAGraph::Composition(NFAGraph&& nfa_graph,
std::vector<size_t> start_vertexes, std::vector<size_t> start_vertexes,
std::vector<size_t> final_vertexes) { std::vector<size_t> final_vertexes) {
auto add_final_vertexes = tree.final_vertexes_; auto add_final_vertexes = nfa_graph.final_vertexes_;
auto add_start_vertexes = tree.start_vertexes_; auto add_start_vertexes = nfa_graph.start_vertexes_;
size_t new_count_vertexes = count_vertexes_; size_t new_count_vertexes = count_vertexes_;
for (auto& i: tree.vertexes_) { for (auto& i: nfa_graph.vertexes_) {
new_count_vertexes = std::max(new_count_vertexes, i.first + count_vertexes_ + 1); new_count_vertexes = std::max(new_count_vertexes, i.first + count_vertexes_ + 1);
i.second->number_ += count_vertexes_; i.second->number_ += count_vertexes_;
std::map<char, std::set<size_t>> new_transitions; std::map<char, std::set<size_t>> new_transitions;
@ -135,25 +135,25 @@ void NFATree::Composition(NFATree&& tree,
} }
} }
std::shared_ptr<NFATree::Vertex> NFATree::GetVertex(size_t number) { std::shared_ptr<NFAGraph::Vertex> NFAGraph::GetVertex(size_t number) {
if (vertexes_.count(number)) if (vertexes_.count(number))
return vertexes_[number]; return vertexes_[number];
return nullptr; return nullptr;
} }
size_t NFATree::GetCountVertexes() const { size_t NFAGraph::GetCountVertexes() const {
return count_vertexes_; return count_vertexes_;
} }
const std::vector<size_t>& NFATree::GetFinalVertexes() const { const std::vector<size_t>& NFAGraph::GetFinalVertexes() const {
return final_vertexes_; return final_vertexes_;
} }
const std::vector<size_t>& NFATree::GetStartVertexes() const { const std::vector<size_t>& NFAGraph::GetStartVertexes() const {
return start_vertexes_; return start_vertexes_;
} }
void NFATree::Print() const { void NFAGraph::Print() const {
for (auto i: vertexes_) { for (auto i: vertexes_) {
std::cout << i.second->number_ << " " << "f-" << i.second->is_final_ << " s-" << std::cout << i.second->number_ << " " << "f-" << i.second->is_final_ << " s-" <<
i.second->is_start_ << std::endl; i.second->is_start_ << std::endl;

View file

@ -1,9 +1,8 @@
#include "NFA/NFATree.hpp" #include "NFA/NFAGraph.hpp"
#include <iostream>
using Vertex = NFA::NFATree::Vertex; using Vertex = NFA::NFAGraph::Vertex;
Vertex::Vertex(NFATree* owner) : owner_(owner) {} Vertex::Vertex(NFAGraph* owner) : owner_(owner) {}
bool Vertex::IsFinal() const { bool Vertex::IsFinal() const {
return is_final_; return is_final_;
@ -40,7 +39,7 @@ void Vertex::RemoveEdge(char symbol, size_t number) {
owner_->GetVertex(number)->back_transitions_.erase(symbol); owner_->GetVertex(number)->back_transitions_.erase(symbol);
} }
void Vertex::SetOwner(NFATree* owner) { void Vertex::SetOwner(NFAGraph* owner) {
owner_ = owner; owner_ = owner;
} }

View file

@ -43,48 +43,4 @@ DFAGraph DFAGraphToFDFAGraph(DFAGraph&& graph, const std::vector<char>& alphabet
return result; return result;
} }
/*
FDFAGraph DFAGraphToFDFAGraph(DFAGraph&& graph, const std::vector<char>& alphabet) {
FDFAGraph result;
const int n = graph.GetCountVertexes();
std::map<size_t, size_t> number_vertex_in_DFA;
for (int i = 0; i < n; ++i) {
if (!graph.GetVertex(i)) continue;
number_vertex_in_DFA[i] = result.AddNewVertex();
}
for (int i = 0; i < n; ++i) {
if (!graph.GetVertex(i)) continue;
if (graph.GetVertex(i)->IsFinal())
result.GetVertex(number_vertex_in_DFA[i])->SetFinal(true);
if (graph.GetVertex(i)->IsStart())
result.GetVertex(number_vertex_in_DFA[i])->SetStart(true);
const auto& transitions = graph.GetVertex(i)->GetTransitions();
for (const auto& t: transitions) {
result.GetVertex(number_vertex_in_DFA[i])->AddEdge(t.first, t.second);
}
}
size_t drain = result.AddNewVertex();
for (int i = 0; i < n; ++i) {
const auto& transitions = graph.GetVertex(i)->GetTransitions();
for (char j: alphabet) {
if (!transitions.count(j)) {
result.GetVertex(i)->AddEdge(j, drain);
}
}
}
if (result.GetVertex(drain)->GetBackTransitions().size() == 0) {
result.RemoveVertex(drain);
} else {
for (char j: alphabet) {
result.GetVertex(drain)->AddEdge(j, drain);
}
}
return result;
}
*/
} }

View file

@ -1,22 +1,20 @@
#include "converters/DFAToMinDFA.hpp" #include "converters/DFAToMinDFA.hpp"
#include <cstdlib>
#include <iostream>
namespace converters { namespace converters {
DFAGraph DFAToMinDFA(DFAGraph&& graph) { DFAGraph DFAGraphToMinDFAGraph(DFAGraph&& graph) {
const int n = graph.GetCountVertexes(); const int n = graph.GetCountVertexes();
DFAGraph result; DFAGraph result;
std::vector<char> alphabet; std::vector<char> alphabet;
{ {
std::set<char> salphabet; std::set<char> set_alphabet;
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
for (auto i: graph.GetVertex(i)->GetTransitions()) { for (auto i: graph.GetVertex(i)->GetTransitions()) {
salphabet.insert(i.first); set_alphabet.insert(i.first);
} }
} }
alphabet = std::vector<char>(salphabet.begin(), salphabet.end()); alphabet = std::vector<char>(set_alphabet.begin(), set_alphabet.end());
} }
std::vector<std::vector<std::vector<size_t>>> table(n, std::vector<std::vector<std::vector<size_t>>> table(n,

View file

@ -1,6 +1,4 @@
#include "converters/DFAToRegular.hpp" #include "converters/DFAToRegular.hpp"
#include <cstdlib>
#include <iostream>
namespace converters { namespace converters {
std::string DFAGraphToRegular(DFAGraph&& graph) { std::string DFAGraphToRegular(DFAGraph&& graph) {

View file

@ -1,135 +1,22 @@
#include "converters/NFAToDFA.hpp" #include "converters/NFAToDFA.hpp"
#include <functional> #include <functional>
#include <iostream>
#include <queue> #include <queue>
#include <stdexcept>
namespace converters { namespace converters {
/* NFAGraph AddAllEpsilonTransitions(NFAGraph&& nfa_graph) {
OLD version const int n = nfa_graph.GetCountVertexes();
NFATree AddAllEpsilonTransitions(NFATree&& tree) {
// Algorithm from 2-sat
const int n = tree.GetCountVertexes();
std::vector<bool> used(n, false);
std::vector<int> component(n, -1);
std::vector<int> order;
std::function<void(int)> dfs_order = [&tree, &used, &order, &dfs_order](int v) -> void {
used[v] = true;
const auto& transitions = tree.GetVertex(v)->GetTransitions();
if (transitions.count(' ')) {
const auto& s = transitions.at(' ');
for (auto i: s) {
if (!used[i]) {
dfs_order(i);
}
}
}
order.push_back(v);
};
std::function<void(int, int)> dfs_component = [&tree, &component,
&dfs_component](int v, int c) -> void {
component[v] = c;
const auto& transitions = tree.GetVertex(v)->GetBackTransitions();
if (transitions.count(' ')) {
const auto& s = transitions.at(' ');
for (auto i: s) {
if (component[i] == -1) {
dfs_component(i, c);
}
}
}
};
for (int i = 0; i < n; ++i) {
if (!used[i] && tree.GetVertex(i)) {
dfs_order(i);
}
}
int current_component = 0;
for (int i = 0; i < n; ++i) {
int v = order[n - i - 1];
if (component[v] == -1 && tree.GetVertex(i)) {
dfs_component(v, current_component++);
}
}
std::set<std::pair<int, int>> connectivity_components;
used.assign(n, false);
std::vector<std::set<int>> connectivity_from(current_component);
std::function<void(int)> dfs_connectivity = [&tree, &component,
&used, &dfs_connectivity, &connectivity_from](int v) -> void {
used[v] = true;
const auto& transitions = tree.GetVertex(v)->GetTransitions();
if (transitions.count(' ')) {
const auto& s = transitions.at(' ');
for (auto i: s) {
if (!used[i]) {
dfs_connectivity(i);
}
if (component[v] != component[i]) {
connectivity_from[component[v]].insert(connectivity_from[component[i]].begin(),
connectivity_from[component[i]].end());
connectivity_from[component[v]].insert(component[i]);
}
}
}
};
for (int i = 0; i < n; ++i) {
if (!tree.GetVertex(i)) continue;
connectivity_components.emplace(component[i], component[i]);
if (!used[i]) {
dfs_connectivity(i);
}
for (int j: connectivity_from[component[i]]) {
std::cout << i << " " << j << std::endl;
connectivity_components.emplace(component[i], j);
}
}
for (int i = 0; i < n; ++i) {
std::cout << component[i] << " ";
}
std::cout << std::endl;
for (int i: connectivity_from[1]) {
std::cout << i << " ";
}
std::cout << std::endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i == j) continue;
if (connectivity_components.count(std::make_pair(component[i], component[j]))) {
tree.GetVertex(i)->AddEdge(' ', j);
}
}
}
return std::move(tree);
}
*/
NFATree AddAllEpsilonTransitions(NFATree&& tree) {
const int n = tree.GetCountVertexes();
std::vector<bool> used(n, false); std::vector<bool> used(n, false);
std::function<void(int, int)> dfs = [&tree, &used, &dfs](int u, int v) -> void { std::function<void(int, int)> dfs = [&nfa_graph, &used, &dfs](int u, int v) -> void {
used[v] = true; used[v] = true;
const auto& transitions = tree.GetVertex(v)->GetTransitions(); const auto& transitions = nfa_graph.GetVertex(v)->GetTransitions();
if (transitions.count(' ')) { if (transitions.count(' ')) {
const auto& s = transitions.at(' '); const auto& s = transitions.at(' ');
for (auto i: s) { for (auto i: s) {
if (!used[i]) { if (!used[i]) {
if (u != i) if (u != i)
tree.GetVertex(u)->AddEdge(' ', i); nfa_graph.GetVertex(u)->AddEdge(' ', i);
dfs(u, i); dfs(u, i);
} }
} }
@ -137,62 +24,62 @@ NFATree AddAllEpsilonTransitions(NFATree&& tree) {
}; };
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
if (!tree.GetVertex(i)) continue; if (!nfa_graph.GetVertex(i)) continue;
used.assign(n, false); used.assign(n, false);
dfs(i, i); dfs(i, i);
} }
return std::move(tree); return std::move(nfa_graph);
} }
NFATree AddAllPossibleStartFilnal(NFATree&& tree) { NFAGraph AddAllPossibleStartFinal(NFAGraph&& nfa_graph) {
if (tree.GetStartVertexes().size() != 1) { if (nfa_graph.GetStartVertexes().size() != 1) {
size_t start_vertex = tree.AddNewVertex(); size_t start_vertex = nfa_graph.AddNewVertex();
for (auto v: tree.GetStartVertexes()) { for (auto v: nfa_graph.GetStartVertexes()) {
tree.GetVertex(start_vertex)->AddEdge(' ', v); nfa_graph.GetVertex(start_vertex)->AddEdge(' ', v);
} }
} }
for (const auto& v: tree.GetFinalVertexes()) { for (const auto& v: nfa_graph.GetFinalVertexes()) {
const auto& transitions = tree.GetVertex(v)->GetBackTransitions(); const auto& transitions = nfa_graph.GetVertex(v)->GetBackTransitions();
if (transitions.count(' ')) { if (transitions.count(' ')) {
const auto& s = transitions.at(' '); const auto& s = transitions.at(' ');
for (auto i: s) { for (auto i: s) {
tree.GetVertex(i)->SetFinal(true); nfa_graph.GetVertex(i)->SetFinal(true);
} }
} }
} }
return std::move(tree); return std::move(nfa_graph);
} }
NFATree DeleteEpsilonTransitions(NFATree&& tree) { NFAGraph DeleteEpsilonTransitions(NFAGraph&& nfa_graph) {
const int n = tree.GetCountVertexes(); const int n = nfa_graph.GetCountVertexes();
for (int v = 0; v < n; ++v) { for (int v = 0; v < n; ++v) {
if (!tree.GetVertex(v)) continue; if (!nfa_graph.GetVertex(v)) continue;
const auto& transitions = tree.GetVertex(v)->GetTransitions(); const auto& transitions = nfa_graph.GetVertex(v)->GetTransitions();
if (transitions.count(' ')) { if (transitions.count(' ')) {
auto s = transitions.at(' '); auto s = transitions.at(' ');
for (auto u: s) { for (auto u: s) {
for (auto& i: tree.GetVertex(u)->GetTransitions()) { for (auto& i: nfa_graph.GetVertex(u)->GetTransitions()) {
if (i.first == ' ') continue; if (i.first == ' ') continue;
for (auto t: i.second) { for (auto t: i.second) {
tree.GetVertex(v)->AddEdge(i.first, t); nfa_graph.GetVertex(v)->AddEdge(i.first, t);
} }
} }
} }
for (auto u: s) { for (auto u: s) {
tree.GetVertex(v)->RemoveEdge(' ', u); nfa_graph.GetVertex(v)->RemoveEdge(' ', u);
} }
} }
} }
return std::move(tree); return std::move(nfa_graph);
} }
NFATree DeleteTransitionsByOneLetter(NFATree&& tree) { NFAGraph DeleteTransitionsByOneLetter(NFAGraph&& nfa_graph) {
const int n = tree.GetCountVertexes(); const int n = nfa_graph.GetCountVertexes();
NFATree result_tree; NFAGraph result_tree;
std::map<std::pair<std::set<size_t>, char>, std::set<size_t>> transitions; std::map<std::pair<std::set<size_t>, char>, std::set<size_t>> transitions;
std::map<std::set<size_t>, size_t> number_vertex_in_result_tree; std::map<std::set<size_t>, size_t> number_vertex_in_result_tree;
@ -200,14 +87,14 @@ NFATree DeleteTransitionsByOneLetter(NFATree&& tree) {
std::set<char> alphabet; std::set<char> alphabet;
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
if (!tree.GetVertex(i)) continue; if (!nfa_graph.GetVertex(i)) continue;
for (const auto& j: tree.GetVertex(i)->GetTransitions()) { for (const auto& j: nfa_graph.GetVertex(i)->GetTransitions()) {
if (j.second.size() > 0) if (j.second.size() > 0)
alphabet.insert(j.first); alphabet.insert(j.first);
} }
} }
for (auto i: tree.GetStartVertexes()) { for (auto i: nfa_graph.GetStartVertexes()) {
queue.push({i}); queue.push({i});
number_vertex_in_result_tree[{i}] = result_tree.AddNewVertex(); number_vertex_in_result_tree[{i}] = result_tree.AddNewVertex();
} }
@ -219,7 +106,7 @@ NFATree DeleteTransitionsByOneLetter(NFATree&& tree) {
for (auto symbol: alphabet) { for (auto symbol: alphabet) {
std::set<size_t> result; std::set<size_t> result;
for (auto v: current) { for (auto v: current) {
const auto& transitions = tree.GetVertex(v)->GetTransitions(); const auto& transitions = nfa_graph.GetVertex(v)->GetTransitions();
if (transitions.count(symbol)) { if (transitions.count(symbol)) {
const auto& s = transitions.at(symbol); const auto& s = transitions.at(symbol);
result.insert(s.begin(), s.end()); result.insert(s.begin(), s.end());
@ -248,9 +135,9 @@ NFATree DeleteTransitionsByOneLetter(NFATree&& tree) {
auto v = i.second; auto v = i.second;
for (auto i: s) { for (auto i: s) {
if (tree.GetVertex(i)->IsFinal()) if (nfa_graph.GetVertex(i)->IsFinal())
result_tree.GetVertex(v)->SetFinal(true); result_tree.GetVertex(v)->SetFinal(true);
if (tree.GetVertex(i)->IsStart()) if (nfa_graph.GetVertex(i)->IsStart())
result_tree.GetVertex(v)->SetStart(true); result_tree.GetVertex(v)->SetStart(true);
} }
} }
@ -258,31 +145,25 @@ NFATree DeleteTransitionsByOneLetter(NFATree&& tree) {
return result_tree; return result_tree;
} }
DFAGraph NFATreeToDFAGraph(NFATree&& tree) { DFAGraph NFAGraphToDFAGraph(NFAGraph&& nfa_graph) {
/* nfa_graph = DeleteTransitionsByOneLetter(DeleteEpsilonTransitions(AddAllPossibleStartFinal(
tree = AddAllEpsilonTransitions(std::move(tree)); AddAllEpsilonTransitions(std::move(nfa_graph)))));
tree = AddAllPossibleStartFilnal(std::move(tree));
tree = DeleteEpsilonTransitions(std::move(tree));
tree = DeleteTransitionsByOneLetter(std::move(tree));
*/
tree = DeleteTransitionsByOneLetter(DeleteEpsilonTransitions(AddAllPossibleStartFilnal(
AddAllEpsilonTransitions(std::move(tree)))));
const int n = tree.GetCountVertexes(); const int n = nfa_graph.GetCountVertexes();
DFAGraph result; DFAGraph result;
std::map<int, int> number_vertex_in_DFA; std::map<int, int> number_vertex_in_DFA;
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
if (!tree.GetVertex(i)) continue; if (!nfa_graph.GetVertex(i)) continue;
number_vertex_in_DFA[i] = result.AddNewVertex(); number_vertex_in_DFA[i] = result.AddNewVertex();
} }
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
if (!tree.GetVertex(i)) continue; if (!nfa_graph.GetVertex(i)) continue;
if (tree.GetVertex(i)->IsFinal()) if (nfa_graph.GetVertex(i)->IsFinal())
result.GetVertex(number_vertex_in_DFA[i])->SetFinal(true); result.GetVertex(number_vertex_in_DFA[i])->SetFinal(true);
if (tree.GetVertex(i)->IsStart()) if (nfa_graph.GetVertex(i)->IsStart())
result.GetVertex(number_vertex_in_DFA[i])->SetStart(true); result.GetVertex(number_vertex_in_DFA[i])->SetStart(true);
const auto& transitions = tree.GetVertex(i)->GetTransitions(); const auto& transitions = nfa_graph.GetVertex(i)->GetTransitions();
for (const auto& t: transitions) { for (const auto& t: transitions) {
if (t.second.size() != 1) throw std::logic_error(""); if (t.second.size() != 1) throw std::logic_error("");
result.GetVertex(number_vertex_in_DFA[i])->AddEdge(t.first, *t.second.begin()); result.GetVertex(number_vertex_in_DFA[i])->AddEdge(t.first, *t.second.begin());

View file

@ -1,7 +1,6 @@
#include "converters/RegularToNFA.hpp" #include "converters/RegularToNFA.hpp"
#include "regular/RegularTree.hpp" #include "regular/RegularTree.hpp"
#include "NFA/NFATree.hpp" #include "NFA/NFAGraph.hpp"
#include <iostream>
using namespace NFA; using namespace NFA;
using namespace regular; using namespace regular;
@ -13,8 +12,8 @@ struct do_nothing_deleter{
}; };
template<typename deleter> template<typename deleter>
NFATree RegularToNFA(std::unique_ptr<Node, deleter> node) { NFAGraph RegularToNFA(std::unique_ptr<Node, deleter> node) {
NFATree result; NFAGraph result;
if (node->type == Node::Type::Word) { if (node->type == Node::Type::Word) {
auto end = result.AddNewVertex(); auto end = result.AddNewVertex();
auto start = end; auto start = end;
@ -80,7 +79,7 @@ NFATree RegularToNFA(std::unique_ptr<Node, deleter> node) {
} }
namespace converters { namespace converters {
NFATree RegularToNFA(RegularTree&& tree) { NFAGraph RegularToNFAGraph(RegularTree&& tree) {
const Node& root = tree.GetNode(); const Node& root = tree.GetNode();
return RegularToNFA(std::unique_ptr<Node, return RegularToNFA(std::unique_ptr<Node,
do_nothing_deleter>(&const_cast<Node&>(tree.GetNode()))); do_nothing_deleter>(&const_cast<Node&>(tree.GetNode())));

View file

@ -16,17 +16,17 @@ using namespace converters;
int main() { int main() {
{ {
RegularTree r("a*"); RegularTree r("a*");
NFATree NFA_tree = RegularToNFA(std::move(r)); NFAGraph NFA_tree = RegularToNFAGraph(std::move(r));
DFAGraph DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
DFA_graph.Print(); DFA_graph.Print();
std::string reg = DFAGraphToRegular(std::move(DFA_graph)); std::string reg = DFAGraphToRegular(std::move(DFA_graph));
r = RegularTree(reg); r = RegularTree(reg);
NFA_tree = RegularToNFA(std::move(r)); NFA_tree = RegularToNFAGraph(std::move(r));
DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
DFA_graph.Print(); DFA_graph.Print();
} }
@ -36,16 +36,16 @@ int main() {
{ {
RegularTree r("a+"); RegularTree r("a+");
r.Print(); r.Print();
NFA::NFATree NFA_tree = converters::RegularToNFA(std::move(r)); NFA::NFAGraph NFA_tree = converters::RegularToNFAGraph(std::move(r));
NFA_tree.Print(); NFA_tree.Print();
DFA::DFAGraph DFA_graph = converters::NFATreeToDFAGraph(std::move(NFA_tree)); DFA::DFAGraph DFA_graph = converters::NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph.Print(); DFA_graph.Print();
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
DFA_graph.Print(); DFA_graph.Print();
} }
return 0; return 0;
NFA::NFATree tree; NFA::NFAGraph tree;
const int N = 10; const int N = 10;
int a[N]; int a[N];
@ -110,7 +110,7 @@ int main() {
tree.GetVertex(a[3])->SetFinal(true); tree.GetVertex(a[3])->SetFinal(true);
tree.GetVertex(a[4])->SetFinal(true); tree.GetVertex(a[4])->SetFinal(true);
DFA::DFAGraph res = converters::NFATreeToDFAGraph(std::move(tree)); DFA::DFAGraph res = converters::NFAGraphToDFAGraph(std::move(tree));
// FDFA::FDFAGraph res2 = converters::DFAGraphToFDFAGraph(std::move(res), {'a', 'b'}); // FDFA::FDFAGraph res2 = converters::DFAGraphToFDFAGraph(std::move(res), {'a', 'b'});
// res2.Print(); // res2.Print();
@ -119,7 +119,7 @@ int main() {
// res = converters::DFAGraphToFDFAGraph(std::move(res), {'a', 'b'}); // res = converters::DFAGraphToFDFAGraph(std::move(res), {'a', 'b'});
res = converters::DFAToMinDFA(std::move(res)); res = converters::DFAGraphToMinDFAGraph(std::move(res));
res.Print(); res.Print();
@ -163,7 +163,7 @@ int main() {
RegularTree reg_tree(str); RegularTree reg_tree(str);
reg_tree.Print(); reg_tree.Print();
auto NFA_tree = converters::RegularToNFA(std::move(reg_tree)); auto NFA_tree = converters::RegularToNFAGraph(std::move(reg_tree));
std::cout << std::endl; std::cout << std::endl;

View file

@ -17,16 +17,16 @@ extern std::string GenerateRandomString(std::vector<char> alphabet, size_t len);
TEST(DFA_to_regular, a_star) { TEST(DFA_to_regular, a_star) {
RegularTree r("a*"); RegularTree r("a*");
NFATree NFA_tree = RegularToNFA(std::move(r)); NFAGraph NFA_tree = RegularToNFAGraph(std::move(r));
DFAGraph DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
std::string reg = DFAGraphToRegular(std::move(DFA_graph)); std::string reg = DFAGraphToRegular(std::move(DFA_graph));
r = RegularTree(reg); r = RegularTree(reg);
NFA_tree = RegularToNFA(std::move(r)); NFA_tree = RegularToNFAGraph(std::move(r));
DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
std::string s = ""; std::string s = "";
@ -40,16 +40,16 @@ TEST(DFA_to_regular, a_plus) {
std::string regulars[] = {"a+", "(a)+", "(a+)"}; std::string regulars[] = {"a+", "(a)+", "(a+)"};
for (const auto& regular: regulars) { for (const auto& regular: regulars) {
RegularTree r(regular); RegularTree r(regular);
NFATree NFA_tree = RegularToNFA(std::move(r)); NFAGraph NFA_tree = RegularToNFAGraph(std::move(r));
DFAGraph DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
std::string reg = DFAGraphToRegular(std::move(DFA_graph)); std::string reg = DFAGraphToRegular(std::move(DFA_graph));
r = RegularTree(reg); r = RegularTree(reg);
NFA_tree = RegularToNFA(std::move(r)); NFA_tree = RegularToNFAGraph(std::move(r));
DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
std::string s = ""; std::string s = "";
ASSERT_EQ(DFA_graph.Accepted(s), false); ASSERT_EQ(DFA_graph.Accepted(s), false);
@ -65,16 +65,16 @@ TEST(DFA_to_regular, abc) {
std::string regulars[] = {"abc"}; std::string regulars[] = {"abc"};
for (const auto& regular: regulars) { for (const auto& regular: regulars) {
RegularTree r(regular); RegularTree r(regular);
NFATree NFA_tree = RegularToNFA(std::move(r)); NFAGraph NFA_tree = RegularToNFAGraph(std::move(r));
DFAGraph DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
std::string reg = DFAGraphToRegular(std::move(DFA_graph)); std::string reg = DFAGraphToRegular(std::move(DFA_graph));
r = RegularTree(reg); r = RegularTree(reg);
NFA_tree = RegularToNFA(std::move(r)); NFA_tree = RegularToNFAGraph(std::move(r));
DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
ASSERT_EQ(DFA_graph.Accepted("abc"), true); ASSERT_EQ(DFA_graph.Accepted("abc"), true);
for (int i = 0; i < 1000; ++i) { for (int i = 0; i < 1000; ++i) {
@ -89,16 +89,16 @@ TEST(DFA_to_regular, a_or_b_or_c) {
std::string regulars[] = {}; std::string regulars[] = {};
for (const auto& regular: regulars) { for (const auto& regular: regulars) {
RegularTree r("a|b|c"); RegularTree r("a|b|c");
NFATree NFA_tree = RegularToNFA(std::move(r)); NFAGraph NFA_tree = RegularToNFAGraph(std::move(r));
DFAGraph DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
std::string reg = DFAGraphToRegular(std::move(DFA_graph)); std::string reg = DFAGraphToRegular(std::move(DFA_graph));
r = RegularTree(reg); r = RegularTree(reg);
NFA_tree = RegularToNFA(std::move(r)); NFA_tree = RegularToNFAGraph(std::move(r));
DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
ASSERT_EQ(DFA_graph.Accepted("a"), true); ASSERT_EQ(DFA_graph.Accepted("a"), true);
ASSERT_EQ(DFA_graph.Accepted("b"), true); ASSERT_EQ(DFA_graph.Accepted("b"), true);
@ -117,16 +117,16 @@ TEST(DFA_to_regular, a_star_or_b_star_or_c_star) {
std::string regulars[] = {"a*|b*|c*", "(a*)|(b*|c*)", "(a*|b*|c*)", "((a*)|(b*)|(c*))"}; std::string regulars[] = {"a*|b*|c*", "(a*)|(b*|c*)", "(a*|b*|c*)", "((a*)|(b*)|(c*))"};
for (const auto& regular: regulars) { for (const auto& regular: regulars) {
RegularTree r(regular); RegularTree r(regular);
NFATree NFA_tree = RegularToNFA(std::move(r)); NFAGraph NFA_tree = RegularToNFAGraph(std::move(r));
DFAGraph DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
std::string reg = DFAGraphToRegular(std::move(DFA_graph)); std::string reg = DFAGraphToRegular(std::move(DFA_graph));
r = RegularTree(reg); r = RegularTree(reg);
NFA_tree = RegularToNFA(std::move(r)); NFA_tree = RegularToNFAGraph(std::move(r));
DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
auto check = [](const std::string str) { auto check = [](const std::string str) {
std::set<char> s(str.begin(), str.end()); std::set<char> s(str.begin(), str.end());
@ -151,16 +151,16 @@ TEST(DFA_to_regular, a_star_or_b_star_or_c_star) {
TEST(DFA_to_regular, _a_star_or_b_star_or_c_star_plus) { TEST(DFA_to_regular, _a_star_or_b_star_or_c_star_plus) {
RegularTree r("(a*|b*|c*)+"); RegularTree r("(a*|b*|c*)+");
NFATree NFA_tree = RegularToNFA(std::move(r)); NFAGraph NFA_tree = RegularToNFAGraph(std::move(r));
DFAGraph DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
std::string reg = DFAGraphToRegular(std::move(DFA_graph)); std::string reg = DFAGraphToRegular(std::move(DFA_graph));
r = RegularTree(reg); r = RegularTree(reg);
NFA_tree = RegularToNFA(std::move(r)); NFA_tree = RegularToNFAGraph(std::move(r));
DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
auto check = [](const std::string str) { auto check = [](const std::string str) {
std::set<char> s(str.begin(), str.end()); std::set<char> s(str.begin(), str.end());
@ -177,16 +177,16 @@ TEST(DFA_to_regular, _a_star_or_b_star_or_c_star_plus) {
TEST(DFA_to_regular, _a_or_b_or_c_star_plus) { TEST(DFA_to_regular, _a_or_b_or_c_star_plus) {
RegularTree r("(a|b|c*)+"); RegularTree r("(a|b|c*)+");
NFATree NFA_tree = RegularToNFA(std::move(r)); NFAGraph NFA_tree = RegularToNFAGraph(std::move(r));
DFAGraph DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
std::string reg = DFAGraphToRegular(std::move(DFA_graph)); std::string reg = DFAGraphToRegular(std::move(DFA_graph));
r = RegularTree(reg); r = RegularTree(reg);
NFA_tree = RegularToNFA(std::move(r)); NFA_tree = RegularToNFAGraph(std::move(r));
DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
auto check = [](const std::string str) { auto check = [](const std::string str) {
std::set<char> s(str.begin(), str.end()); std::set<char> s(str.begin(), str.end());

View file

@ -1,7 +1,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <random> #include <random>
#include "DFA/DFAGraph.hpp" #include "DFA/DFAGraph.hpp"
#include "NFA/NFATree.hpp" #include "NFA/NFAGraph.hpp"
#include "converters/NFAToDFA.hpp" #include "converters/NFAToDFA.hpp"
using namespace NFA; using namespace NFA;
@ -19,7 +19,7 @@ std::string GenerateRandomString(std::vector<char> alphabet, size_t len) {
} }
TEST(check_equivalence_NFA_to_DFA, 1) { TEST(check_equivalence_NFA_to_DFA, 1) {
NFA::NFATree tree; NFA::NFAGraph tree;
const int N = 10; const int N = 10;
int a[N]; int a[N];
@ -34,7 +34,7 @@ TEST(check_equivalence_NFA_to_DFA, 1) {
tree.GetVertex(a[2])->SetFinal(true); tree.GetVertex(a[2])->SetFinal(true);
tree.GetVertex(a[3])->SetFinal(true); tree.GetVertex(a[3])->SetFinal(true);
DFA::DFAGraph res = converters::NFATreeToDFAGraph(std::move(tree)); DFA::DFAGraph res = converters::NFAGraphToDFAGraph(std::move(tree));
EXPECT_FALSE(res.Accepted("")); EXPECT_FALSE(res.Accepted(""));
EXPECT_TRUE(res.Accepted("a")); EXPECT_TRUE(res.Accepted("a"));
@ -46,7 +46,7 @@ TEST(check_equivalence_NFA_to_DFA, 1) {
} }
TEST(check_equivalence_NFA_to_DFA, 2) { TEST(check_equivalence_NFA_to_DFA, 2) {
NFA::NFATree tree; NFA::NFAGraph tree;
const int N = 10; const int N = 10;
int a[N]; int a[N];
@ -62,7 +62,7 @@ TEST(check_equivalence_NFA_to_DFA, 2) {
tree.GetVertex(a[1])->SetFinal(true); tree.GetVertex(a[1])->SetFinal(true);
tree.GetVertex(a[2])->SetFinal(true); tree.GetVertex(a[2])->SetFinal(true);
DFA::DFAGraph res = converters::NFATreeToDFAGraph(std::move(tree)); DFA::DFAGraph res = converters::NFAGraphToDFAGraph(std::move(tree));
auto check = [](const std::string& str) { auto check = [](const std::string& str) {
if (str.size() > 0) { if (str.size() > 0) {

View file

@ -16,9 +16,9 @@ extern std::string GenerateRandomString(std::vector<char> alphabet, size_t len);
TEST(regular_to_DFA, a_star) { TEST(regular_to_DFA, a_star) {
RegularTree r("a*"); RegularTree r("a*");
NFATree NFA_tree = RegularToNFA(std::move(r)); NFAGraph NFA_tree = RegularToNFAGraph(std::move(r));
DFAGraph DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
std::string s = ""; std::string s = "";
for (int i = 0; i < 100; ++i) { for (int i = 0; i < 100; ++i) {
@ -31,9 +31,9 @@ TEST(regular_to_DFA, a_plus) {
std::string regulars[] = {"a+", "(a)+", "(a+)"}; std::string regulars[] = {"a+", "(a)+", "(a+)"};
for (const auto& regular: regulars) { for (const auto& regular: regulars) {
RegularTree r(regular); RegularTree r(regular);
NFATree NFA_tree = RegularToNFA(std::move(r)); NFAGraph NFA_tree = RegularToNFAGraph(std::move(r));
DFAGraph DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
std::string s = ""; std::string s = "";
ASSERT_EQ(DFA_graph.Accepted(s), false); ASSERT_EQ(DFA_graph.Accepted(s), false);
@ -49,9 +49,9 @@ TEST(regular_to_DFA, abc) {
std::string regulars[] = {"abc"}; std::string regulars[] = {"abc"};
for (const auto& regular: regulars) { for (const auto& regular: regulars) {
RegularTree r(regular); RegularTree r(regular);
NFATree NFA_tree = RegularToNFA(std::move(r)); NFAGraph NFA_tree = RegularToNFAGraph(std::move(r));
DFAGraph DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
ASSERT_EQ(DFA_graph.Accepted("abc"), true); ASSERT_EQ(DFA_graph.Accepted("abc"), true);
for (int i = 0; i < 1000; ++i) { for (int i = 0; i < 1000; ++i) {
@ -66,9 +66,9 @@ TEST(regular_to_DFA, a_or_b_or_c) {
std::string regulars[] = {}; std::string regulars[] = {};
for (const auto& regular: regulars) { for (const auto& regular: regulars) {
RegularTree r("a|b|c"); RegularTree r("a|b|c");
NFATree NFA_tree = RegularToNFA(std::move(r)); NFAGraph NFA_tree = RegularToNFAGraph(std::move(r));
DFAGraph DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
ASSERT_EQ(DFA_graph.Accepted("a"), true); ASSERT_EQ(DFA_graph.Accepted("a"), true);
ASSERT_EQ(DFA_graph.Accepted("b"), true); ASSERT_EQ(DFA_graph.Accepted("b"), true);
@ -87,9 +87,9 @@ TEST(regular_to_DFA, a_star_or_b_star_or_c_star) {
std::string regulars[] = {"a*|b*|c*", "(a*)|(b*|c*)", "(a*|b*|c*)", "((a*)|(b*)|(c*))"}; std::string regulars[] = {"a*|b*|c*", "(a*)|(b*|c*)", "(a*|b*|c*)", "((a*)|(b*)|(c*))"};
for (const auto& regular: regulars) { for (const auto& regular: regulars) {
RegularTree r(regular); RegularTree r(regular);
NFATree NFA_tree = RegularToNFA(std::move(r)); NFAGraph NFA_tree = RegularToNFAGraph(std::move(r));
DFAGraph DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
auto check = [](const std::string str) { auto check = [](const std::string str) {
std::set<char> s(str.begin(), str.end()); std::set<char> s(str.begin(), str.end());
@ -114,9 +114,9 @@ TEST(regular_to_DFA, a_star_or_b_star_or_c_star) {
TEST(regular_to_DFA, _a_star_or_b_star_or_c_star_plus) { TEST(regular_to_DFA, _a_star_or_b_star_or_c_star_plus) {
RegularTree r("(a*|b*|c*)+"); RegularTree r("(a*|b*|c*)+");
NFATree NFA_tree = RegularToNFA(std::move(r)); NFAGraph NFA_tree = RegularToNFAGraph(std::move(r));
DFAGraph DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
auto check = [](const std::string str) { auto check = [](const std::string str) {
std::set<char> s(str.begin(), str.end()); std::set<char> s(str.begin(), str.end());
@ -133,9 +133,9 @@ TEST(regular_to_DFA, _a_star_or_b_star_or_c_star_plus) {
TEST(regular_to_DFA, _a_or_b_or_c_star_plus) { TEST(regular_to_DFA, _a_or_b_or_c_star_plus) {
RegularTree r("(a|b|c*)+"); RegularTree r("(a|b|c*)+");
NFATree NFA_tree = RegularToNFA(std::move(r)); NFAGraph NFA_tree = RegularToNFAGraph(std::move(r));
DFAGraph DFA_graph = NFATreeToDFAGraph(std::move(NFA_tree)); DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree));
DFA_graph = DFAToMinDFA(std::move(DFA_graph)); DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
auto check = [](const std::string str) { auto check = [](const std::string str) {
std::set<char> s(str.begin(), str.end()); std::set<char> s(str.begin(), str.end());