dot files

This commit is contained in:
MaxanRus 2021-10-05 20:41:05 +03:00
parent 124a6c17b1
commit f715234f41
7 changed files with 61 additions and 9 deletions

View file

@ -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)
@ -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)

View file

@ -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<size_t, std::shared_ptr<Vertex>> vertexes_;

View file

@ -61,6 +61,7 @@ class NFAGraph {
const std::vector<size_t>& GetStartVertexes() const;
void Print() const;
void CreateDotFile(const std::string& filename) const;
private:
size_t count_vertexes_ = 0;
std::map<size_t, std::shared_ptr<Vertex>> vertexes_;

View file

@ -1,6 +1,7 @@
#include "DFA/DFAGraph.hpp"
#include <algorithm>
#include <iostream>
#include <fstream>
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";
}
}

View file

@ -1,6 +1,7 @@
#include "NFA/NFAGraph.hpp"
#include <algorithm>
#include <iostream>
#include <fstream>
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";
}
}

View file

@ -1,4 +1,5 @@
#include "converters/DFAToMinDFA.hpp"
#include <iostream>
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<std::vector<size_t>>& last_layer(table.back());
size_t count_vertex = 0;

View file

@ -1,5 +1,4 @@
#include <iostream>
#include <cassert>
#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));