reglibcpp  1.0.0
(Naïve) C++ implementation of models for regular languages
nfa.h
Go to the documentation of this file.
1 #ifndef REG_NFA_H
2 #define REG_NFA_H
3 
5 #include <memory>
6 
7 #include <vector>
8 
9 #include <valarray>
10 
11 #include <string>
12 
13 namespace reg {
14 class dfa;
16 
22 class nfa {
23 public:
24  nfa();
25  nfa(nfa const& n);
26  nfa(nfa&& n);
27  nfa& operator=(nfa const& n);
28  nfa& operator=(nfa&& n);
29  virtual ~nfa ();
30 
31  bool operator==(nfa const& n) const;
32  bool operator!=(nfa const& n) const;
33  std::valarray<bool> const& delta(size_t q, char32_t symbol) const;
34  std::valarray<bool> const& delta(size_t q, std::string const& utf8Symbol) const;
35  std::valarray<bool> deltaHat(size_t q, std::u32string const& word) const;
36  std::valarray<bool> deltaHat(size_t q, std::string const& utf8Word) const;
37  std::valarray<bool> deltaHat(std::valarray<bool> const& qs, std::u32string const& word) const;
38  std::valarray<bool> deltaHat(std::valarray<bool> const& qs, std::string const& utf8Word) const;
39  std::valarray<bool> const& epsilonClosure(size_t q) const;
40  std::valarray<bool> epsilonClosure(std::valarray<bool> const& qs) const;
41  std::string const& getLabelOf(size_t q) const;
42  std::string getLabelOf(std::valarray<bool> qs) const;
43  std::vector<char32_t> const& getAlphabet() const;
44  std::vector<std::string> const& getUtf8Alphabet() const;
45  size_t getNumberOfStates() const;
46  bool isAccepting(size_t q) const;
48 
52  class builder {
53  public:
54  builder();
55  builder(nfa const& nfa);
56  builder(dfa const& dfa);
57  builder(builder& b);
58  builder(builder&& b);
59  builder& operator=(builder const& b);
61  virtual ~builder ();
62 
63  builder& addSymbol(char32_t symbol);
64  builder& addSymbol(std::string const& utf8Symbol);
65  builder& setAccepting(std::string const& state, bool accept);
66  builder& makeInitial(std::string const& state);
67  builder& addTransition(std::string const& from, std::string const& to, char32_t symbol);
68  builder& addTransition(std::string const& from, std::string const& to, std::string const& utf8Symbol);
69  nfa build();
70  private:
71  struct pImpl;
72  std::unique_ptr<pImpl> p;
73  };
74 private:
75  struct pImpl;
76  std::unique_ptr<pImpl> p;
77  nfa(
78  std::vector<char32_t>& alphabet,
79  std::vector<std::vector<std::valarray<bool>>>& transitions,
80  std::vector<std::string>& labels,
81  std::valarray<bool>& acceptingStates
82  );
83 };
84 } // namespace reg
85 #endif
nfa build()
Builds the NFA, as defined by previous operations.
Definition: nfa.cpp:495
Represents nondeterministic finite automata with ε-moves.
Definition: nfa.h:22
std::vector< char32_t > const & getAlphabet() const
Fetches this NFA&#39;s set of processable symbols.
Definition: nfa.cpp:268
std::string const & getLabelOf(size_t q) const
Puts a name to an index.
Definition: nfa.cpp:237
Represents deterministic finite automata.
Definition: dfa.h:22
std::vector< std::string > const & getUtf8Alphabet() const
Fetches this NFA&#39;s set of processable symbols as UTF-8-encoded strings.
Definition: nfa.cpp:277
std::valarray< bool > deltaHat(size_t q, std::u32string const &word) const
Computes this NFA&#39;s transition function recursively for a string of symbols, starting in a specified ...
Definition: nfa.cpp:140
Constructs NFAs step by step.
Definition: nfa.h:52
bool operator==(nfa const &n) const
Tests whether this NFA accepts exactly the same language as another one.
Definition: nfa.cpp:90
bool isAccepting(size_t q) const
Tests whether a state is an accept state within this NFA.
Definition: nfa.cpp:296
nfa & operator=(nfa const &n)
Copy-assigns this NFA by copying another one&#39;s private implementation object.
Definition: nfa.cpp:308
bool operator!=(nfa const &n) const
Tests whether this NFA doesn&#39;t accept the same language as another one.
Definition: nfa.cpp:95
builder & addSymbol(char32_t symbol)
Adds a symbol to the prospective NFA&#39;s alphabet.
Definition: nfa.cpp:425
builder & setAccepting(std::string const &state, bool accept)
Sets whether or not a state will be accepting within the prospective NFA.
Definition: nfa.cpp:442
Private implementation details of NFAs.
Definition: nfa.cpp:31
builder & makeInitial(std::string const &state)
Resets the initial state for the prospective NFA.
Definition: nfa.cpp:461
Private implementation details of NFA builders.
Definition: nfa.cpp:328
size_t getNumberOfStates() const
Counts this NFA&#39;s states.
Definition: nfa.cpp:286
Definition: dfa.cpp:32
nfa()
Constructs an NFA accepting the empty language ∅.
Definition: nfa.cpp:82
builder & operator=(builder const &b)
Copy-assigns a builder by copying another one&#39;s private implementation object.
Definition: nfa.cpp:402
builder & addTransition(std::string const &from, std::string const &to, char32_t symbol)
Adds a transition for the prospective NFA.
Definition: nfa.cpp:475
std::valarray< bool > const & delta(size_t q, char32_t symbol) const
Computes this NFA&#39;s transition function for a state and a symbol.
Definition: nfa.cpp:106
std::valarray< bool > const & epsilonClosure(size_t q) const
Computes a state&#39;s ε-closure.
Definition: nfa.cpp:185
builder()
Constructs a blank builder object.
Definition: nfa.cpp:353