9 #include <unordered_set> 10 using std::unordered_set;
12 #include <unordered_map> 13 using std::unordered_map;
16 using std::reference_wrapper;
18 #include <forward_list> 19 using std::forward_list;
27 unordered_map<string,unordered_map<string,expression::exptr>>
transitions;
41 pImpl(unordered_map<
string,unordered_map<string,expression::exptr>>& transitionMap,
42 string const& initialState,
43 forward_list<reference_wrapper<string const>>& acceptingStates) :
transitions(move(transitionMap)) {
48 for (
string const& a : acceptingStates) {
62 auto reIt = from.find(pLabel);
63 if (reIt == from.end() || !reIt->second) {
100 state = string(1, prefix).append(std::to_string(0));
101 for (
size_t q(1);
transitions.count(state) != 0; q++) {
102 state = string(1, prefix).append(std::to_string(q));
105 for (state =
string(1, prefix).append(1, suffix);
107 state.append(1, suffix)
110 return transitions.insert(std::make_pair(state, unordered_map<string, expression::exptr>())).first->first;
126 unordered_map<string,unordered_map<string,expression::exptr>> transitions(n.
getNumberOfStates()+2);
127 forward_list<reference_wrapper<string const>> acceptingStates;
130 auto& entry = transitions[qLabel];
132 acceptingStates.push_front(qLabel);
135 auto& pSet = n.
delta(qi, symbol);
136 for (
size_t pi(0); pi < pSet.size(); pi++) {
148 p = unique_ptr<pImpl>(
new pImpl(transitions, n.
getLabelOf(0), acceptingStates));
159 p->transitions[p->initial][p->accepting] = r;
164 case expression::operation::alternation :
167 case expression::operation::concatenation :
170 case expression::operation::kleene :
173 case expression::operation::symbol :
176 case expression::operation::empty :
194 vector<reference_wrapper<string const>> active;
195 for (
auto const& entry : p->transitions) {
196 if (entry.first != p->initial && entry.first != p->accepting) {
197 active.push_back(entry.first);
205 return p->getTransition(qLabel, pLabel);
214 vector<pair<string const&, string const&>> splittable;
215 splittable.reserve(p->transitions.size()*p->transitions.size());
216 for (pair<
string const, unordered_map<string, expression::exptr>>
const& from : p->transitions) {
217 for (pair<string const, expression::exptr>
const& to : from.second) {
218 if (to.second && to.second->getOperation() > expression::operation::symbol) {
219 splittable.push_back(std::make_pair(std::ref(from.first), std::ref(to.first)));
223 splittable.shrink_to_fit();
237 vector<reference_wrapper<string const>> newStates;
238 auto const& re = p->getTransition(qLabel, pLabel);
239 switch (re->getOperation()) {
240 case expression::operation::alternation : {
241 auto sub1 = *(re->begin());
242 auto sub2 = *(re->begin()+1);
244 newStates.reserve(4);
245 for (
size_t i(0); i < 4; i++) {
246 newStates.push_back(p->generateState());
249 p->transitions[qLabel][newStates[0]] = epsilon;
250 p->transitions[newStates[0]][newStates[1]] = sub1;
251 p->transitions[newStates[1]][pLabel] = epsilon;
252 p->transitions[qLabel][newStates[2]] = epsilon;
253 p->transitions[newStates[2]][newStates[3]] = sub2;
254 p->transitions[newStates[3]][pLabel] = epsilon;
257 case expression::operation::concatenation : {
258 auto sub1 = *(re->begin());
259 auto sub2 = *(re->begin()+1);
261 newStates.reserve(2);
262 for (
size_t i(0); i < 2; i++) {
263 newStates.push_back(p->generateState());
265 p->transitions[qLabel][newStates[0]] = sub1;
267 p->transitions[newStates[1]][pLabel] = sub2;
270 case expression::operation::kleene : {
271 auto sub = *(re->begin());
273 p->transitions[qLabel][pLabel] = epsilon;
274 newStates.reserve(2);
275 for (
size_t i(0); i < 2; i++) {
276 newStates.push_back(p->generateState());
278 p->transitions[qLabel][newStates[0]] = epsilon;
279 p->transitions[newStates[1]][pLabel] = epsilon;
280 p->transitions[newStates[1]][newStates[0]] = epsilon;
281 p->transitions[newStates[0]][newStates[1]] = sub;
284 case expression::operation::symbol :
285 case expression::operation::empty :
302 for (
auto const& nodes : splittable) {
308 for (
auto const& from : p->transitions) {
309 for (
auto const& to : from.second) {
310 if (to.second != empty) {
311 b.
addTransition(from.first, to.first, to.second->extractSymbol());
322 auto const& re = p->getTransition(qLabel, pLabel);
326 auto const& selfRe = p->getTransition(qLabel, qLabel);
327 for (
auto const& from : p->transitions) {
328 if (from.first != qLabel) {
329 auto to = from.second.find(qLabel);
330 if (to != from.second.end() && to->second) {
345 p->transitions[qLabel].erase(pLabel);
350 forward_list<reference_wrapper<string const>> right;
351 for (
auto const& to : p->transitions[qLabel]) {
352 right.push_front(to.first);
354 for (
auto const& to : right) {
355 if (to.get() != qLabel) {
359 for (
auto& from : p->transitions) {
360 from.second.erase(qLabel);
362 p->transitions.erase(qLabel);
387 p.reset(
new pImpl(*(n.p)));
402 gnfa::~gnfa() =
default;
pImpl()
Constructs private implementation object for a GNFA accepting the empty language ∅.
void addTransition(string const &qLabel, string const &pLabel, expression::exptr re, bool optimized=true, bool aggressive=false)
Safely adds a transition RE between two states.
void ripState(std::string const &qLabel)
Removes a state, bypassing all its outgoing transitions.
std::vector< std::reference_wrapper< std::string const > > getActiveStates() const
Reveals the names of this GNFA's non-initial, non-accepting states.
nfa build()
Builds the NFA, as defined by previous operations.
expression::exptr getTransition(std::string const &qLabel, std::string const &pLabel) const
Extracts the RE labelling the transition between two states.
Represents nondeterministic finite automata with ε-moves.
unordered_map< string, unordered_map< string, expression::exptr > > transitions
Holds the transition table viz state × state → regular expression.
std::vector< char32_t > const & getAlphabet() const
Fetches this NFA's set of processable symbols.
std::string const & getLabelOf(size_t q) const
Puts a name to an index.
std::vector< exptr >::const_iterator begin() const
Returns an iterator pointing to this RE's first subexpression.
static exptr const & spawnEmptyString()
Gives an RE representing the empty string ε.
Constructs NFAs step by step.
gnfa(nfa const &n)
Constructs a GNFA with the same states and transitions as a given NFA.
nfa splitAllTransitions()
Splits all transitions until only ∅, ε, and symbol REs remain and builds the resulting NFA...
expression::exptr ripAllStates()
Removes all active states, constructing an RE semantically equivalent to this GNFA.
std::string const & getAcceptingState() const
Reveals the name of this GNFA's accept state.
static exptr spawnAlternation(exptr const &l, exptr const &r, bool optimized=true, bool aggressive=false)
Gives an RE representing the alternation of two given REs.
bool isAccepting(size_t q) const
Tests whether a state is an accept state within this NFA.
Represents formal regular expressions.
char32_t extractSymbol() const
Reports this symbol expression's UTF-32-encoded symbol.
std::string const & getInitialState() const
Reveals the name of this GNFA's initial state.
gnfa & operator=(gnfa const &n)
Copy-assigns this GNFA by copying another one's private implementation object.
static exptr const & spawnSymbol(char32_t symbol)
Gives an RE representing the given UTF-32-encoded symbol.
std::vector< std::pair< std::string const &, std::string const & > > getSplittableTransitions() const
Reveals this GNFA's splittable transitions.
string initial
Holds the name of the initial state.
builder & setAccepting(std::string const &state, bool accept)
Sets whether or not a state will be accepting within the prospective NFA.
expression::exptr const & getTransition(string const &qLabel, string const &pLabel) const
Safely fetches a transition RE between two states.
Contains the reg::nfa class definition.
operation getOperation() const
Reports this RE's function.
Represents generalized nondeterministic finite automata.
string const & generateState(char prefix='q', char suffix='\0')
Generates a unique new state name with given prefix (and suffix).
builder & makeInitial(std::string const &state)
Resets the initial state for the prospective NFA.
std::vector< std::reference_wrapper< std::string const > > splitTransition(std::string const &qLabel, std::string const &pLabel)
Splits a transition between two states, adding new states if needed.
size_t getNumberOfStates() const
Counts this NFA's states.
void bypassTransition(std::string const &qLabel, std::string const &pLabel)
Removes a transition between two states and replaces it with equivalent ones, bypassing its beginning...
pImpl(unordered_map< string, unordered_map< string, expression::exptr >> &transitionMap, string const &initialState, forward_list< reference_wrapper< string const >> &acceptingStates)
Constructs private implementation object with provided members.
Contains the reg::gnfa class definition.
builder & addTransition(std::string const &from, std::string const &to, char32_t symbol)
Adds a transition for the prospective NFA.
std::valarray< bool > const & delta(size_t q, char32_t symbol) const
Computes this NFA's transition function for a state and a symbol.
std::shared_ptr< expression const > exptr
This is the type used to handle regular expressions.
string accepting
Holds the name of the accept state.
static exptr const & spawnEmptySet()
Gives an RE representing the empty set ∅.
static exptr spawnKleene(exptr const &b, bool optimized=true, bool aggressive=false)
Gives an RE representing the Kleene closure of a given RE.
static exptr spawnConcatenation(exptr const &l, exptr const &r, bool optimized=true, bool aggressive=false)
Gives an RE representing the concatenation of two given REs.
Private implementation details of GNFAs.