reglibcpp
2.0.0
A C++ implementation of models for regular languages
|
Represents deterministic finite automata. More...
#include <dfa.h>
Classes | |
struct | impl |
Private implementation details of DFAs. More... | |
Public Member Functions | |
dfa () | |
Constructs a DFA accepting the empty language ∅. More... | |
dfa (std::vector< char32_t > &&alphabet, std::vector< std::vector< size_t >> &&transitions, std::vector< std::string > &&labels, std::valarray< bool > &&acceptingStates) | |
Constructs DFA with a private implementation object with provided members. More... | |
dfa (fabuilder &b, bool force=false) | |
Constructs a DFA from a builder by calling its build method. More... | |
dfa (dfa const &d) | |
Copy-constructs a DFA by copying another one's private implementation object. More... | |
dfa (dfa &&d) | |
Move-constructs a DFA by stealing another one's private implementation object. More... | |
dfa & | operator= (dfa const &d) |
Copy-assigns this DFA by copying another one's private implementation object. More... | |
dfa & | operator= (dfa &&d) |
Move-assigns this DFA by stealing another one's private implementation object. More... | |
operator nfa const & () const | |
Returns an NFA accepting the same language as this DFA. More... | |
bool | operator== (dfa const &d) const |
Tests whether this DFA accepts exactly the same language as another one. More... | |
bool | operator!= (dfa const &d) const |
Tests whether this DFA doesn't accept the same language as another one. More... | |
bool | operator== (nfa const &d) const |
Tests whether this DFA accepts exactly the same language as another object. More... | |
bool | operator!= (nfa const &d) const |
Tests whether this DFA doesn't accept the same language as another object. More... | |
size_t | delta (size_t qi, size_t si) const |
Computes this DFA's transition function for a state index and a symbol index. More... | |
size_t | delta (size_t qi, std::string const &symbol) const |
Computes this DFA's transition function for a state index and a UTF-8-encoded symbol. More... | |
std::string const & | delta (std::string const &q, std::string const &symbol) const |
Computes this DFA's transition function for a state label and a UTF-8-encoded symbol. More... | |
size_t | delta_ (size_t qi, char32_t u32symbol) const |
Computes this DFA's transition function for a state index and a UTF-32-encoded symbol. More... | |
std::string const & | delta_ (std::string const &q, char32_t u32symbol) const |
Computes this DFA's transition function for a state label and a UTF-32-encoded symbol. More... | |
size_t | 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... | |
std::string const & | deltaHat (std::string const &q, 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 name. More... | |
size_t | deltaHat_ (size_t qi, std::u32string const &u32word) const |
Computes this DFA's transition function recursively for a UTF-32-encoded string of symbols, starting in a state specified by its index. More... | |
std::string const & | deltaHat_ (std::string const &q, std::u32string const &u32word) const |
Computes this DFA's transition function recursively for a UTF-32-encoded string of symbols, starting in a state specified by its name. More... | |
std::string const & | getInitialState () const |
Names this DFA's initial state. More... | |
std::vector< std::string > const & | getStates () const |
Fetches this DFA's set of states. More... | |
std::vector< std::string > const & | getAlphabet () const |
Fetches this DFA's set of processable symbols as UTF-8-encoded strings. More... | |
std::vector< char32_t > const & | getAlphabet_ () const |
Fetches this DFA's set of processable symbols. More... | |
bool | isAccepting (size_t qi) const |
Tests whether a state is an accept state within this DFA. More... | |
bool | isAccepting (std::string const &q) const |
Tests whether a state is an accept state within this DFA. More... | |
Static Public Member Functions | |
static fabuilder | unite (dfa const &d1, dfa const &d2) |
Creates a builder for a DFA accepting the union of the languages of two DFAs. More... | |
static fabuilder | intersect (dfa const &d1, dfa const &d2) |
Creates a builder for a DFA accepting the intersection of the languages of two DFAs. More... | |
static fabuilder | subtract (dfa const &d1, dfa const &d2) |
Creates a builder for a DFA accepting the set difference of the languages of two DFAs. More... | |
static fabuilder | complement (dfa const &d) |
Creates a builder for a DFA accepting the complement of the language of a DFA. More... | |
static std::vector< std::valarray< bool > > | indistinguishableStates (std::vector< std::vector< size_t >> const &transitions, std::valarray< bool > const &accepting) |
Builds the table of indistinguishable states w.r.t. a transition function. More... | |
Friends | |
std::string | findShortestWord (dfa const &d) |
Searches the shortest UTF-8-encoded word accepted by a given DFA. More... | |
std::u32string | findShortestWord_ (dfa const &d) |
Searches the shortest UTF-32-encoded word accepted by a given DFA. More... | |
Represents deterministic finite automata.
Instances of this class are completely immutable. The builder class is the preferred way of constructing DFAs.
By convention, the state with index 0 is the initial state.
reg::dfa::dfa | ( | ) |
reg::dfa::dfa | ( | std::vector< char32_t > && | alphabet, |
std::vector< std::vector< size_t >> && | transitions, | ||
std::vector< std::string > && | labels, | ||
std::valarray< bool > && | acceptingStates | ||
) |
Constructs DFA with a private implementation object with provided members.
reg::dfa::dfa | ( | fabuilder & | b, |
bool | force = false |
||
) |
reg::dfa::dfa | ( | dfa const & | d | ) |
Copy-constructs a DFA by copying another one's private implementation object.
reg::dfa::dfa | ( | dfa && | d | ) |
Move-constructs a DFA by stealing another one's private implementation object.
The other DFA will be accepting the empty language ∅ afterwards.
Creates a builder for a DFA accepting the complement of the language of a DFA.
The input DFAs' state names will be retained in the created DFA.
d | the DFA |
size_t reg::dfa::delta | ( | size_t | qi, |
size_t | si | ||
) | const |
Computes this DFA'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() ) |
state_not_found | if qi ≥ getStates().size() |
symbol_not_found | if si ≥ getAlphabet().size() |
size_t reg::dfa::delta | ( | size_t | qi, |
std::string const & | symbol | ||
) | const |
Computes this DFA'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()) |
state_not_found | if qi ≥ getStates().size() |
symbol_not_found | if symbol ∉ getAlphabet() |
string const & reg::dfa::delta | ( | std::string const & | q, |
std::string const & | symbol | ||
) | const |
Computes this DFA'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() |
size_t reg::dfa::delta_ | ( | size_t | qi, |
char32_t | u32symbol | ||
) | const |
Computes this DFA'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_()) |
state_not_found | if qi ≥ getStates().size() |
symbol_not_found | if u32symbol ∉ getAlphabet_() |
string const & reg::dfa::delta_ | ( | std::string const & | q, |
char32_t | u32symbol | ||
) | const |
Computes this DFA's transition function for a state label 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 q ∉ getStates() |
symbol_not_found | if u32symbol ∉ getAlphabet_() |
size_t reg::dfa::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()) |
state_not_found | if qi ≥ getStates().size() |
symbol_not_found | if one of the word symbols ∉ getAlphabet() |
string const & reg::dfa::deltaHat | ( | std::string const & | q, |
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 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() |
size_t reg::dfa::deltaHat_ | ( | size_t | qi, |
std::u32string const & | u32word | ||
) | const |
Computes this DFA's transition function recursively for a UTF-32-encoded 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_()) |
state_not_found | if qi ≥ getStates().size() |
symbol_not_found | if one of the u32word symbols ∉ getAlphabet_() |
string const & reg::dfa::deltaHat_ | ( | std::string const & | q, |
std::u32string const & | u32word | ||
) | const |
Computes this DFA's transition function recursively for a UTF-32-encoded 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 of the u32word symbols ∉ getAlphabet_() |
Fetches this DFA's set of processable symbols as UTF-8-encoded strings.
vector< char32_t > const & reg::dfa::getAlphabet_ | ( | ) | const |
Fetches this DFA's set of processable symbols.
string const & reg::dfa::getInitialState | ( | ) | const |
Fetches this DFA's set of states.
|
static |
Builds the table of indistinguishable states w.r.t. a transition function.
transitions | the transition function to base indistinguishability computation off |
accepting | the set of states that's trivially distinguishable from the rest |
true
values mark indistinguishable states Creates a builder for a DFA accepting the intersection of the languages of two DFAs.
The input DFAs' state names will be concatenated and collisions resolved by appending _
in the created DFA.
d1 | the first DFA |
d2 | the other DFA |
bool reg::dfa::isAccepting | ( | size_t | qi | ) | const |
Tests whether a state is an accept state within this DFA.
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::dfa::isAccepting | ( | std::string const & | q | ) | const |
Tests whether a state is an accept state within this DFA.
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() |
reg::dfa::operator nfa const & | ( | ) | const |
Returns an NFA accepting the same language as this DFA.
bool reg::dfa::operator!= | ( | dfa const & | d | ) | const |
bool reg::dfa::operator!= | ( | nfa const & | n | ) | const |
Copy-assigns this DFA by copying another one's private implementation object.
Move-assigns this DFA by stealing another one's private implementation object.
The other DFA will be accepting the empty language ∅ afterwards.
bool reg::dfa::operator== | ( | dfa const & | d | ) | const |
Tests whether this DFA accepts exactly the same language as another one.
More specifically, checks whether this DFA's initial state is indistinguishable from the other one's.
bool reg::dfa::operator== | ( | nfa const & | n | ) | const |
Creates a builder for a DFA accepting the set difference of the languages of two DFAs.
The input DFAs' state names will be concatenated and collisions resolved by appending _
in the created DFA.
d1 | the first DFA |
d2 | the other DFA |
Creates a builder for a DFA accepting the union of the languages of two DFAs.
The input DFAs' state names will be concatenated and collisions resolved by appending _
in the created DFA.
d1 | the first DFA |
d2 | the other DFA |
|
friend |
Searches the shortest UTF-8-encoded word accepted by a given DFA.
This calls findShortestWord_() and converts the result to UTF-8. If you don't want that overhead and can handle UTF-32-encoded strings, use that function.
d | the DFA |
std::logic_error | if the DFA doesn't accept any words |
|
friend |
Searches the shortest UTF-32-encoded word accepted by a given DFA.
d | the DFA |
std::logic_error | if the DFA doesn't accept any words |