reglibcpp  1.7.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 <string>
10 
12 namespace reg {
13 class nfa;
15 
21 class dfa {
22 public:
23  class builder;
24  dfa();
25  dfa(dfa const& d);
26  dfa(dfa&& d);
27  dfa& operator=(dfa const& d);
28  dfa& operator=(dfa&& d);
29  virtual ~dfa ();
30 
31  operator nfa const&() const;
32  bool operator==(dfa const& d) const;
33  bool operator!=(dfa const& d) const;
34  bool operator==(nfa const& d) const;
35  bool operator!=(nfa const& d) const;
36  size_t delta(size_t qi, size_t si) const;
37  size_t delta(size_t qi, char32_t symbol) const;
38  size_t delta(size_t qi, std::string const& utf8Symbol) const;
39  std::string const& delta(std::string const& q, char32_t symbol) const;
40  std::string const& delta(std::string const& q, std::string const& utf8Symbol) const;
41  size_t deltaHat(size_t qi, std::u32string const& word) const;
42  size_t deltaHat(size_t qi, std::string const& utf8Word) const;
43  std::string const& deltaHat(std::string const& q, std::u32string const& word) const;
44  std::string const& deltaHat(std::string const& q, std::string const& utf8Word) const;
47  std::string const& getLabelOf(size_t qi) const;
48  std::string const& getInitialState() const;
49  std::vector<std::string> const& getStates() const;
50  std::vector<char32_t> const& getAlphabet() const;
52  size_t getNumberOfStates() const;
53  size_t getNumberOfSymbols() const;
54  bool isAccepting(size_t qi) const;
55  bool isAccepting(std::string const& q) const;
56  static dfa::builder unite(dfa const& d1, dfa const& d2);
57  static dfa::builder intersect(dfa const& d1, dfa const& d2);
58  static dfa::builder subtract(dfa const& d1, dfa const& d2);
59  static dfa::builder complement(dfa const& d);
60  friend std::u32string findShortestWord(dfa const& d);
61  friend std::string findShortestUtf8Word(dfa const& d);
63 
67  class builder {
68  public:
69  builder ();
70  builder (dfa const& dfa);
71  builder (nfa const& nfa);
72  builder(builder const& b);
73  builder(builder&& b);
74  builder& operator=(const builder& b);
76  virtual ~builder ();
77 
78  operator dfa();
79  builder& addSymbol(char32_t symbol);
80  builder& addSymbol(std::string const& utf8Symbol);
81  builder& setAccepting(std::string const& state, bool accept);
82  builder& makeInitial(std::string const& state);
83  builder& defineTransition(std::string const& from, std::string const& to, char32_t symbol);
84  builder& defineTransition(std::string const& from, std::string const& to, std::string const& utf8Symbol);
85  builder& merge();
86  builder& purge();
87  builder& minimize();
88  builder& unite(dfa const& other);
89  builder& intersect(dfa const& other);
91  builder& normalizeStateNames(std::string const& prefix);
92  dfa build();
93  private:
94  struct pImpl;
96  };
97 private:
98  struct pImpl;
100  dfa(
101  std::vector<char32_t>& alphabet,
102  std::vector<std::vector<size_t>>& transitions,
103  std::vector<std::string>& labels,
104  std::valarray<bool>& acceptingStates
105  );
106 };
109 } // namespace reg
110 
111 #endif
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:497
std::string const & getInitialState() const
Names this DFA's initial state.
Definition: dfa.cpp:347
builder & setAccepting(std::string const &state, bool accept)
Sets whether or not a state will be accepting within the prospective DFA.
Definition: dfa.cpp:691
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this DFA.
Definition: dfa.cpp:391
builder & unite(dfa const &other)
Makes the prospective DFA also accept every word of another DFA&#39;s language.
Definition: dfa.cpp:872
dfa()
Constructs a DFA accepting the empty language ∅.
Definition: dfa.cpp:127
Represents nondeterministic finite automata with ε-moves.
Definition: nfa.h:23
bool operator==(dfa const &d) const
Tests whether this DFA accepts exactly the same language as another one.
Definition: dfa.cpp:177
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:457
Represents deterministic finite automata.
Definition: dfa.h:21
Private implementation details of DFAs.
Definition: dfa.cpp:54
builder & minimize()
Convenience method for chaining purge and merge to achieve proper minimization.
Definition: dfa.cpp:861
builder & purge()
Purges the prospective DFA of unreachable and non-producing states, allowing for minimization.
Definition: dfa.cpp:802
friend std::string findShortestUtf8Word(dfa const &d)
Same as above for a UTF-8-encoded word.
Definition: dfa.cpp:445
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:239
std::vector< char32_t > const & getAlphabet() const
Fetches this DFA's set of processable symbols.
Definition: dfa.cpp:363
bool operator!=(dfa const &d) const
Tests whether this DFA doesn't accept the same language as another one.
Definition: dfa.cpp:221
size_t getNumberOfSymbols() const
Definition: dfa.cpp:381
Private implementation details of DFA builders.
Definition: dfa.cpp:505
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:299
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:481
friend std::u32string findShortestWord(dfa const &d)
Searches the shortest UTF-32-encoded word accepted by a given DFA.
Definition: dfa.cpp:418
builder & complement()
Inverts the prospective DFA&#39;s language with respect to all possible strings over its alphabet...
Definition: dfa.cpp:1007
std::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
Definition: dfa.cpp:355
builder & makeInitial(std::string const &state)
Resets the initial state for the prospective DFA.
Definition: dfa.cpp:709
builder & merge()
Merges the prospective DFA's indistinguishable states, allowing for minimization. ...
Definition: dfa.cpp:750
Constructs DFAs step by step.
Definition: dfa.h:67
size_t getNumberOfStates() const
Definition: dfa.cpp:376
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:1018
builder()
Constructs a blank builder object.
Definition: dfa.cpp:583
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:469
Where this library lives.
Definition: dfa.cpp:51
std::u32string getShortestWord() const
Definition: dfa.cpp:329
builder & intersect(dfa const &other)
Makes the prospective DFA accept only words accepted also by another DFA.
Definition: dfa.cpp:950
builder & operator=(const builder &b)
Copy-assigns a builder by copying another one's private implementation object.
Definition: dfa.cpp:646
string findShortestUtf8Word(dfa const &d)
Same as above for a UTF-8-encoded word.
Definition: dfa.cpp:445
std::string getShortestUtf8Word() const
Definition: dfa.cpp:334
u32string findShortestWord(dfa const &d)
Searches the shortest UTF-32-encoded word accepted by a given DFA.
Definition: dfa.cpp:418
dfa build()
Builds the DFA, as defined by previous operations, including completion.
Definition: dfa.cpp:1052
std::string const & getLabelOf(size_t qi) const
Definition: dfa.cpp:339
std::vector< std::string > const & getUtf8Alphabet() const
Fetches this DFA's set of processable symbols as UTF-8-encoded strings.
Definition: dfa.cpp:371
builder & defineTransition(std::string const &from, std::string const &to, char32_t symbol)
(Re-)Defines a transition for the prospective DFA.
Definition: dfa.cpp:725
builder & addSymbol(char32_t symbol)
Adds a symbol to the prospective DFA's alphabet.
Definition: dfa.cpp:675
dfa & operator=(dfa const &d)
Copy-assigns this DFA by copying another one's private implementation object.
Definition: dfa.cpp:144