reglibcpp  1.3.0
(Naïve) C++ implementation of models for regular languages
dfa.h
Go to the documentation of this file.
1 #ifndef REG_DFA_H
2 #define REG_DFA_H
3 
5 #include <memory>
6 
7 #include <vector>
8 
9 #include <valarray>
10 
11 #include <string>
12 
13 #include <locale>
14 
15 #include <codecvt>
16 
17 namespace reg {
18 class nfa;
20 
26 class dfa {
27 public:
28  class builder;
29  dfa();
30  dfa(dfa const& d);
31  dfa(dfa&& d);
32  dfa(nfa const& n);
33  dfa(builder& b);
34  dfa& operator=(const dfa& d);
35  dfa& operator=(dfa&& d);
36  virtual ~dfa ();
37 
38  bool operator==(const dfa& d) const;
39  bool operator!=(const dfa& d) const;
40  size_t delta(size_t q, char32_t symbol) const;
41  size_t delta(size_t q, std::string const& utf8Symbol) const;
42  size_t deltaHat(size_t q, std::u32string const& word) const;
43  size_t deltaHat(size_t q, std::string const& utf8Word) const;
44  std::u32string getShortestWord() const;
45  std::string getShortestUtf8Word() const;
46  std::string const& getLabelOf(size_t q) const;
47  std::vector<char32_t> const& getAlphabet() const;
48  std::vector<std::string> const& getUtf8Alphabet() const;
49  size_t getNumberOfStates() const;
50  bool isAccepting(size_t q) const;
51  static dfa::builder unite(dfa const& d1, dfa const& d2);
52  static dfa::builder intersect(dfa const& d1, dfa const& d2);
53  static dfa::builder subtract(dfa const& d1, dfa const& d2);
54  static dfa::builder complement(dfa const& d);
56 
60  class builder {
61  public:
62  builder ();
63  builder (dfa const& dfa);
64  builder (nfa const& nfa);
65  builder(builder const& b);
66  builder(builder&& b);
67  builder& operator=(const builder& b);
69  virtual ~builder ();
70 
71  builder& addSymbol(char32_t symbol);
72  builder& addSymbol(std::string const& utf8Symbol);
73  builder& setAccepting(std::string const& state, bool accept);
74  builder& makeInitial(std::string const& state);
75  builder& defineTransition(std::string const& from, std::string const& to, char32_t symbol);
76  builder& defineTransition(std::string const& from, std::string const& to, std::string const& utf8Symbol);
77  builder& merge();
78  builder& purge();
79  builder& minimize();
80  builder& unite(dfa const& other);
81  builder& intersect(dfa const& other);
83  builder& normalizeStateNames(std::string const& prefix);
84  dfa build();
85  private:
86  struct pImpl;
87  std::unique_ptr<pImpl> p;
88  };
89 private:
90  struct pImpl;
91  std::unique_ptr<pImpl> p;
92  dfa(
93  std::vector<char32_t>& alphabet,
94  std::vector<std::vector<size_t>>& transitions,
95  std::vector<std::string>& labels,
96  std::valarray<bool>& acceptingStates
97  );
98 };
99 
100 template<class T> size_t index_of(std::vector<T> const& vec, T const& element);
101 extern template size_t index_of(std::vector<char32_t> const& vec, char32_t const& element);
102 
103 extern std::wstring_convert<std::codecvt_utf8<char32_t>,char32_t> converter;
104 } // namespace reg
105 #endif
bool isAccepting(size_t q) const
Tests whether a state is an accept state within this DFA.
Definition: dfa.cpp:319
bool operator!=(const dfa &d) const
Tests whether this DFA doesn't accept the same language as another one.
Definition: dfa.cpp:195
static dfa::builder complement(dfa const &d)
Creates a builder for a DFA accepting the complement of the language of a DFA.
Definition: dfa.cpp:376
builder & setAccepting(std::string const &state, bool accept)
Sets whether or not a state will be accepting within the prospective DFA.
Definition: dfa.cpp:603
builder & unite(dfa const &other)
Makes the prospective DFA also accept every word of another DFA&#39;s language.
Definition: dfa.cpp:784
size_t delta(size_t q, char32_t symbol) const
Computes this DFA's transition function for a state and a symbol.
Definition: dfa.cpp:203
dfa()
Constructs a DFA accepting the empty language ∅.
Definition: dfa.cpp:104
Represents nondeterministic finite automata with ε-moves.
Definition: nfa.h:22
static dfa::builder unite(dfa const &d1, dfa const &d2)
Creates a builder for a DFA accepting the union of the languages of two DFAs.
Definition: dfa.cpp:336
Represents deterministic finite automata.
Definition: dfa.h:26
Private implementation details of DFAs.
Definition: dfa.cpp:32
builder & minimize()
Convenience method for chaining purge and merge to achieve proper minimization.
Definition: dfa.cpp:773
builder & purge()
Purges the prospective DFA of unreachable and non-producing states, allowing for minimization.
Definition: dfa.cpp:714
std::vector< char32_t > const & getAlphabet() const
Fetches this DFA's set of processable symbols.
Definition: dfa.cpp:294
dfa & operator=(const dfa &d)
Copy-assigns this DFA by copying another one's private implementation object.
Definition: dfa.cpp:127
Private implementation details of DFA builders.
Definition: dfa.cpp:383
static dfa::builder subtract(dfa const &d1, dfa const &d2)
Creates a builder for a DFA accepting the set difference of the languages of two DFAs.
Definition: dfa.cpp:360
bool operator==(const dfa &d) const
Tests whether this DFA accepts exactly the same language as another one.
Definition: dfa.cpp:151
builder & complement()
Inverts the prospective DFA&#39;s language with respect to all possible strings over its alphabet...
Definition: dfa.cpp:919
builder & makeInitial(std::string const &state)
Resets the initial state for the prospective DFA.
Definition: dfa.cpp:621
builder & merge()
Merges the prospective DFA's indistinguishable states, allowing for minimization. ...
Definition: dfa.cpp:662
Constructs DFAs step by step.
Definition: dfa.h:60
size_t deltaHat(size_t q, std::u32string const &word) const
Computes this DFA's transition function recursively for a string of symbols, starting in a specified ...
Definition: dfa.cpp:229
size_t getNumberOfStates() const
Counts this DFA's states.
Definition: dfa.cpp:310
builder & normalizeStateNames(std::string const &prefix)
Reduces the prospective NFA&#39;s state names to consecutive numbers, prefixed with a given string...
Definition: dfa.cpp:930
builder()
Constructs a blank builder object.
Definition: dfa.cpp:500
static dfa::builder intersect(dfa const &d1, dfa const &d2)
Creates a builder for a DFA accepting the intersection of the languages of two DFAs.
Definition: dfa.cpp:348
Definition: dfa.cpp:29
size_t index_of(vector< T > const &vec, T const &element)
Basically Java&#39;s List interface&#39;s indexOf, but as a non-member function and returning the container&#39;s...
Definition: dfa.cpp:995
std::u32string getShortestWord() const
Searches the shortest UTF-32-encoded word accepted by this DFA.
Definition: dfa.cpp:254
builder & intersect(dfa const &other)
Makes the prospective DFA accept only words accepted also by another DFA.
Definition: dfa.cpp:862
builder & operator=(const builder &b)
Copy-assigns a builder by copying another one's private implementation object.
Definition: dfa.cpp:563
std::string getShortestUtf8Word() const
Same as above for a UTF-8-encoded word, INCLUDING POTENTIAL INFINITE LOOP.
Definition: dfa.cpp:277
std::string const & getLabelOf(size_t q) const
Puts a name to an index.
Definition: dfa.cpp:286
dfa build()
Builds the DFA, as defined by previous operations, including completion.
Definition: dfa.cpp:964
std::vector< std::string > const & getUtf8Alphabet() const
Fetches this DFA's set of processable symbols as UTF-8-encoded strings.
Definition: dfa.cpp:302
builder & defineTransition(std::string const &from, std::string const &to, char32_t symbol)
(Re-)Defines a transition for the prospective DFA.
Definition: dfa.cpp:637
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1001
builder & addSymbol(char32_t symbol)
Adds a symbol to the prospective DFA's alphabet.
Definition: dfa.cpp:587