reglibcpp  1.3.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  class builder;
25  nfa();
26  nfa(nfa const& n);
27  nfa(nfa&& n);
28  nfa(dfa const& d);
29  nfa(builder& b);
30  nfa& operator=(nfa const& n);
31  nfa& operator=(nfa&& n);
32  virtual ~nfa ();
33 
34  bool operator==(nfa const& n) const;
35  bool operator!=(nfa const& n) const;
36  std::valarray<bool> const& delta(size_t q, char32_t symbol) const;
37  std::valarray<bool> const& delta(size_t q, std::string const& utf8Symbol) const;
38  std::valarray<bool> deltaHat(size_t q, std::u32string const& word) const;
39  std::valarray<bool> deltaHat(size_t q, std::string const& utf8Word) const;
40  std::valarray<bool> deltaHat(std::valarray<bool> const& qs, std::u32string const& word) const;
41  std::valarray<bool> deltaHat(std::valarray<bool> const& qs, std::string const& utf8Word) const;
42  std::valarray<bool> const& epsilonClosure(size_t q) const;
43  std::valarray<bool> epsilonClosure(std::valarray<bool> const& qs) const;
44  std::u32string getShortestWord() const;
45  std::string getShortestUtf8Word() const;
46  std::string const& getLabelOf(size_t q) const;
47  std::string getLabelOf(std::valarray<bool> const& qs) const;
48  std::vector<char32_t> const& getAlphabet() const;
49  std::vector<std::string> const& getUtf8Alphabet() const;
50  size_t getNumberOfStates() const;
51  bool isAccepting(size_t q) const;
52  bool hasAccepting(std::valarray<bool> const& qs) const;
53  static nfa::builder unite(nfa const& n1, nfa const& n2);
54  static nfa::builder intersect(nfa const& n1, nfa const& n2);
55  static nfa::builder subtract(nfa const& n1, nfa const& n2);
56  static nfa::builder complement(nfa const& n);
58 
62  class builder {
63  public:
64  builder();
65  builder(nfa const& nfa);
66  builder(dfa const& dfa);
67  builder(builder const& b);
68  builder(builder&& b);
69  builder& operator=(builder const& b);
71  virtual ~builder ();
72 
73  builder& addSymbol(char32_t symbol);
74  builder& addSymbol(std::string const& utf8Symbol);
75  builder& setAccepting(std::string const& state, bool accept);
76  builder& makeInitial(std::string const& state);
77  builder& addTransition(std::string const& from, std::string const& to, char32_t symbol);
78  builder& addTransition(std::string const& from, std::string const& to, std::string const& utf8Symbol);
79  builder& unite(nfa const& other);
80  builder& intersect(nfa const& other);
81  builder& normalizeStateNames(std::string const& prefix);
82  nfa build();
83  private:
84  struct pImpl;
85  std::unique_ptr<pImpl> p;
86  };
87 private:
88  struct pImpl;
89  std::unique_ptr<pImpl> p;
90  nfa(
91  std::vector<char32_t>& alphabet,
92  std::vector<std::vector<std::valarray<bool>>>& transitions,
93  std::vector<std::string>& labels,
94  std::valarray<bool>& acceptingStates
95  );
96 };
97 } // namespace reg
98 #endif
nfa build()
Builds the NFA, as defined by previous operations.
Definition: nfa.cpp:771
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:390
std::string getShortestUtf8Word() const
Same as above for a UTF-8-encoded word, INCLUDING POTENTIAL INFINITE LOOP.
Definition: nfa.cpp:208
Represents nondeterministic finite automata with ε-moves.
Definition: nfa.h:22
std::vector< char32_t > const & getAlphabet() const
Fetches this NFA's set of processable symbols.
Definition: nfa.cpp:298
std::string const & getLabelOf(size_t q) const
Puts a name to an index.
Definition: nfa.cpp:268
Represents deterministic finite automata.
Definition: dfa.h:26
std::vector< std::string > const & getUtf8Alphabet() const
Fetches this NFA's set of processable symbols as UTF-8-encoded strings.
Definition: nfa.cpp:306
std::valarray< bool > deltaHat(size_t q, std::u32string const &word) const
Computes this NFA's transition function recursively for a string of symbols, starting in a specified ...
Definition: nfa.cpp:132
Constructs NFAs step by step.
Definition: nfa.h:62
bool operator==(nfa const &n) const
Tests whether this NFA accepts exactly the same language as another one.
Definition: nfa.cpp:87
bool isAccepting(size_t q) const
Tests whether a state is an accept state within this NFA.
Definition: nfa.cpp:323
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:361
builder & unite(nfa const &other)
Makes the prospective NFA also accept every word of another NFA&#39;s language.
Definition: nfa.cpp:636
nfa & operator=(nfa const &n)
Copy-assigns this NFA by copying another one's private implementation object.
Definition: nfa.cpp:408
bool operator!=(nfa const &n) const
Tests whether this NFA doesn't accept the same language as another one.
Definition: nfa.cpp:92
builder & addSymbol(char32_t symbol)
Adds a symbol to the prospective NFA's alphabet.
Definition: nfa.cpp:529
builder & setAccepting(std::string const &state, bool accept)
Sets whether or not a state will be accepting within the prospective NFA.
Definition: nfa.cpp:545
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:592
std::u32string getShortestWord() const
Searches the shortest UTF-32-encoded word accepted by this NFA.
Definition: nfa.cpp:183
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:349
Private implementation details of NFAs.
Definition: nfa.cpp:29
builder & makeInitial(std::string const &state)
Resets the initial state for the prospective NFA.
Definition: nfa.cpp:563
builder & intersect(nfa const &other)
Makes the prospective NFA accept only words accepted also by another NFA.
Definition: nfa.cpp:694
Private implementation details of NFA builders.
Definition: nfa.cpp:430
size_t getNumberOfStates() const
Counts this NFA's states.
Definition: nfa.cpp:314
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:373
bool hasAccepting(std::valarray< bool > const &qs) const
Tests whether a set of states contains an accept state within this NFA.
Definition: nfa.cpp:332
Definition: dfa.cpp:29
nfa()
Constructs an NFA accepting the empty language ∅.
Definition: nfa.cpp:79
builder & operator=(builder const &b)
Copy-assigns a builder by copying another one's private implementation object.
Definition: nfa.cpp:507
builder & addTransition(std::string const &from, std::string const &to, char32_t symbol)
Adds a transition for the prospective NFA.
Definition: nfa.cpp:576
std::valarray< bool > const & delta(size_t q, char32_t symbol) const
Computes this NFA's transition function for a state and a symbol.
Definition: nfa.cpp:102
std::valarray< bool > const & epsilonClosure(size_t q) const
Computes a state's ε-closure.
Definition: nfa.cpp:217
builder()
Constructs a blank builder object.
Definition: nfa.cpp:455