reglibcpp  2.0.0
A C++ implementation of models for regular languages
nfa.h
Go to the documentation of this file.
1 // Copyright 2017, 2018 Tom Kranz
2 //
3 // This file is part of reglibcpp.
4 //
5 // reglibcpp is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // reglibcpp is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with reglibcpp. If not, see <https://www.gnu.org/licenses/>.
17 
18 #ifndef REG_NFA_H
19 #define REG_NFA_H
20 
22 #include <vector>
23 
24 #include <string>
25 
26 #include "utils.h"
27 
28 namespace reg {
29 class fabuilder;
30 class dfa;
32 
38 class nfa {
39 public:
44  typedef std::unordered_set<
48  nfa();
49  nfa(
50  std::vector<char32_t>&& alphabet,
52  std::vector<std::string>&& labels,
53  std::valarray<bool>&& acceptingStates
54  );
55  nfa(fabuilder& b);
56  nfa(nfa const& n);
57  nfa(nfa&& n);
58  nfa& operator=(nfa const& n);
59  nfa& operator=(nfa&& n);
60  virtual ~nfa ();
61 
62  operator dfa const&() const;
64  bool operator==(nfa const& n) const;
65  bool operator!=(nfa const& n) const;
66  std::valarray<bool> const& delta(size_t qi, size_t si) const;
67  std::valarray<bool> const& delta(size_t qi, std::string const& symbol) const;
68  state_set delta(std::string const& q, std::string const& symbol) const;
69  std::valarray<bool> const& delta_(size_t qi, char32_t u32symbol) const;
70  state_set delta_(std::string const& q, char32_t u32symbol) const;
71  std::valarray<bool> deltaHat(size_t qi, std::string const& word) const;
72  state_set deltaHat(std::string const& q, std::string const& word) const;
74  std::valarray<bool> const& qs, std::string const& word) const;
75  state_set deltaHat(state_set const& qs, std::string const& word) const;
76  std::valarray<bool> deltaHat_(size_t qi, std::u32string const& u32word) const;
78  std::string const& q, std::u32string const& u32word) const;
80  std::valarray<bool> const& qs, std::u32string const& u32word) const;
81  state_set deltaHat_(state_set const& qs, std::u32string const& u32word) const;
82  std::valarray<bool> const& epsilonClosure(size_t qi) const;
83  state_set epsilonClosure(std::string const& q) const;
85  state_set epsilonClosure(state_set const& qs) const;
86  std::string to_string(std::valarray<bool> const& qs) const;
87  std::string to_string(state_set const& qs) const;
88  state_set decodeSet(std::valarray<bool> const& qs) const;
89  std::valarray<bool> encodeSet(state_set const& qs) const;
90  std::string const& getInitialState() const;
91  std::vector<std::string> const& getStates() const;
92  state_set getStatesSet() const;
95  std::vector<char32_t> const& getAlphabet_() const;
96  bool isAccepting(size_t qi) const;
97  bool isAccepting(std::string const& q) const;
98  bool isAccepting(std::valarray<bool> const& qs) const;
99  bool isAccepting(state_set const& qs) const;
100  static fabuilder unite(nfa const& n1, nfa const& n2);
101  static fabuilder intersect(nfa const& n1, nfa const& n2);
102  static fabuilder subtract(nfa const& n1, nfa const& n2);
103  static fabuilder complement(nfa const& n);
104  friend std::string findShortestWord(nfa const& n);
105  friend std::u32string findShortestWord_(nfa const& n);
106 
110  inline size_t operator()(
112  return std::hash<std::string>()(ref.get());
113  }
114  };
118  inline bool operator()(
121  return &(lhs.get()) == &(rhs.get()) && lhs.get() == rhs.get();
122  }
123  };
124 private:
125  struct impl;
127 };
128 std::string findShortestWord(nfa const& n);
129 std::u32string findShortestWord_(nfa const& n);
130 } // namespace reg
131 
132 #endif
u32string findShortestWord_(dfa const &d)
Searches the shortest UTF-32-encoded word accepted by a given DFA.
Definition: dfa.cpp:447
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:41
string findShortestWord(dfa const &d)
Searches the shortest UTF-8-encoded word accepted by a given DFA.
Definition: dfa.cpp:483
friend std::string findShortestWord(nfa const &n)
Same as above for a UTF-8-encoded word.
Definition: nfa.cpp:706
Represents nondeterministic finite automata with ε-moves.
Definition: nfa.h:38
static fabuilder 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:750
Represents deterministic finite automata.
Definition: dfa.h:41
bool operator==(nfa const &n) const
Checks whether this NFA describes the same regular language as another object.
Definition: nfa.cpp:123
std::vector< char32_t > const & getAlphabet_() const
Fetches this NFA's set of processable symbols.
Definition: nfa.cpp:598
size_t operator()(std::reference_wrapper< std::string const > const &ref) const
Delegates hashing to referenced objects.
Definition: nfa.h:110
std::string to_string(std::valarray< bool > const &qs) const
Puts a name to a set of indices.
Definition: nfa.cpp:506
nfa & operator=(nfa const &n)
Copy-assigns this NFA by copying another one's private implementation object.
Definition: nfa.cpp:784
Contains utility classes, objects and functions used throughout the library.
std::valarray< bool > encodeSet(state_set const &qs) const
Translates a set of states from set to bool representation.
Definition: nfa.cpp:555
bool operator!=(nfa const &n) const
Checks whether this NFA describes a different regular language than another object.
Definition: nfa.cpp:133
Provides std::unordered_set::key_eq for state_set.
Definition: nfa.h:116
state_set getStatesSet() const
Fetches this NFA's set of states as a set of (references to) strings.
Definition: nfa.cpp:580
std::valarray< bool > deltaHat(size_t qi, std::string const &word) const
Computes this DFA's transition function recursively for a UTF-8-encoded string of symbols...
Definition: nfa.cpp:275
static fabuilder 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:736
Provides std::unordered_set::hash_function for state_set.
Definition: nfa.h:108
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:538
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this NFA.
Definition: nfa.cpp:613
std::valarray< bool > const & delta_(size_t qi, char32_t u32symbol) const
Computes this NFA's transition function for a state index and a UTF-32-encoded symbol.
Definition: nfa.cpp:170
friend std::u32string findShortestWord_(nfa const &n)
Searches the shortest UTF-32-encoded word accepted by a given NFA.
Definition: nfa.cpp:674
Constructs NFAs step by step.
Definition: fabuilder.h:31
std::valarray< bool > getStatesBools() const
Fetches this NFA's set of states encoded as an array of bools.
Definition: nfa.cpp:589
Where this library lives.
Definition: dfa.cpp:48
nfa()
Constructs an NFA accepting the empty language ∅.
Definition: nfa.cpp:93
std::string const & getInitialState() const
Names this NFA's initial state.
Definition: nfa.cpp:567
static fabuilder 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:721
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:146
std::valarray< bool > const & epsilonClosure(size_t qi) const
Computes a state's ε-closure.
Definition: nfa.cpp:418
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:574
std::valarray< bool > deltaHat_(size_t qi, std::u32string const &u32word) const
Computes this NFA's transition function recursively for a string of symbols, starting in a state spec...
Definition: nfa.cpp:250
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:118
static fabuilder complement(nfa const &n)
Creates a builder for an NFA accepting the complement of the language of an NFA.
Definition: nfa.cpp:769
std::vector< std::string > const & getAlphabet() const
Fetches this NFA's set of processable symbols as UTF-8-encoded strings.
Definition: nfa.cpp:605