reglibcpp  1.6.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 
18 namespace reg {
19 class nfa;
21 
27 class dfa {
28 public:
29  class builder;
30  dfa();
31  dfa(dfa const& d);
32  dfa(dfa&& d);
33  dfa(nfa const& n);
34  dfa(builder& b);
35  dfa& operator=(const dfa& d);
36  dfa& operator=(dfa&& d);
37  virtual ~dfa ();
38 
39  bool operator==(const dfa& d) const;
40  bool operator!=(const dfa& d) const;
41  size_t delta(size_t qi, size_t si) const;
42  size_t delta(size_t qi, char32_t symbol) const;
43  size_t delta(size_t qi, std::string const& utf8Symbol) const;
44  std::string const& delta(std::string const& q, char32_t symbol) const;
45  std::string const& delta(std::string const& q, std::string const& utf8Symbol) const;
46  size_t deltaHat(size_t qi, std::u32string const& word) const;
47  size_t deltaHat(size_t qi, std::string const& utf8Word) const;
48  std::string const& deltaHat(std::string const& q, std::u32string const& word) const;
49  std::string const& deltaHat(std::string const& q, std::string const& utf8Word) const;
52  std::string const& getLabelOf(size_t qi) const;
53  std::string const& getInitialState() const;
54  std::vector<std::string> const& getStates() const;
55  std::vector<char32_t> const& getAlphabet() const;
57  size_t getNumberOfStates() const;
58  size_t getNumberOfSymbols() const;
59  bool isAccepting(size_t qi) const;
60  bool isAccepting(std::string const& q) const;
61  static dfa::builder unite(dfa const& d1, dfa const& d2);
62  static dfa::builder intersect(dfa const& d1, dfa const& d2);
63  static dfa::builder subtract(dfa const& d1, dfa const& d2);
64  static dfa::builder complement(dfa const& d);
65  friend std::u32string findShortestWord(dfa const& d);
66  friend std::string findShortestUtf8Word(dfa const& d);
68 
72  class builder {
73  public:
74  builder ();
75  builder (dfa const& dfa);
76  builder (nfa const& nfa);
77  builder(builder const& b);
78  builder(builder&& b);
79  builder& operator=(const builder& b);
81  virtual ~builder ();
82 
83  builder& addSymbol(char32_t symbol);
84  builder& addSymbol(std::string const& utf8Symbol);
85  builder& setAccepting(std::string const& state, bool accept);
86  builder& makeInitial(std::string const& state);
87  builder& defineTransition(std::string const& from, std::string const& to, char32_t symbol);
88  builder& defineTransition(std::string const& from, std::string const& to, std::string const& utf8Symbol);
89  builder& merge();
90  builder& purge();
91  builder& minimize();
92  builder& unite(dfa const& other);
93  builder& intersect(dfa const& other);
95  builder& normalizeStateNames(std::string const& prefix);
96  dfa build();
97  private:
98  struct pImpl;
100  };
101 private:
102  struct pImpl;
104  dfa(
105  std::vector<char32_t>& alphabet,
106  std::vector<std::vector<size_t>>& transitions,
107  std::vector<std::string>& labels,
108  std::valarray<bool>& acceptingStates
109  );
110 };
113 
115 
120 template<class C,class T> size_t index_of(C const& container, T const& element) {
121  static_assert(std::is_same<typename C::value_type,T>::value, "C must be a container with T as value_type.");
122  return static_cast<size_t>(std::distance(container.begin(), std::find(container.begin(), container.end(), element)));
123 }
124 
126 } // namespace reg
127 
128 #endif
bool operator!=(const dfa &d) const
Tests whether this DFA doesn't accept the same language as another one.
Definition: dfa.cpp:217
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:485
std::string const & getInitialState() const
Names this DFA's initial state.
Definition: dfa.cpp:335
builder & setAccepting(std::string const &state, bool accept)
Sets whether or not a state will be accepting within the prospective DFA.
Definition: dfa.cpp:674
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this DFA.
Definition: dfa.cpp:379
builder & unite(dfa const &other)
Makes the prospective DFA also accept every word of another DFA&#39;s language.
Definition: dfa.cpp:855
dfa()
Constructs a DFA accepting the empty language ∅.
Definition: dfa.cpp:126
Represents nondeterministic finite automata with ε-moves.
Definition: nfa.h:25
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:445
Represents deterministic finite automata.
Definition: dfa.h:27
Private implementation details of DFAs.
Definition: dfa.cpp:54
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1060
builder & minimize()
Convenience method for chaining purge and merge to achieve proper minimization.
Definition: dfa.cpp:844
builder & purge()
Purges the prospective DFA of unreachable and non-producing states, allowing for minimization.
Definition: dfa.cpp:785
friend std::string findShortestUtf8Word(dfa const &d)
Same as above for a UTF-8-encoded word.
Definition: dfa.cpp:433
size_t delta(size_t qi, size_t si) const
Computes this DFA's transition function for a state index and a symbol index.
Definition: dfa.cpp:227
std::vector< char32_t > const & getAlphabet() const
Fetches this DFA's set of processable symbols.
Definition: dfa.cpp:351
dfa & operator=(const dfa &d)
Copy-assigns this DFA by copying another one's private implementation object.
Definition: dfa.cpp:149
size_t getNumberOfSymbols() const
Definition: dfa.cpp:369
Private implementation details of DFA builders.
Definition: dfa.cpp:493
size_t index_of(C const &container, 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.h:120
size_t deltaHat(size_t qi, std::u32string const &word) const
Computes this DFA's transition function recursively for a string of symbols, starting in a state spec...
Definition: dfa.cpp:287
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:469
friend std::u32string findShortestWord(dfa const &d)
Searches the shortest UTF-32-encoded word accepted by a given DFA.
Definition: dfa.cpp:406
bool operator==(const dfa &d) const
Tests whether this DFA accepts exactly the same language as another one.
Definition: dfa.cpp:173
builder & complement()
Inverts the prospective DFA&#39;s language with respect to all possible strings over its alphabet...
Definition: dfa.cpp:990
std::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
Definition: dfa.cpp:343
builder & makeInitial(std::string const &state)
Resets the initial state for the prospective DFA.
Definition: dfa.cpp:692
builder & merge()
Merges the prospective DFA's indistinguishable states, allowing for minimization. ...
Definition: dfa.cpp:733
Constructs DFAs step by step.
Definition: dfa.h:72
size_t getNumberOfStates() const
Definition: dfa.cpp:364
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:1001
builder()
Constructs a blank builder object.
Definition: dfa.cpp:571
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:457
Where this library lives.
Definition: dfa.cpp:51
std::u32string getShortestWord() const
Definition: dfa.cpp:317
builder & intersect(dfa const &other)
Makes the prospective DFA accept only words accepted also by another DFA.
Definition: dfa.cpp:933
builder & operator=(const builder &b)
Copy-assigns a builder by copying another one's private implementation object.
Definition: dfa.cpp:634
string findShortestUtf8Word(dfa const &d)
Same as above for a UTF-8-encoded word.
Definition: dfa.cpp:433
std::string getShortestUtf8Word() const
Definition: dfa.cpp:322
u32string findShortestWord(dfa const &d)
Searches the shortest UTF-32-encoded word accepted by a given DFA.
Definition: dfa.cpp:406
dfa build()
Builds the DFA, as defined by previous operations, including completion.
Definition: dfa.cpp:1035
std::string const & getLabelOf(size_t qi) const
Definition: dfa.cpp:327
std::vector< std::string > const & getUtf8Alphabet() const
Fetches this DFA's set of processable symbols as UTF-8-encoded strings.
Definition: dfa.cpp:359
builder & defineTransition(std::string const &from, std::string const &to, char32_t symbol)
(Re-)Defines a transition for the prospective DFA.
Definition: dfa.cpp:708
builder & addSymbol(char32_t symbol)
Adds a symbol to the prospective DFA's alphabet.
Definition: dfa.cpp:658