reglibcpp
1.5.0
(Naïve) C++ implementation of models for regular languages
|
Represents nondeterministic finite automata with ε-moves. More...
#include <nfa.h>
Classes | |
class | builder |
Constructs NFAs step by step. More... | |
struct | equal_to_reference_string_const |
Provides std::unordered_set::key_eq for state_set. More... | |
struct | hash_reference_string_const |
Provides std::unordered_set::hash_function for state_set. More... | |
struct | pImpl |
Private implementation details of NFAs. More... | |
Public Types | |
typedef 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. More... | |
Public Member Functions | |
nfa () | |
Constructs an NFA accepting the empty language ∅. More... | |
nfa (nfa const &n) | |
Copy-constructs an NFA by copying another one's private implementation object. More... | |
nfa (nfa &&n) | |
Move-constructs an NFA by stealing another one's private implementation object. More... | |
nfa (dfa const &d) | |
Constructs an NFA from a DFA. More... | |
nfa (builder &b) | |
Constructs an NFA from a builder. More... | |
nfa & | operator= (nfa const &n) |
Copy-assigns this NFA by copying another one's private implementation object. More... | |
nfa & | operator= (nfa &&n) |
Move-assigns this NFA by stealing another one's private implementation object. More... | |
bool | operator== (nfa const &n) const |
Tests whether this NFA accepts exactly the same language as another one. More... | |
bool | operator!= (nfa const &n) const |
Tests whether this NFA doesn't accept the same language as another one. More... | |
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. More... | |
std::valarray< bool > const & | delta (size_t qi, char32_t symbol) const |
Computes this NFA's transition function for a state index and a symbol. More... | |
std::valarray< bool > const & | delta (size_t qi, std::string const &utf8Symbol) const |
Same as above for a UTF-8-encoded symbol. More... | |
state_set | delta (std::string const &q, char32_t symbol) const |
Computes this NFA's transition function for a state index and a symbol. More... | |
state_set | delta (std::string const &q, std::string const &utf8Symbol) const |
Same as above for a UTF-8-encoded symbol. More... | |
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 specified by its index. More... | |
std::valarray< bool > | deltaHat (size_t qi, std::string const &utf8Word) const |
Same as above for a UTF-8-encoded string of symbols. More... | |
state_set | deltaHat (std::string const &q, std::u32string const &word) const |
Computes this NFA's transition function recursively for a string of symbols, starting in a state specified by its name. More... | |
state_set | deltaHat (std::string const &q, std::string const &utf8Word) const |
Same as above for a UTF-8-encoded string of symbols. More... | |
std::valarray< bool > | deltaHat (std::valarray< bool > const &qs, std::u32string const &word) const |
Computes this NFA's transition function recursively for a string of symbols, starting in multiple specified states. More... | |
std::valarray< bool > | deltaHat (std::valarray< bool > const &qs, std::string const &utf8Word) const |
Same as above for a UTF-8-encoded string of symbols. More... | |
state_set | deltaHat (state_set const &qs, std::u32string const &word) const |
Computes this NFA's transition function recursively for a string of symbols, starting in multiple states specified by their names. More... | |
state_set | deltaHat (state_set const &qs, std::string const &utf8Word) const |
Same as above for a UTF-8-encoded string of symbols. More... | |
std::valarray< bool > const & | epsilonClosure (size_t qi) const |
Computes a state's ε-closure. More... | |
state_set | epsilonClosure (std::string const &q) const |
Computes a state's ε-closure. More... | |
std::valarray< bool > | epsilonClosure (std::valarray< bool > const &qs) const |
Computes the union of multiple states' ε-closures. More... | |
state_set | epsilonClosure (state_set const &qs) const |
Computes the union of multiple states' ε-closures. More... | |
std::u32string | getShortestWord () const |
Searches the shortest UTF-32-encoded word accepted by this NFA. More... | |
std::string | getShortestUtf8Word () const |
Same as above for a UTF-8-encoded word. More... | |
std::string const & | getLabelOf (size_t q) const |
Puts a name to an index. More... | |
std::string | getLabelOf (std::valarray< bool > const &qs) const |
Puts a name to a set of indices. More... | |
std::string | getLabelOf (state_set const &qs) const |
Puts a name to a set of states. More... | |
state_set | decodeSet (std::valarray< bool > const &qs) const |
Translates a set of states from bool to set representation. More... | |
std::valarray< bool > | encodeSet (state_set const &qs) const |
Translates a set of states from set to bool representation. More... | |
std::string const & | getInitialState () const |
Names this NFA's initial state. More... | |
std::vector< std::string > const & | getStates () const |
Fetches this NFA's set of states in order of internal representation. More... | |
state_set | getStatesSet () const |
Fetches this NFA's set of states as a set of (references to) strings. More... | |
std::valarray< bool > | getStatesBools () const |
Fetches this NFA's set of states encoded as an array of bools. More... | |
std::vector< char32_t > const & | getAlphabet () const |
Fetches this NFA's set of processable symbols. More... | |
std::vector< std::string > const & | getUtf8Alphabet () const |
Fetches this NFA's set of processable symbols as UTF-8-encoded strings. More... | |
size_t | getNumberOfStates () const |
Counts this NFA's states. More... | |
size_t | getNumberOfSymbols () const |
Counts this NFA's alphabet symbols. More... | |
bool | isAccepting (size_t qi) const |
Tests whether a state is an accept state within this NFA. More... | |
bool | isAccepting (std::string const &q) const |
Tests whether a state is an accept state within this NFA. More... | |
bool | hasAccepting (std::valarray< bool > const &qs) const |
Tests whether a set of states contains an accept state within this NFA. More... | |
bool | hasAccepting (state_set const &qs) const |
Tests whether a set of states contains an accept state within this NFA. More... | |
Static Public Member Functions | |
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. More... | |
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. More... | |
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. More... | |
static nfa::builder | complement (nfa const &n) |
Creates a builder for an NFA accepting the complement of the language of an NFA. More... | |
Represents nondeterministic finite automata with ε-moves.
Instances of this class are completely immutable. The builder class is the preferred way of constructing NFAs.
By convention, the state with index 0 is the initial state.
reg::nfa::nfa | ( | ) |
reg::nfa::nfa | ( | nfa const & | n | ) |
Copy-constructs an NFA by copying another one's private implementation object.
reg::nfa::nfa | ( | nfa && | n | ) |
Move-constructs an NFA by stealing another one's private implementation object.
The other NFA will be accepting the empty language ∅ afterwards.
reg::nfa::nfa | ( | dfa const & | d | ) |
reg::nfa::nfa | ( | nfa::builder & | b | ) |
|
static |
Creates a builder for an NFA accepting the complement of the language of an NFA.
The input NFAs' state names will probably be mangled in the created NFA.
n | the NFA |
Definition at line 616 of file nfa.cpp.
nfa::state_set reg::nfa::decodeSet | ( | std::valarray< bool > const & | qs | ) | const |
Translates a set of states from bool to set representation.
qs | valarray with true at indices of states in question |
Definition at line 405 of file nfa.cpp.
valarray< bool > const & reg::nfa::delta | ( | size_t | qi, |
size_t | si | ||
) | const |
Computes this NFA's transition function for a state index and a symbol index.
qi | the state's index (< getNumberOfStates) |
si | index of the symbol to process (∈ getAlphabet) |
true
at reached states' indices std::out_of_range | if qi ≥ getNumberOfStates |
std::domain_error | if si ≥ getNumberOfSymbols |
Definition at line 105 of file nfa.cpp.
valarray< bool > const & reg::nfa::delta | ( | size_t | qi, |
char32_t | symbol | ||
) | const |
Computes this NFA's transition function for a state index and a symbol.
qi | the state's index (< getNumberOfStates) |
symbol | the symbol to process (∈ getAlphabet) |
true
at reached states' indices std::out_of_range | if qi ≥ getNumberOfStates |
std::domain_error | if symbol ∉ getAlphabet |
Definition at line 123 of file nfa.cpp.
valarray< bool > const & reg::nfa::delta | ( | size_t | qi, |
std::string const & | utf8Symbol | ||
) | const |
Same as above for a UTF-8-encoded symbol.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 132 of file nfa.cpp.
nfa::state_set reg::nfa::delta | ( | std::string const & | q, |
char32_t | symbol | ||
) | const |
Computes this NFA's transition function for a state index and a symbol.
q | the state's label (∈ getStates) |
symbol | the symbol to process (∈ getAlphabet) |
std::out_of_range | if qi ≥ getNumberOfStates |
std::domain_error | if symbol ∉ getAlphabet |
Definition at line 144 of file nfa.cpp.
nfa::state_set reg::nfa::delta | ( | std::string const & | q, |
std::string const & | utf8Symbol | ||
) | const |
Same as above for a UTF-8-encoded symbol.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 153 of file nfa.cpp.
valarray< bool > reg::nfa::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 specified by its index.
qi | the starting state's index (< getNumberOfStates) |
word | the string of symbols to process (all of which ∈ getAlphabet) |
true
at reached states' indices std::out_of_range | if qi ≥ getNumberOfStates |
std::domain_error | if one the word symbols ∉ getAlphabet |
Definition at line 165 of file nfa.cpp.
valarray< bool > reg::nfa::deltaHat | ( | size_t | qi, |
std::string const & | utf8Word | ||
) | const |
Same as above for a UTF-8-encoded string of symbols.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 175 of file nfa.cpp.
nfa::state_set reg::nfa::deltaHat | ( | std::string const & | q, |
std::u32string const & | word | ||
) | const |
Computes this NFA's transition function recursively for a string of symbols, starting in a state specified by its name.
q | the starting state's label (∈ getStates) |
word | the string of symbols to process (all of which ∈ getAlphabet) |
std::out_of_range | if q ∉ getStates |
std::domain_error | if one the word symbols ∉ getAlphabet |
Definition at line 187 of file nfa.cpp.
nfa::state_set reg::nfa::deltaHat | ( | std::string const & | q, |
std::string const & | utf8Word | ||
) | const |
Same as above for a UTF-8-encoded string of symbols.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 196 of file nfa.cpp.
valarray< bool > reg::nfa::deltaHat | ( | std::valarray< bool > const & | qs, |
std::u32string const & | word | ||
) | const |
Computes this NFA's transition function recursively for a string of symbols, starting in multiple specified states.
qs | valarray with true at starting states' indices |
word | the string of symbols to process (all of which ∈ getAlphabet) |
true
at reached states' indices std::domain_error | if one of the word symbols ∉ getAlphabet |
Definition at line 207 of file nfa.cpp.
valarray< bool > reg::nfa::deltaHat | ( | std::valarray< bool > const & | qs, |
std::string const & | utf8Word | ||
) | const |
Same as above for a UTF-8-encoded string of symbols.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 224 of file nfa.cpp.
nfa::state_set reg::nfa::deltaHat | ( | nfa::state_set const & | qs, |
std::u32string const & | word | ||
) | const |
Computes this NFA's transition function recursively for a string of symbols, starting in multiple states specified by their names.
qs | set of starting states' labels |
word | the string of symbols to process (all of which ∈ getAlphabet) |
std::domain_error | if one of the word symbols ∉ getAlphabet |
Definition at line 235 of file nfa.cpp.
nfa::state_set reg::nfa::deltaHat | ( | nfa::state_set const & | qs, |
std::string const & | utf8Word | ||
) | const |
Same as above for a UTF-8-encoded string of symbols.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 240 of file nfa.cpp.
valarray< bool > reg::nfa::encodeSet | ( | nfa::state_set const & | qs | ) | const |
Translates a set of states from set to bool representation.
qs | set of labels of the states in question |
true
at indices of states in question Definition at line 422 of file nfa.cpp.
valarray< bool > const & reg::nfa::epsilonClosure | ( | size_t | qi | ) | const |
Computes a state's ε-closure.
qi | the state's index (< getNumberOfStates) |
true
at indices of states reachable without actual inputs std::out_of_range | if qi ≥ getNumberOfStates |
Definition at line 287 of file nfa.cpp.
nfa::state_set reg::nfa::epsilonClosure | ( | std::string const & | q | ) | const |
Computes a state's ε-closure.
q | the state's label (∈ getStates) |
std::out_of_range | if q ≥ getNumberOfStates |
Definition at line 327 of file nfa.cpp.
valarray< bool > reg::nfa::epsilonClosure | ( | std::valarray< bool > const & | qs | ) | const |
Computes the union of multiple states' ε-closures.
qs | valarray with true at indices of states in question |
true
at indices of states reachable without actual inputs Definition at line 340 of file nfa.cpp.
nfa::state_set reg::nfa::epsilonClosure | ( | nfa::state_set const & | qs | ) | const |
Computes the union of multiple states' ε-closures.
qs | set of labels of states in question |
Definition at line 356 of file nfa.cpp.
vector< char32_t > const & reg::nfa::getAlphabet | ( | ) | const |
string const & reg::nfa::getInitialState | ( | ) | const |
Names this NFA's initial state.
Definition at line 434 of file nfa.cpp.
string const & reg::nfa::getLabelOf | ( | size_t | qi | ) | const |
Puts a name to an index.
qi | the state's index (< getNumberOfStates) |
std::out_of_range | if q ≥ getNumberOfStates |
string reg::nfa::getLabelOf | ( | std::valarray< bool > const & | qs | ) | const |
Puts a name to a set of indices.
qs | valarray with true at indices of states in question |
Definition at line 375 of file nfa.cpp.
string reg::nfa::getLabelOf | ( | nfa::state_set const & | qs | ) | const |
Puts a name to a set of states.
qs | set of labels of states in question |
Definition at line 396 of file nfa.cpp.
size_t reg::nfa::getNumberOfStates | ( | ) | const |
Counts this NFA's states.
Definition at line 482 of file nfa.cpp.
size_t reg::nfa::getNumberOfSymbols | ( | ) | const |
Counts this NFA's alphabet symbols.
Definition at line 490 of file nfa.cpp.
string reg::nfa::getShortestUtf8Word | ( | ) | const |
Same as above for a UTF-8-encoded word.
Definition at line 277 of file nfa.cpp.
u32string reg::nfa::getShortestWord | ( | ) | const |
Searches the shortest UTF-32-encoded word accepted by this NFA.
std::logic_error | if the NFA doesn't accept any words |
Definition at line 249 of file nfa.cpp.
valarray< bool > reg::nfa::getStatesBools | ( | ) | const |
Fetches this NFA's set of states encoded as an array of bools.
true
values Definition at line 458 of file nfa.cpp.
nfa::state_set reg::nfa::getStatesSet | ( | ) | const |
Fetches this NFA's set of states as a set of (references to) strings.
Definition at line 450 of file nfa.cpp.
bool reg::nfa::hasAccepting | ( | std::valarray< bool > const & | qs | ) | const |
Tests whether a set of states contains an accept state within this NFA.
qs | valarray with true at indices of states in question |
true
if the set of states contains an accept states, false
else Definition at line 528 of file nfa.cpp.
bool reg::nfa::hasAccepting | ( | nfa::state_set const & | qs | ) | const |
Tests whether a set of states contains an accept state within this NFA.
qs | set of labels of states in question |
true
if the set of states contains an accept states, false
else Definition at line 543 of file nfa.cpp.
|
static |
Creates a builder for an NFA accepting the intersection of the languages of two NFAs.
The input NFAs' state names will be concatenated and collisions resolved by appending _
in the created NFA.
n1 | the first NFA |
n2 | the other NFA |
bool reg::nfa::isAccepting | ( | size_t | qi | ) | const |
Tests whether a state is an accept state within this NFA.
qi | the state's index (< getNumberOfStates) |
true
if the state is in the set of accept states, false
else std::out_of_range | if qi ≥ getNumberOfStates |
Definition at line 500 of file nfa.cpp.
bool reg::nfa::isAccepting | ( | std::string const & | q | ) | const |
Tests whether a state is an accept state within this NFA.
q | the state's label (∈ getStates) |
true
if the state is in the set of accept states, false
else std::out_of_range | if q ∉ getStates |
Definition at line 515 of file nfa.cpp.
bool reg::nfa::operator!= | ( | nfa const & | n | ) | const |
Copy-assigns this NFA by copying another one's private implementation object.
Move-assigns this NFA by stealing another one's private implementation object.
The other NFA will be accepting the empty language ∅ afterwards.
bool reg::nfa::operator== | ( | nfa const & | n | ) | const |
|
static |
Creates a builder for an NFA accepting the set difference of the languages of two NFAs.
The input NFAs' state names will probably be mangled in the created NFA.
n1 | the first NFA |
n2 | the other NFA |
|
static |
Creates a builder for an NFA accepting the union of the languages of two NFAs.
The input NFAs' state names will be prepended with either 1
or 2
in the created NFA.
n1 | the first NFA |
n2 | the other NFA |