From 83b6e0edc3ab26c3fa1cf4ff171db5b43e4d73b4 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Sun, 7 Sep 2014 20:52:19 +0200 Subject: [PATCH] Derivations produce DFA --- .../src/conversions/re2fa/Brzozowski.cpp | 32 ++++++++++--------- alib2algo/src/conversions/re2fa/Brzozowski.h | 4 +-- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/alib2algo/src/conversions/re2fa/Brzozowski.cpp b/alib2algo/src/conversions/re2fa/Brzozowski.cpp index 3de12a3997..ca82b08dec 100644 --- a/alib2algo/src/conversions/re2fa/Brzozowski.cpp +++ b/alib2algo/src/conversions/re2fa/Brzozowski.cpp @@ -17,7 +17,7 @@ #include <label/StringLabel.h> #include "../../regexp/RegExpDerivation.h" -//#include "regexp/RegExpOptimize.h" +#include "../../regexp/RegExpOptimize.h" namespace re2fa { @@ -39,12 +39,11 @@ void Brzozowski::Visit(void* userData, const regexp::UnboundedRegExp& regexp) out.second = regexp.containsEmptyString(); } -automaton::NFA Brzozowski::convert(const regexp::RegExp& regexp) +automaton::DFA Brzozowski::convert(const regexp::RegExp& regexp) { // 1. - // regexp::RegExpOptimize opt; - // regexp::RegExp V = opt.optimize(regexp); - regexp::RegExp V = regexp; + regexp::RegExpOptimize opt; + regexp::RegExp V = opt.optimize(regexp); std::pair<std::set<alphabet::Symbol>, bool> out({}, false); regexp.getData().Accept((void*) &out, *this); @@ -71,7 +70,7 @@ automaton::NFA Brzozowski::convert(const regexp::RegExp& regexp) { string::LinearString string(std::vector<alphabet::Symbol>{a}); regexp::RegExp derived = deriv.derivation(dregexp, string); - // derived = opt.optimize(derived); + derived = opt.optimize(derived); // this will also add \emptyset as a regexp (and as FA state) if(Q.count(derived) == 0) // if this state has already been found, do not add @@ -83,18 +82,23 @@ automaton::NFA Brzozowski::convert(const regexp::RegExp& regexp) i += 1; } - // ------------------------------------------------------------------------ - // 3. - - automaton::NFA automaton; - int stateId = 0; std::map<regexp::RegExp, automaton::State> stateMap; + int stateId = 0; for(const auto& r : Q) { automaton::State q(label::Label(label::StringLabel(std::toBase26(stateId++)))); stateMap.insert(std::make_pair(r, q)); - automaton.addState(q); + } + + // ------------------------------------------------------------------------ + // 3. + + automaton::DFA automaton(stateMap.find(V)->second); + + for(const auto& r : stateMap) + { + automaton.addState(r.second); } automaton.setInputSymbols(alphabet); @@ -107,14 +111,12 @@ automaton::NFA Brzozowski::convert(const regexp::RegExp& regexp) { string::LinearString string(std::vector<alphabet::Symbol>{a}); regexp::RegExp derived = deriv.derivation(r, string); - // derived = opt.optimize(derived); + derived = opt.optimize(derived); automaton.addTransition(stateMap.find(r)->second, a, stateMap.find(derived)->second); } } - automaton.addInitialState(stateMap.find(V)->second); - for(const auto& r : Q) { r.getData().Accept((void*) &out, *this); diff --git a/alib2algo/src/conversions/re2fa/Brzozowski.h b/alib2algo/src/conversions/re2fa/Brzozowski.h index 58d4d0aaa5..09679bfb0d 100644 --- a/alib2algo/src/conversions/re2fa/Brzozowski.h +++ b/alib2algo/src/conversions/re2fa/Brzozowski.h @@ -11,7 +11,7 @@ #include <regexp/RegExp.h> #include <regexp/formal/FormalRegExp.h> #include <regexp/unbounded/UnboundedRegExp.h> -#include <automaton/FSM/NFA.h> +#include <automaton/FSM/DFA.h> namespace re2fa { @@ -30,7 +30,7 @@ public: * Performs conversion. * @return FSM equivalent to original regular expression. */ - automaton::NFA convert(const regexp::RegExp& regexp); + automaton::DFA convert(const regexp::RegExp& regexp); private: void Visit(void* , const regexp::FormalRegExp& regexp); -- GitLab