reglibcpp  1.7.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 <vector>
6 
7 #include <valarray>
8 
9 #include <string>
10 
11 #include "utils.h"
12 
13 namespace reg {
14 
15 class dfa;
17 
23 class nfa {
24 public:
29  class builder;
30  nfa();
31  nfa(nfa const& n);
32  nfa(nfa&& n);
33  nfa& operator=(nfa const& n);
34  nfa& operator=(nfa&& n);
35  virtual ~nfa ();
36 
37  operator dfa const&() const;
38  bool operator==(nfa const& n) const;
39  bool operator!=(nfa const& n) const;
40  std::valarray<bool> const& delta(size_t qi, size_t si) const;
41  std::valarray<bool> const& delta(size_t qi, char32_t symbol) const;
42  std::valarray<bool> const& delta(size_t qi, std::string const& utf8Symbol) const;
43  state_set delta(std::string const& q, char32_t symbol) const;
44  state_set delta(std::string const& q, std::string const& utf8Symbol) const;
45  std::valarray<bool> deltaHat(size_t qi, std::u32string const& word) const;
46  std::valarray<bool> deltaHat(size_t qi, std::string const& utf8Word) const;
47  state_set deltaHat(std::string const& q, std::u32string const& word) const;
48  state_set deltaHat(std::string const& q, std::string const& utf8Word) const;
49  std::valarray<bool> deltaHat(std::valarray<bool> const& qs, std::u32string const& word) const;
50  std::valarray<bool> deltaHat(std::valarray<bool> const& qs, std::string const& utf8Word) const;
51  state_set deltaHat(state_set const& qs, std::u32string const& word) const;
52  state_set deltaHat(state_set const& qs, std::string const& utf8Word) const;
53  std::valarray<bool> const& epsilonClosure(size_t qi) const;
54  state_set epsilonClosure(std::string const& q) const;
56  state_set epsilonClosure(state_set const& qs) const;
59  std::string const& getLabelOf(size_t q) const;
61  std::string getLabelOf(state_set const& qs) const;
62  std::string to_string(std::valarray<bool> const& qs) const;
63  std::string to_string(state_set const& qs) const;
64  state_set decodeSet(std::valarray<bool> const& qs) const;
65  std::valarray<bool> encodeSet(state_set const& qs) const;
66  std::string const& getInitialState() const;
67  std::vector<std::string> const& getStates() const;
68  state_set getStatesSet() const;
70  std::vector<char32_t> const& getAlphabet() const;
72  size_t getNumberOfStates() const;
73  size_t getNumberOfSymbols() const;
74  bool isAccepting(size_t qi) const;
75  bool isAccepting(std::string const& q) const;
76  bool isAccepting(std::valarray<bool> const& qs) const;
77  bool isAccepting(state_set const& qs) const;
78  bool hasAccepting(std::valarray<bool> const& qs) const;
79  bool hasAccepting(state_set const& qs) const;
80  static nfa::builder unite(nfa const& n1, nfa const& n2);
81  static nfa::builder intersect(nfa const& n1, nfa const& n2);
82  static nfa::builder subtract(nfa const& n1, nfa const& n2);
83  static nfa::builder complement(nfa const& n);
84  friend std::u32string findShortestWord(nfa const& n);
85  friend std::string findShortestUtf8Word(nfa const& n);
87 
91  class builder {
92  public:
93  builder();
94  builder(nfa const& nfa);
95  builder(dfa const& dfa);
96  builder(builder const& b);
97  builder(builder&& b);
98  builder& operator=(builder const& b);
100  virtual ~builder ();
101 
102  operator nfa();
103  builder& addSymbol(char32_t symbol);
104  builder& addSymbol(std::string const& utf8Symbol);
105  builder& setAccepting(std::string const& state, bool accept);
106  builder& makeInitial(std::string const& state);
107  builder& addTransition(std::string const& from, std::string const& to, char32_t symbol);
108  builder& addTransition(std::string const& from, std::string const& to, std::string const& utf8Symbol);
109  builder& unite(nfa const& other);
110  builder& intersect(nfa const& other);
111  builder& normalizeStateNames(std::string const& prefix);
112  nfa build();
113  private:
114  struct pImpl;
116  };
121  return std::hash<std::string>()(ref.get());
122  }
123  };
128  return lhs.get() == rhs.get();
129  }
130  };
131 private:
132  struct pImpl;
134  nfa(
135  std::vector<char32_t>& alphabet,
137  std::vector<std::string>& labels,
138  std::valarray<bool>& acceptingStates
139  );
140 };
141 std::u32string findShortestWord(nfa const& n);
142 std::string findShortestUtf8Word(nfa const& n);
143 } // namespace reg
144 
145 #endif
nfa build()
Builds the NFA, as defined by previous operations.
Definition: nfa.cpp:1003
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:621
std::string getShortestUtf8Word() const
Definition: nfa.cpp:253
size_t getNumberOfSymbols() const
Definition: nfa.cpp:463
Represents nondeterministic finite automata with ε-moves.
Definition: nfa.h:23
std::vector< char32_t > const & getAlphabet() const
Fetches this NFA's set of processable symbols.
Definition: nfa.cpp:445
std::string const & getLabelOf(size_t q) const
Definition: nfa.cpp:335
Represents deterministic finite automata.
Definition: dfa.h:21
std::vector< std::string > const & getUtf8Alphabet() const
Fetches this NFA's set of processable symbols as UTF-8-encoded strings.
Definition: nfa.cpp:453
Constructs NFAs step by step.
Definition: nfa.h:91
bool operator==(nfa const &n) const
Checks whether this NFA describes the same regular language as another object.
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:592
size_t operator()(std::reference_wrapper< std::string const > const &ref) const
Delegates hashing to referenced objects.
Definition: nfa.h:120
std::string to_string(std::valarray< bool > const &qs) const
Puts a name to a set of indices.
Definition: nfa.cpp:344
builder & unite(nfa const &other)
Makes the prospective NFA also accept every word of another NFA&#39;s language.
Definition: nfa.cpp:868
nfa & operator=(nfa const &n)
Copy-assigns this NFA by copying another one's private implementation object.
Definition: nfa.cpp:633
std::valarray< bool > encodeSet(state_set const &qs) const
Translates a set of states from set to bool representation.
Definition: nfa.cpp:401
bool operator!=(nfa const &n) const
Checks whether this NFA describes a different regular language than another object.
Definition: nfa.cpp:96
builder & addSymbol(char32_t symbol)
Adds a symbol to the prospective NFA's alphabet.
Definition: nfa.cpp:761
Provides std::unordered_set::key_eq for state_set.
Definition: nfa.h:125
state_set getStatesSet() const
Fetches this NFA's set of states as a set of (references to) strings.
Definition: nfa.cpp:429
builder & setAccepting(std::string const &state, bool accept)
Sets whether or not a state will be accepting within the prospective NFA.
Definition: nfa.cpp:777
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:824
Provides std::unordered_set::hash_function for state_set.
Definition: nfa.h:118
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:384
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this NFA.
Definition: nfa.cpp:473
std::u32string getShortestWord() const
Definition: nfa.cpp:248
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:580
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 existing state names...
Definition: nfa.h:26
builder & makeInitial(std::string const &state)
Resets the initial state for the prospective NFA.
Definition: nfa.cpp:795
builder & intersect(nfa const &other)
Makes the prospective NFA accept only words accepted also by another NFA.
Definition: nfa.cpp:926
Private implementation details of NFA builders.
Definition: nfa.cpp:656
size_t getNumberOfStates() const
Definition: nfa.cpp:458
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:604
friend std::u32string findShortestWord(nfa const &n)
Searches the shortest UTF-32-encoded word accepted by a given NFA.
Definition: nfa.cpp:539
std::valarray< bool > getStatesBools() const
Fetches this NFA's set of states encoded as an array of bools.
Definition: nfa.cpp:437
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:168
bool hasAccepting(std::valarray< bool > const &qs) const
Definition: nfa.cpp:524
Where this library lives.
Definition: dfa.cpp:51
nfa()
Constructs an NFA accepting the empty language ∅.
Definition: nfa.cpp:68
string findShortestUtf8Word(dfa const &d)
Same as above for a UTF-8-encoded word.
Definition: dfa.cpp:445
builder & operator=(builder const &b)
Copy-assigns a builder by copying another one's private implementation object.
Definition: nfa.cpp:734
std::string const & getInitialState() const
Names this NFA's initial state.
Definition: nfa.cpp:413
u32string findShortestWord(dfa const &d)
Searches the shortest UTF-32-encoded word accepted by a given DFA.
Definition: dfa.cpp:418
builder & addTransition(std::string const &from, std::string const &to, char32_t symbol)
Adds a transition for the prospective NFA.
Definition: nfa.cpp:808
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:108
std::valarray< bool > const & epsilonClosure(size_t qi) const
Computes a state's ε-closure.
Definition: nfa.cpp:263
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:421
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:127
builder()
Constructs a blank builder object.
Definition: nfa.cpp:681
friend std::string findShortestUtf8Word(nfa const &n)
Same as above for a UTF-8-encoded word.
Definition: nfa.cpp:568