reglibcpp  1.0.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 namespace reg {
14 class nfa;
16 
22 class dfa {
23 public:
24  class builder;
25  dfa();
26  dfa(dfa& d);
27  dfa(dfa&& d);
28  dfa(builder& b);
29  dfa& operator=(const dfa& d);
30  dfa& operator=(dfa&& d);
31  virtual ~dfa ();
32 
33  bool operator==(const dfa& d) const;
34  bool operator!=(const dfa& d) const;
35  size_t delta(size_t q, char32_t symbol) const;
36  size_t delta(size_t q, std::string const& utf8Symbol) const;
37  size_t deltaHat(size_t q, std::u32string const& word) const;
38  size_t deltaHat(size_t q, std::string const& utf8Word) const;
39  std::string const& getLabelOf(size_t q) const;
40  std::vector<char32_t> const& getAlphabet() const;
41  std::vector<std::string> const& getUtf8Alphabet() const;
42  size_t getNumberOfStates() const;
43  bool isAccepting(size_t q) const;
45 
49  class builder {
50  public:
51  builder ();
52  builder (dfa const& dfa);
53  builder (nfa const& nfa);
54  builder(builder& b);
55  builder(builder&& b);
56  builder& operator=(const builder& b);
58  virtual ~builder ();
59 
60  builder& addSymbol(char32_t symbol);
61  builder& addSymbol(std::string const& utf8Symbol);
62  builder& setAccepting(std::string const& state, bool accept);
63  builder& makeInitial(std::string const& state);
64  builder& defineTransition(std::string const& from, std::string const& to, char32_t symbol);
65  builder& defineTransition(std::string const& from, std::string const& to, std::string const& utf8Symbol);
66  builder& merge();
67  builder& purge();
68  dfa build();
69  private:
70  struct pImpl;
71  std::unique_ptr<pImpl> p;
72  };
73 private:
74  struct pImpl;
75  std::unique_ptr<pImpl> p;
76  dfa(
77  std::vector<char32_t>& alphabet,
78  std::vector<std::vector<size_t>>& transitions,
79  std::vector<std::string>& labels,
80  std::valarray<bool>& acceptingStates
81  );
82 };
83 
84 } // namespace reg
85 #endif
bool isAccepting(size_t q) const
Tests whether a state is an accept state within this DFA.
Definition: dfa.cpp:315
bool operator!=(const dfa &d) const
Tests whether this DFA doesn&#39;t accept the same language as another one.
Definition: dfa.cpp:214
builder & setAccepting(std::string const &state, bool accept)
Sets whether or not a state will be accepting within the prospective DFA.
Definition: dfa.cpp:552
size_t delta(size_t q, char32_t symbol) const
Computes this DFA&#39;s transition function for a state and a symbol.
Definition: dfa.cpp:223
dfa()
Constructs a DFA accepting the empty language ∅.
Definition: dfa.cpp:107
Represents nondeterministic finite automata with ε-moves.
Definition: nfa.h:22
Represents deterministic finite automata.
Definition: dfa.h:22
Private implementation details of DFAs.
Definition: dfa.cpp:35
builder & purge()
Purges the prospective DFA of unreachable and non-producing states, allowing for minimization.
Definition: dfa.cpp:663
std::vector< char32_t > const & getAlphabet() const
Fetches this DFA&#39;s set of processable symbols.
Definition: dfa.cpp:287
dfa & operator=(const dfa &d)
Copy-assigns this DFA by copying another one&#39;s private implementation object.
Definition: dfa.cpp:127
Private implementation details of DFA builders.
Definition: dfa.cpp:320
bool operator==(const dfa &d) const
Tests whether this DFA accepts exactly the same language as another one.
Definition: dfa.cpp:152
builder & makeInitial(std::string const &state)
Resets the initial state for the prospective DFA.
Definition: dfa.cpp:571
builder & merge()
Merges the prospective DFA&#39;s indistinguishable states, allowing for minimization. ...
Definition: dfa.cpp:611
Constructs DFAs step by step.
Definition: dfa.h:49
size_t deltaHat(size_t q, std::u32string const &word) const
Computes this DFA&#39;s transition function recursively for a string of symbols, starting in a specified ...
Definition: dfa.cpp:261
size_t getNumberOfStates() const
Counts this DFA&#39;s states.
Definition: dfa.cpp:305
builder()
Constructs a blank builder object.
Definition: dfa.cpp:437
Definition: dfa.cpp:32
builder & operator=(const builder &b)
Copy-assigns a builder by copying another one&#39;s private implementation object.
Definition: dfa.cpp:510
std::string const & getLabelOf(size_t q) const
Puts a name to an index.
Definition: dfa.cpp:278
dfa build()
Builds the DFA, as defined by previous operations, including completion.
Definition: dfa.cpp:726
std::vector< std::string > const & getUtf8Alphabet() const
Fetches this DFA&#39;s set of processable symbols as UTF-8-encoded strings.
Definition: dfa.cpp:296
builder & defineTransition(std::string const &from, std::string const &to, char32_t symbol)
(Re-)Defines a transition for the prospective DFA.
Definition: dfa.cpp:587
builder & addSymbol(char32_t symbol)
Adds a symbol to the prospective DFA&#39;s alphabet.
Definition: dfa.cpp:535