reglibcpp
2.0.0
A C++ implementation of models for regular languages
|
Represents nondeterministic finite automata with ε-moves. More...
#include <nfa.h>
Classes | |
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 | impl |
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 existing state names. More... | |
Public Member Functions | |
nfa () | |
Constructs an NFA accepting the empty language ∅. More... | |
nfa (std::vector< char32_t > &&alphabet, std::vector< std::vector< std::valarray< bool >>> &&transitions, std::vector< std::string > &&labels, std::valarray< bool > &&acceptingStates) | |
Constructs NFA with a private implementation object with provided members. More... | |
nfa (fabuilder &b) | |
Constructs an NFA from a builder by calling its buils method. 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 & | 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... | |
operator dfa const & () const | |
Returns a DFA accepting the same language as this NFA. More... | |
bool | operator== (nfa const &n) const |
Checks whether this NFA describes the same regular language as another object. More... | |
bool | operator!= (nfa const &n) const |
Checks whether this NFA describes a different regular language than another object. 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, std::string const &symbol) const |
Computes this NFA's transition function for a state index and a UTF-8-encoded symbol. More... | |
state_set | delta (std::string const &q, std::string const &symbol) const |
Computes this NFA's transition function for a state label and a UTF-8-encoded symbol. More... | |
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. More... | |
state_set | delta_ (std::string const &q, char32_t u32symbol) const |
Computes this NFA's transition function for a state index and a UTF-32-encoded symbol. More... | |
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, starting in a state specified by its index. More... | |
state_set | deltaHat (std::string const &q, std::string const &word) const |
Computes this NFA's transition function recursively for a UTF-8-encoded string of symbols, starting in a state specified by its name. More... | |
std::valarray< bool > | deltaHat (std::valarray< bool > const &qs, std::string const &word) const |
Computes this NFA's transition function recursively for a string of UTF-8-encoded symbols, starting in multiple specified states. More... | |
state_set | deltaHat (state_set const &qs, std::string const &word) const |
Computes this NFA's transition function recursively for a string of UTF-8-encoded symbols, starting in multiple states specified by their names. More... | |
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 specified by its index. More... | |
state_set | deltaHat_ (std::string const &q, std::u32string const &u32word) const |
Computes this NFA's transition function recursively for a string of symbols, starting in a state specified by its name. More... | |
std::valarray< bool > | deltaHat_ (std::valarray< bool > const &qs, std::u32string const &u32word) const |
Computes this NFA's transition function recursively for a string of UTF-32-encoded symbols, starting in multiple specified states. More... | |
state_set | deltaHat_ (state_set const &qs, std::u32string const &u32word) const |
Computes this NFA's transition function recursively for a string of UTF-32-encoded symbols, starting in multiple states specified by their names. 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::string | to_string (std::valarray< bool > const &qs) const |
Puts a name to a set of indices. More... | |
std::string | to_string (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< std::string > const & | getAlphabet () const |
Fetches this NFA's set of processable symbols as UTF-8-encoded strings. More... | |
std::vector< char32_t > const & | getAlphabet_ () const |
Fetches this NFA's set of processable 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 | isAccepting (std::valarray< bool > const &qs) const |
Tests whether a set of states contains an accept state within this NFA. More... | |
bool | isAccepting (state_set const &qs) const |
Tests whether a set of states contains an accept state within this NFA. More... | |
Static Public Member Functions | |
static fabuilder | unite (nfa const &n1, nfa const &n2) |
Creates a builder for an NFA accepting the union of the languages of two NFAs. More... | |
static fabuilder | intersect (nfa const &n1, nfa const &n2) |
Creates a builder for an NFA accepting the intersection of the languages of two NFAs. More... | |
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. More... | |
static fabuilder | complement (nfa const &n) |
Creates a builder for an NFA accepting the complement of the language of an NFA. More... | |
Friends | |
std::string | findShortestWord (nfa const &n) |
Same as above for a UTF-8-encoded word. More... | |
std::u32string | findShortestWord_ (nfa const &n) |
Searches the shortest UTF-32-encoded word accepted by a given NFA. More... | |
Represents nondeterministic finite automata with ε-moves.
Instances of this class are completely immutable. The builder class is the preferred means to construct NFAs.
By convention, the state with index 0 is the initial state.
reg::nfa::nfa | ( | ) |
reg::nfa::nfa | ( | std::vector< char32_t > && | alphabet, |
std::vector< std::vector< std::valarray< bool >>> && | transitions, | ||
std::vector< std::string > && | labels, | ||
std::valarray< bool > && | acceptingStates | ||
) |
Constructs NFA with a private implementation object with provided members.
reg::nfa::nfa | ( | fabuilder & | b | ) |
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.
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 |
nfa::state_set reg::nfa::decodeSet | ( | std::valarray< bool > const & | qs | ) | const |
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 (< getStates().size() ) |
si | index of the symbol to process (< getAlphabet().size() ) |
true
at reached states' indices state_not_found | if qi ≥ getStates().size() |
symbol_not_found | if si ≥ getAlphabet().size() |
valarray< bool > const & reg::nfa::delta | ( | size_t | qi, |
std::string const & | symbol | ||
) | const |
Computes this NFA's transition function for a state index and a UTF-8-encoded symbol.
This converts symbol
to UTF-32 and calls delta_(size_t,char32_t) const with the std::u32string's first char32_t
. If you don't want that overhead and already have a char32_t
on your hands, use that method.
qi | the state's index (< getStates().size() ) |
symbol | the symbol to process (∈ getAlphabet()) |
true
at reached states' indices state_not_found | if qi ≥ getStates().size() |
symbol_not_found | if symbol ∉ getAlphabet() |
nfa::state_set reg::nfa::delta | ( | std::string const & | q, |
std::string const & | symbol | ||
) | const |
Computes this NFA's transition function for a state label and a UTF-8-encoded symbol.
This converts symbol
to UTF-32 and calls delta_(std::string const&,char32_t) const with the std::u32string's first char32_t
. If you don't want that overhead and already have a char32_t
on your hands, use that method.
q | the state's label (∈ getStates()) |
symbol | the symbol to process (∈ getAlphabet()) |
state_not_found | if q ∉ getStates() |
symbol_not_found | if symbol ∉ getAlphabet() |
valarray< bool > const & reg::nfa::delta_ | ( | size_t | qi, |
char32_t | u32symbol | ||
) | const |
Computes this NFA's transition function for a state index and a UTF-32-encoded symbol.
This looks up the index_of u32symbol
within getAlphabet_() and calls delta(size_t, size_t) const. If you don't want that overhead and already have a symbol index on your hands, use that method.
qi | the state's index (< getStates().size() ) |
u32symbol | the symbol to process (∈ getAlphabet_()) |
true
at reached states' indices state_not_found | if qi ≥ getStates().size() |
symbol_not_found | if u32symbol ∉ getAlphabet_() |
nfa::state_set reg::nfa::delta_ | ( | std::string const & | q, |
char32_t | u32symbol | ||
) | const |
Computes this NFA's transition function for a state index and a UTF-32-encoded symbol.
This looks up the index_of q
within getStates() and calls delta_(size_t,char32_t) const. If you don't want that overhead and already have a state index on your hands, use that method.
q | the state's label (∈ getStates) |
u32symbol | the symbol to process (∈ getAlphabet_()) |
state_not_found | if qi ≥ getStates().size() |
symbol_not_found | if u32symbol ∉ getAlphabet_() |
valarray< bool > reg::nfa::deltaHat | ( | size_t | qi, |
std::string const & | word | ||
) | const |
Computes this DFA's transition function recursively for a UTF-8-encoded string of symbols, starting in a state specified by its index.
This converts word
to UTF-32 and calls deltaHat_(size_t, std::u32string const&) const. If you don't want that overhead and already have a std::u32string on your hands, use that method.
qi | the starting state's index (< getStates().size() ) |
word | the string of symbols to process (all of which ∈ getAlphabet()) |
true
at reached states' indices state_not_found | if qi ≥ getStates().size() |
symbol_not_found | if one of the word symbols ∉ getAlphabet() |
nfa::state_set reg::nfa::deltaHat | ( | std::string const & | q, |
std::string const & | word | ||
) | const |
Computes this NFA's transition function recursively for a UTF-8-encoded string of symbols, starting in a state specified by its name.
This converts word
to UTF-32 and calls deltaHat_(std::string const&, std::u32string const&) const. If you don't want that overhead and already have a std::u32string on your hands, use that method.
q | the starting state's label (∈ getStates()) |
word | the string of symbols to process (all of which ∈ getAlphabet()) |
state_not_found | if q ∉ getStates() |
symbol_not_found | if one of the word symbols ∉ getAlphabet() |
valarray< bool > reg::nfa::deltaHat | ( | std::valarray< bool > const & | qs, |
std::string const & | word | ||
) | const |
Computes this NFA's transition function recursively for a string of UTF-8-encoded symbols, starting in multiple specified states.
This converts word
to UTF-32 and calls deltaHat_(std::valarray<bool> const&, std::u32string const&) const. If you don't want that overhead and already have a std::u32string on your hands, use that method.
qs | valarray with true at starting states' indices |
word | the string of symbols to process (all of which ∈ getAlphabet()) |
true
at reached states' indices symbol_not_found | if one of the word symbols ∉ getAlphabet() |
nfa::state_set reg::nfa::deltaHat | ( | nfa::state_set const & | qs, |
std::string const & | word | ||
) | const |
Computes this NFA's transition function recursively for a string of UTF-8-encoded symbols, starting in multiple states specified by their names.
This converts word
to UTF-32 and calls deltaHat_(std::valarray<bool> const&, std::u32string const&) const. If you don't want that overhead and already have a std::u32string on your hands, use that method.
qs | set of starting states' labels |
word | the string of symbols to process (all of which ∈ getAlphabet_()) |
symbol_not_found | if one of the u32word symbols ∉ getAlphabet_() |
valarray< bool > reg::nfa::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 specified by its index.
qi | the starting state's index (< getStates().size() ) |
u32word | the string of symbols to process (all of which ∈ getAlphabet_()) |
true
at reached states' indices state_not_found | if qi ≥ getStates().size() |
symbol_not_found | if one the u32word symbols ∉ getAlphabet_() |
nfa::state_set reg::nfa::deltaHat_ | ( | std::string const & | q, |
std::u32string const & | u32word | ||
) | const |
Computes this NFA's transition function recursively for a string of symbols, starting in a state specified by its name.
This looks up the index_of q
within getStates() and calls deltaHat_(size_t,std::u32string const&) const. If you don't want that overhead and already have a state index on your hands, use that method.
q | the starting state's label (∈ getStates()) |
u32word | the string of symbols to process (all of which ∈ getAlphabet_()) |
state_not_found | if q ∉ getStates |
symbol_not_found | if one the u32word symbols ∉ getAlphabet_() |
valarray< bool > reg::nfa::deltaHat_ | ( | std::valarray< bool > const & | qs, |
std::u32string const & | u32word | ||
) | const |
Computes this NFA's transition function recursively for a string of UTF-32-encoded symbols, starting in multiple specified states.
qs | valarray with true at starting states' indices |
u32word | the string of symbols to process (all of which ∈ getAlphabet_()) |
true
at reached states' indices symbol_not_found | if one of the u32word symbols ∉ getAlphabet_() |
nfa::state_set reg::nfa::deltaHat_ | ( | nfa::state_set const & | qs, |
std::u32string const & | u32word | ||
) | const |
Computes this NFA's transition function recursively for a string of UTF-32-encoded symbols, starting in multiple states specified by their names.
This encodes qs
in a std::valarray<bool> and calls deltaHat_(std::valarray<bool> const&, std::u32string const&) const. If you don't want that overhead and already have a std::valarray<bool> on your hands, use that method.
qs | set of starting states' labels |
u32word | the string of symbols to process (all of which ∈ getAlphabet_()) |
symbol_not_found | if one of the u32word symbols ∉ getAlphabet_() |
valarray< bool > reg::nfa::encodeSet | ( | nfa::state_set const & | qs | ) | const |
valarray< bool > const & reg::nfa::epsilonClosure | ( | size_t | qi | ) | const |
Computes a state's ε-closure.
qi | the state's index (< getStates().size() ) |
true
at indices of states reachable without actual inputs state_not_found | if qi ≥ getStates().size() |
nfa::state_set reg::nfa::epsilonClosure | ( | std::string const & | q | ) | const |
Computes a state's ε-closure.
This looks up the index_of q
within getStates() and calls epsilonClosure(size_t) const. If you don't want that overhead and already have a state index on your hands, use that method.
q | the state's label (∈ getStates) |
state_not_found | if q ≥ getStates().size() |
valarray< bool > reg::nfa::epsilonClosure | ( | std::valarray< bool > const & | qs | ) | const |
nfa::state_set reg::nfa::epsilonClosure | ( | nfa::state_set const & | qs | ) | const |
Computes the union of multiple states' ε-closures.
This encodes qs
in a std::valarray<bool> and calls epsilonClosure(std::valarray<bool> const&) const. If you don't want that overhead and already have a std::valarray<bool> on your hands, use that method.
qs | set of labels of states in question |
vector< char32_t > const & reg::nfa::getAlphabet_ | ( | ) | const |
string const & reg::nfa::getInitialState | ( | ) | const |
valarray< bool > reg::nfa::getStatesBools | ( | ) | const |
Fetches this NFA's set of states encoded as an array of bools.
.size()
, filled with true
values nfa::state_set reg::nfa::getStatesSet | ( | ) | const |
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 (< getStates().size() ) |
true
if the state is in the set of accept states, false
else state_not_found | if qi ≥ getStates().size() |
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 state_not_found | if q ∉ getStates |
bool reg::nfa::isAccepting | ( | std::valarray< bool > const & | qs | ) | const |
bool reg::nfa::isAccepting | ( | nfa::state_set const & | qs | ) | const |
Tests whether a set of states contains an accept state within this NFA.
This encodes qs
in a std::valarray<bool> and calls isAccepting(std::valarray<bool> const&) const. If you don't want that overhead and already have a std::valarray<bool> on your hands, use that method.
qs | set of labels of states in question |
true
if the set of states contains an accept states, false
else reg::nfa::operator dfa const & | ( | ) | const |
Returns a DFA accepting the same language as this NFA.
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 |
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 |
string reg::nfa::to_string | ( | std::valarray< bool > const & | qs | ) | const |
string reg::nfa::to_string | ( | nfa::state_set const & | qs | ) | const |
Creates a builder for an NFA accepting the union of the languages of two NFAs.
Concatenates the names of states defined so far with the other NFA's state names and resolves conflicts by appending _
.
n1 | the first NFA |
n2 | the other NFA |
|
friend |
|
friend |
Searches the shortest UTF-32-encoded word accepted by a given NFA.
n | the NFA |
std::logic_error | if the NFA doesn't accept any words |