From f715234f41707d91047c7ca7e1b959e11de8ef08 Mon Sep 17 00:00:00 2001 From: MaxanRus Date: Tue, 5 Oct 2021 20:41:05 +0300 Subject: [PATCH] dot files --- CMakeLists.txt | 5 ++--- include/DFA/DFAGraph.hpp | 1 + include/NFA/NFAGraph.hpp | 1 + src/DFA/DFAGraph.cpp | 21 +++++++++++++++++++++ src/NFA/NFAGraph.cpp | 23 +++++++++++++++++++++++ src/converters/DFAToMinDFA.cpp | 3 +-- src/main.cpp | 16 ++++++++++++---- 7 files changed, 61 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6395d82..d479af6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.10) project("Formalang") # set(CMAKE_CXX_FLAGS "-O3") +# set(CMAKE_CXX_FLAGS "-O0 --coverage -ftest-coverage -fprofile-arcs") find_package(GTest REQUIRED) find_package(Threads REQUIRED) @@ -19,7 +20,7 @@ set(SOURCE_FILES src/regular/RegularTree.cpp src/regular/RegularTreeNode.cpp src/NFA/NFAGraph.cpp - src/NFA/NFAGraphVertex.cpp + src/NFA/NFAGraphVertex.cpp src/converters/RegularToNFA.cpp src/converters/NFAToDFA.cpp src/DFA/DFAGraph.cpp @@ -44,5 +45,3 @@ add_executable(Formalang src/main.cpp ${SOURCE_FILES}) add_executable(Tests tests/MainTest.cpp ${TEST_FILES} ${SOURCE_FILES}) target_link_libraries(Tests ${GTEST_LIBRARIES} Threads::Threads) -target_link_libraries(Formalang Threads::Threads) - diff --git a/include/DFA/DFAGraph.hpp b/include/DFA/DFAGraph.hpp index a04c796..82b5c5a 100644 --- a/include/DFA/DFAGraph.hpp +++ b/include/DFA/DFAGraph.hpp @@ -59,6 +59,7 @@ class DFAGraph { void Print() const; bool Accepted(const std::string&) const; + void CreateDotFile(const std::string& filename) const; private: size_t count_vertexes_ = 0; std::map> vertexes_; diff --git a/include/NFA/NFAGraph.hpp b/include/NFA/NFAGraph.hpp index abbabaf..69e1d45 100644 --- a/include/NFA/NFAGraph.hpp +++ b/include/NFA/NFAGraph.hpp @@ -61,6 +61,7 @@ class NFAGraph { const std::vector& GetStartVertexes() const; void Print() const; + void CreateDotFile(const std::string& filename) const; private: size_t count_vertexes_ = 0; std::map> vertexes_; diff --git a/src/DFA/DFAGraph.cpp b/src/DFA/DFAGraph.cpp index 1024151..05378b3 100644 --- a/src/DFA/DFAGraph.cpp +++ b/src/DFA/DFAGraph.cpp @@ -1,6 +1,7 @@ #include "DFA/DFAGraph.hpp" #include #include +#include namespace DFA { DFAGraph::DFAGraph(DFAGraph&& another) { @@ -119,5 +120,25 @@ bool DFAGraph::Accepted(const std::string& str) const { } return current->IsFinal(); } +void DFAGraph::CreateDotFile(const std::string& filename) const { + std::ofstream out(filename); + out << "digraph G {\n"; + + for (auto& i: vertexes_) { + for (auto& j: i.second->transitions_) { + out << i.first << "->" << j.second << "[label=" << j.first << "]\n"; + } + if (i.second->IsStart() && i.second->IsFinal()) { + out << " " << i.first << " [shape=star];\n"; + } else if (i.second->IsStart()) { + out << " " << i.first << " [shape=rarrow];\n"; + } else if (i.second->IsFinal()) { + out << " " << i.first << " [shape=Msquare];\n"; + } + } + + out << "}\n"; + +} } diff --git a/src/NFA/NFAGraph.cpp b/src/NFA/NFAGraph.cpp index aefe35a..ade1e57 100644 --- a/src/NFA/NFAGraph.cpp +++ b/src/NFA/NFAGraph.cpp @@ -1,6 +1,7 @@ #include "NFA/NFAGraph.hpp" #include #include +#include namespace NFA { NFAGraph::NFAGraph(NFAGraph&& another) { @@ -168,4 +169,26 @@ void NFAGraph::Print() const { } std::cout << std::endl; } + +void NFAGraph::CreateDotFile(const std::string& filename) const { + std::ofstream out(filename); + out << "digraph G {\n"; + + for (auto& i: vertexes_) { + for (auto& j: i.second->transitions_) { + for (auto& k: j.second) { + out << i.first << "->" << k << "[label=" << j.first << "]\n"; + } + } + if (i.second->IsStart() && i.second->IsFinal()) { + out << " " << i.first << " [shape=Mstar];\n"; + } else if (i.second->IsStart()) { + out << " " << i.first << " [shape=Mdiamond];\n"; + } else if (i.second->IsFinal()) { + out << " " << i.first << " [shape=Msquare];\n"; + } + } + + out << "}\n"; +} } diff --git a/src/converters/DFAToMinDFA.cpp b/src/converters/DFAToMinDFA.cpp index 4a67245..5372265 100644 --- a/src/converters/DFAToMinDFA.cpp +++ b/src/converters/DFAToMinDFA.cpp @@ -1,4 +1,5 @@ #include "converters/DFAToMinDFA.hpp" +#include namespace converters { DFAGraph DFAGraphToMinDFAGraph(DFAGraph&& graph) { @@ -65,7 +66,6 @@ DFAGraph DFAGraphToMinDFAGraph(DFAGraph&& graph) { } } - /* for (int i = 0; i < n; ++i) { for (int k = 0; k < n; ++k) { for (int j = 0; j < alphabet.size() + 1; ++j) { @@ -75,7 +75,6 @@ DFAGraph DFAGraphToMinDFAGraph(DFAGraph&& graph) { } std::cout << std::endl; } - */ std::vector>& last_layer(table.back()); size_t count_vertex = 0; diff --git a/src/main.cpp b/src/main.cpp index d4946b5..30193c9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,4 @@ #include -#include #include "regular/RegularTree.hpp" #include "converters/RegularToNFA.hpp" #include "converters/NFAToDFA.hpp" @@ -52,13 +51,22 @@ void example2() { int main() { { - RegularTree r("(a|b)+bab(a|b)+"); + RegularTree r("((ab|ba)*( |a|ba))"); NFAGraph NFA_tree = RegularToNFAGraph(std::move(r)); DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree)); - DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph)); - + // DFA_graph.CreateDotFile("1.dot"); + // DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph)); DFA_graph = DFAGraphToFDFAGraph(std::move(DFA_graph), {'a', 'b'}); DFA_graph = InvertFDFAGraph(std::move(DFA_graph)); + // DFA_graph.CreateDotFile("2.dot"); + auto ss = DFAGraphToRegular(std::move(DFA_graph)); + std::cout << ss << std::endl; + r = RegularTree(ss); + // r.Print(); + std::cout << r.ToString() << std::endl; + // DFA_graph.Print(); + return 0; + DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph)); DFA_graph.Print(); auto s = DFAGraphToRegular(std::move(DFA_graph));