|
reglibcpp
1.7.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 existing state names. 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 & | 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, 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 |
| std::string | getShortestUtf8Word () const |
| std::string const & | getLabelOf (size_t q) const |
| std::string | getLabelOf (std::valarray< bool > const &qs) const |
| std::string | getLabelOf (state_set const &qs) const |
| 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< 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 |
| size_t | getNumberOfSymbols () const |
| 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... | |
| bool | hasAccepting (std::valarray< bool > const &qs) const |
| bool | hasAccepting (state_set const &qs) const |
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... | |
Friends | |
| std::u32string | findShortestWord (nfa const &n) |
| Searches the shortest UTF-32-encoded word accepted by a given NFA. More... | |
| std::string | findShortestUtf8Word (nfa const &n) |
| Same as above for a UTF-8-encoded word. 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.
|
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 621 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 384 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 108 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 126 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 135 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 147 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 156 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 168 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 178 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 190 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 199 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 210 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 227 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 238 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 243 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 401 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 263 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 301 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 314 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 330 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 413 of file nfa.cpp.
| string const & reg::nfa::getLabelOf | ( | size_t | qi | ) | const |
| string reg::nfa::getLabelOf | ( | std::valarray< bool > const & | qs | ) | const |
Definition at line 370 of file nfa.cpp.
| string reg::nfa::getLabelOf | ( | nfa::state_set const & | qs | ) | const |
Definition at line 375 of file nfa.cpp.
| size_t reg::nfa::getNumberOfStates | ( | ) | const |
Definition at line 458 of file nfa.cpp.
| size_t reg::nfa::getNumberOfSymbols | ( | ) | const |
Definition at line 463 of file nfa.cpp.
| string reg::nfa::getShortestUtf8Word | ( | ) | const |
Definition at line 253 of file nfa.cpp.
| u32string reg::nfa::getShortestWord | ( | ) | const |
Definition at line 248 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 437 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 429 of file nfa.cpp.
| bool reg::nfa::hasAccepting | ( | std::valarray< bool > const & | qs | ) | const |
Definition at line 524 of file nfa.cpp.
| bool reg::nfa::hasAccepting | ( | nfa::state_set const & | qs | ) | const |
Definition at line 529 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 473 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 486 of file nfa.cpp.
| 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.
| qs | set of labels of states in question |
true if the set of states contains an accept states, false else Definition at line 514 of file nfa.cpp.
| 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 |
|
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 |
| string reg::nfa::to_string | ( | std::valarray< bool > const & | qs | ) | const |
| string reg::nfa::to_string | ( | 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 365 of file nfa.cpp.
|
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 |
|
friend |
Same as above for a UTF-8-encoded word.
Definition at line 568 of file nfa.cpp.
|
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 |
Definition at line 539 of file nfa.cpp.
1.8.14