reglibcpp  1.5.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);
66 
70  class builder {
71  public:
72  builder ();
73  builder (dfa const& dfa);
74  builder (nfa const& nfa);
75  builder(builder const& b);
76  builder(builder&& b);
77  builder& operator=(const builder& b);
79  virtual ~builder ();
80 
81  builder& addSymbol(char32_t symbol);
82  builder& addSymbol(std::string const& utf8Symbol);
83  builder& setAccepting(std::string const& state, bool accept);
84  builder& makeInitial(std::string const& state);
85  builder& defineTransition(std::string const& from, std::string const& to, char32_t symbol);
86  builder& defineTransition(std::string const& from, std::string const& to, std::string const& utf8Symbol);
87  builder& merge();
88  builder& purge();
89  builder& minimize();
90  builder& unite(dfa const& other);
91  builder& intersect(dfa const& other);
93  builder& normalizeStateNames(std::string const& prefix);
94  dfa build();
95  private:
96  struct pImpl;
98  };
99 private:
100  struct pImpl;
102  dfa(
103  std::vector<char32_t>& alphabet,
104  std::vector<std::vector<size_t>>& transitions,
105  std::vector<std::string>& labels,
106  std::valarray<bool>& acceptingStates
107  );
108 };
111 
112 template<class T> size_t index_of(std::vector<T> const& vec, T const& element);
113 extern template size_t index_of(std::vector<char32_t> const& vec, char32_t const& element);
114 extern template size_t index_of(std::vector<std::string> const& vec, std::string const& element);
115 
117 } // namespace reg
118 
119 #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:499
std::string const & getInitialState() const
Names this DFA's initial state.
Definition: dfa.cpp:365
builder & setAccepting(std::string const &state, bool accept)
Sets whether or not a state will be accepting within the prospective DFA.
Definition: dfa.cpp:688
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this DFA.
Definition: dfa.cpp:415
builder & unite(dfa const &other)
Makes the prospective DFA also accept every word of another DFA&#39;s language.
Definition: dfa.cpp:869
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:459
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:1088
builder & minimize()
Convenience method for chaining purge and merge to achieve proper minimization.
Definition: dfa.cpp:858
builder & purge()
Purges the prospective DFA of unreachable and non-producing states, allowing for minimization.
Definition: dfa.cpp:799
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:381
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
Counts this DFA's alphabet symbols.
Definition: dfa.cpp:405
Private implementation details of DFA builders.
Definition: dfa.cpp:507
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:483
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:1080
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:1004
std::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
Definition: dfa.cpp:373
builder & makeInitial(std::string const &state)
Resets the initial state for the prospective DFA.
Definition: dfa.cpp:706
builder & merge()
Merges the prospective DFA's indistinguishable states, allowing for minimization. ...
Definition: dfa.cpp:747
Constructs DFAs step by step.
Definition: dfa.h:70
size_t getNumberOfStates() const
Counts this DFA's states.
Definition: dfa.cpp:397
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:1015
builder()
Constructs a blank builder object.
Definition: dfa.cpp:585
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:471
Where this library lives.
Definition: dfa.cpp:51
std::u32string getShortestWord() const
Searches the shortest UTF-32-encoded word accepted by this DFA.
Definition: dfa.cpp:321
builder & intersect(dfa const &other)
Makes the prospective DFA accept only words accepted also by another DFA.
Definition: dfa.cpp:947
builder & operator=(const builder &b)
Copy-assigns a builder by copying another one's private implementation object.
Definition: dfa.cpp:648
string findShortestUtf8Word(dfa const &d)
Same as above for a UTF-8-encoded word.
Definition: dfa.cpp:447
std::string getShortestUtf8Word() const
Same as above for a UTF-8-encoded word.
Definition: dfa.cpp:347
u32string findShortestWord(dfa const &d)
Searches the shortest UTF-32-encoded word accepted by a given DFA.
Definition: dfa.cpp:442
dfa build()
Builds the DFA, as defined by previous operations, including completion.
Definition: dfa.cpp:1049
std::string const & getLabelOf(size_t qi) const
Puts a name to an index.
Definition: dfa.cpp:357
std::vector< std::string > const & getUtf8Alphabet() const
Fetches this DFA's set of processable symbols as UTF-8-encoded strings.
Definition: dfa.cpp:389
builder & defineTransition(std::string const &from, std::string const &to, char32_t symbol)
(Re-)Defines a transition for the prospective DFA.
Definition: dfa.cpp:722
builder & addSymbol(char32_t symbol)
Adds a symbol to the prospective DFA's alphabet.
Definition: dfa.cpp:672