reglibcpp  1.5.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 #include <unordered_set>
14 
15 namespace reg {
16 
17 class dfa;
19 
25 class nfa {
26 public:
31  class builder;
32  nfa();
33  nfa(nfa const& n);
34  nfa(nfa&& n);
35  nfa(dfa const& d);
36  nfa(builder& b);
37  nfa& operator=(nfa const& n);
38  nfa& operator=(nfa&& n);
39  virtual ~nfa ();
40 
41  bool operator==(nfa const& n) const;
42  bool operator!=(nfa const& n) const;
43  std::valarray<bool> const& delta(size_t qi, size_t si) const;
44  std::valarray<bool> const& delta(size_t qi, char32_t symbol) const;
45  std::valarray<bool> const& delta(size_t qi, std::string const& utf8Symbol) const;
46  state_set delta(std::string const& q, char32_t symbol) const;
47  state_set delta(std::string const& q, std::string const& utf8Symbol) const;
48  std::valarray<bool> deltaHat(size_t qi, std::u32string const& word) const;
49  std::valarray<bool> deltaHat(size_t qi, std::string const& utf8Word) const;
50  state_set deltaHat(std::string const& q, std::u32string const& word) const;
51  state_set deltaHat(std::string const& q, std::string const& utf8Word) const;
52  std::valarray<bool> deltaHat(std::valarray<bool> const& qs, std::u32string const& word) const;
53  std::valarray<bool> deltaHat(std::valarray<bool> const& qs, std::string const& utf8Word) const;
54  state_set deltaHat(state_set const& qs, std::u32string const& word) const;
55  state_set deltaHat(state_set const& qs, std::string const& utf8Word) const;
56  std::valarray<bool> const& epsilonClosure(size_t qi) const;
57  state_set epsilonClosure(std::string const& q) const;
59  state_set epsilonClosure(state_set const& qs) const;
62  std::string const& getLabelOf(size_t q) const;
64  std::string getLabelOf(state_set const& qs) const;
65  state_set decodeSet(std::valarray<bool> const& qs) const;
66  std::valarray<bool> encodeSet(state_set const& qs) const;
67  std::string const& getInitialState() const;
68  std::vector<std::string> const& getStates() const;
69  state_set getStatesSet() const;
71  std::vector<char32_t> const& getAlphabet() const;
73  size_t getNumberOfStates() const;
74  size_t getNumberOfSymbols() const;
75  bool isAccepting(size_t qi) const;
76  bool isAccepting(std::string const& q) const;
77  bool hasAccepting(std::valarray<bool> const& qs) const;
78  bool hasAccepting(state_set const& qs) const;
79  static nfa::builder unite(nfa const& n1, nfa const& n2);
80  static nfa::builder intersect(nfa const& n1, nfa const& n2);
81  static nfa::builder subtract(nfa const& n1, nfa const& n2);
82  static nfa::builder complement(nfa const& n);
84 
88  class builder {
89  public:
90  builder();
91  builder(nfa const& nfa);
92  builder(dfa const& dfa);
93  builder(builder const& b);
94  builder(builder&& b);
95  builder& operator=(builder const& b);
97  virtual ~builder ();
98 
99  builder& addSymbol(char32_t symbol);
100  builder& addSymbol(std::string const& utf8Symbol);
101  builder& setAccepting(std::string const& state, bool accept);
102  builder& makeInitial(std::string const& state);
103  builder& addTransition(std::string const& from, std::string const& to, char32_t symbol);
104  builder& addTransition(std::string const& from, std::string const& to, std::string const& utf8Symbol);
105  builder& unite(nfa const& other);
106  builder& intersect(nfa const& other);
107  builder& normalizeStateNames(std::string const& prefix);
108  nfa build();
109  private:
110  struct pImpl;
112  };
117  return std::hash<std::string>()(ref.get());
118  }
119  };
124  return lhs.get() == rhs.get();
125  }
126  };
127 private:
128  struct pImpl;
130  nfa(
131  std::vector<char32_t>& alphabet,
133  std::vector<std::string>& labels,
134  std::valarray<bool>& acceptingStates
135  );
136 };
137 std::u32string findShortestWord(nfa const& n);
138 std::string findShortestUtf8Word(nfa const& n);
139 } // namespace reg
140 
141 #endif
nfa build()
Builds the NFA, as defined by previous operations.
Definition: nfa.cpp:999
static nfa::builder complement(nfa const &n)
Creates a builder for an NFA accepting the complement of the language of an NFA.
Definition: nfa.cpp:616
std::string getShortestUtf8Word() const
Same as above for a UTF-8-encoded word.
Definition: nfa.cpp:277
size_t getNumberOfSymbols() const
Counts this NFA's alphabet symbols.
Definition: nfa.cpp:490
Represents nondeterministic finite automata with ε-moves.
Definition: nfa.h:25
std::vector< char32_t > const & getAlphabet() const
Fetches this NFA's set of processable symbols.
Definition: nfa.cpp:466
std::string const & getLabelOf(size_t q) const
Puts a name to an index.
Definition: nfa.cpp:366
Represents deterministic finite automata.
Definition: dfa.h:27
std::vector< std::string > const & getUtf8Alphabet() const
Fetches this NFA's set of processable symbols as UTF-8-encoded strings.
Definition: nfa.cpp:474
Constructs NFAs step by step.
Definition: nfa.h:88
bool operator==(nfa const &n) const
Tests whether this NFA accepts exactly the same language as another one.
Definition: nfa.cpp:88
static nfa::builder intersect(nfa const &n1, nfa const &n2)
Creates a builder for an NFA accepting the intersection of the languages of two NFAs.
Definition: nfa.cpp:587
size_t operator()(std::reference_wrapper< std::string const > const &ref) const
Delegates hashing to referenced objects.
Definition: nfa.h:116
builder & unite(nfa const &other)
Makes the prospective NFA also accept every word of another NFA&#39;s language.
Definition: nfa.cpp:864
nfa & operator=(nfa const &n)
Copy-assigns this NFA by copying another one's private implementation object.
Definition: nfa.cpp:634
std::valarray< bool > encodeSet(state_set const &qs) const
Translates a set of states from set to bool representation.
Definition: nfa.cpp:422
bool operator!=(nfa const &n) const
Tests whether this NFA doesn't accept the same language as another one.
Definition: nfa.cpp:93
builder & addSymbol(char32_t symbol)
Adds a symbol to the prospective NFA's alphabet.
Definition: nfa.cpp:757
Provides std::unordered_set::key_eq for state_set.
Definition: nfa.h:121
state_set getStatesSet() const
Fetches this NFA's set of states as a set of (references to) strings.
Definition: nfa.cpp:450
builder & setAccepting(std::string const &state, bool accept)
Sets whether or not a state will be accepting within the prospective NFA.
Definition: nfa.cpp:773
builder & normalizeStateNames(std::string const &prefix)
Reduces the prospective NFA&#39;s state names to consecutive numbers, prefixed with a given string...
Definition: nfa.cpp:820
Provides std::unordered_set::hash_function for state_set.
Definition: nfa.h:114
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:405
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this NFA.
Definition: nfa.cpp:500
std::u32string getShortestWord() const
Searches the shortest UTF-32-encoded word accepted by this NFA.
Definition: nfa.cpp:249
static nfa::builder unite(nfa const &n1, nfa const &n2)
Creates a builder for an NFA accepting the union of the languages of two NFAs.
Definition: nfa.cpp:575
std::unordered_set< std::reference_wrapper< std::string const >, hash_reference_string_const, equal_to_reference_string_const > state_set
Nicer name for sets of names of states. Should store references to names in getStates.
Definition: nfa.h:28
builder & makeInitial(std::string const &state)
Resets the initial state for the prospective NFA.
Definition: nfa.cpp:791
builder & intersect(nfa const &other)
Makes the prospective NFA accept only words accepted also by another NFA.
Definition: nfa.cpp:922
Private implementation details of NFA builders.
Definition: nfa.cpp:657
size_t getNumberOfStates() const
Counts this NFA's states.
Definition: nfa.cpp:482
static nfa::builder subtract(nfa const &n1, nfa const &n2)
Creates a builder for an NFA accepting the set difference of the languages of two NFAs...
Definition: nfa.cpp:599
std::valarray< bool > getStatesBools() const
Fetches this NFA's set of states encoded as an array of bools.
Definition: nfa.cpp:458
std::valarray< bool > deltaHat(size_t qi, std::u32string const &word) const
Computes this NFA's transition function recursively for a string of symbols, starting in a state spec...
Definition: nfa.cpp:165
bool hasAccepting(std::valarray< bool > const &qs) const
Tests whether a set of states contains an accept state within this NFA.
Definition: nfa.cpp:528
Where this library lives.
Definition: dfa.cpp:51
nfa()
Constructs an NFA accepting the empty language ∅.
Definition: nfa.cpp:80
string findShortestUtf8Word(dfa const &d)
Same as above for a UTF-8-encoded word.
Definition: dfa.cpp:447
builder & operator=(builder const &b)
Copy-assigns a builder by copying another one's private implementation object.
Definition: nfa.cpp:735
std::string const & getInitialState() const
Names this NFA's initial state.
Definition: nfa.cpp:434
u32string findShortestWord(dfa const &d)
Searches the shortest UTF-32-encoded word accepted by a given DFA.
Definition: dfa.cpp:442
builder & addTransition(std::string const &from, std::string const &to, char32_t symbol)
Adds a transition for the prospective NFA.
Definition: nfa.cpp:804
std::valarray< bool > const & delta(size_t qi, size_t si) const
Computes this NFA's transition function for a state index and a symbol index.
Definition: nfa.cpp:105
std::valarray< bool > const & epsilonClosure(size_t qi) const
Computes a state's ε-closure.
Definition: nfa.cpp:287
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:442
bool operator()(std::reference_wrapper< std::string const > const &lhs, std::reference_wrapper< std::string const > const &rhs) const
Delegates equality check to referenced objects.
Definition: nfa.h:123
builder()
Constructs a blank builder object.
Definition: nfa.cpp:682