7 #include <unordered_map> 8 using std::unordered_map;
11 using std::reference_wrapper;
13 #include <forward_list> 14 using std::forward_list;
22 unordered_map<string,unordered_map<string,expression::exptr>>
transitions;
36 pImpl(unordered_map<
string,unordered_map<string,expression::exptr>>& transitionMap,
37 string const& initialState,
38 forward_list<reference_wrapper<string const>>& acceptingStates) :
transitions(move(transitionMap)) {
43 for (
string const& a : acceptingStates) {
56 auto reIt = from.find(pLabel);
57 if (reIt == from.end() || !reIt->second) {
92 state = string(1, prefix).append(std::to_string(0));
93 for (
size_t q(1);
transitions.count(state) != 0; q++) {
94 state = string(1, prefix).append(std::to_string(q));
97 for (state =
string(1, prefix).append(1, suffix);
99 state.append(1, suffix)
102 return transitions.insert(std::make_pair(state, unordered_map<string, expression::exptr>())).first->first;
117 unordered_map<string,unordered_map<string,expression::exptr>> transitions(n.
getNumberOfStates()+2);
118 forward_list<reference_wrapper<string const>> acceptingStates;
121 auto& entry = transitions[qLabel];
123 acceptingStates.push_front(qLabel);
126 auto& pSet = n.
delta(qi, symbol);
127 for (
size_t pi(0); pi < pSet.size(); pi++) {
139 p = std::make_unique<pImpl>(transitions, n.
getLabelOf(0), acceptingStates);
149 p->transitions[p->initial][p->accepting] = r;
154 case expression::operation::alternation :
157 case expression::operation::concatenation :
160 case expression::operation::kleene :
163 case expression::operation::symbol :
166 case expression::operation::empty :
184 vector<reference_wrapper<string const>> active;
185 for (
auto const& entry : p->transitions) {
186 if (entry.first != p->initial && entry.first != p->accepting) {
187 active.push_back(std::cref(entry.first));
195 return p->getTransition(qLabel, pLabel);
203 vector<pair<reference_wrapper<string const>, reference_wrapper<string const>>> splittable;
204 splittable.reserve(p->transitions.size()*p->transitions.size());
205 for (pair<
string const, unordered_map<string, expression::exptr>>
const& from : p->transitions) {
206 for (pair<string const, expression::exptr>
const& to : from.second) {
207 if (to.second && to.second->getOperation() > expression::operation::symbol) {
208 splittable.push_back(std::make_pair(std::ref(from.first), std::ref(to.first)));
212 splittable.shrink_to_fit();
225 vector<reference_wrapper<string const>> newStates;
226 auto const& re = p->getTransition(qLabel, pLabel);
227 switch (re->getOperation()) {
228 case expression::operation::alternation : {
229 auto sub1 = *(re->begin());
230 auto sub2 = *(re->begin()+1);
232 newStates.reserve(4);
233 for (
size_t i(0); i < 4; i++) {
234 newStates.push_back(p->generateState());
237 p->transitions[qLabel][newStates[0]] = epsilon;
238 p->transitions[newStates[0]][newStates[1]] = sub1;
239 p->transitions[newStates[1]][pLabel] = epsilon;
240 p->transitions[qLabel][newStates[2]] = epsilon;
241 p->transitions[newStates[2]][newStates[3]] = sub2;
242 p->transitions[newStates[3]][pLabel] = epsilon;
245 case expression::operation::concatenation : {
246 auto sub1 = *(re->begin());
247 auto sub2 = *(re->begin()+1);
249 newStates.reserve(2);
250 for (
size_t i(0); i < 2; i++) {
251 newStates.push_back(p->generateState());
253 p->transitions[qLabel][newStates[0]] = sub1;
255 p->transitions[newStates[1]][pLabel] = sub2;
258 case expression::operation::kleene : {
259 auto sub = *(re->begin());
261 p->transitions[qLabel][pLabel] = epsilon;
262 newStates.reserve(2);
263 for (
size_t i(0); i < 2; i++) {
264 newStates.push_back(p->generateState());
266 p->transitions[qLabel][newStates[0]] = epsilon;
267 p->transitions[newStates[1]][pLabel] = epsilon;
268 p->transitions[newStates[1]][newStates[0]] = epsilon;
269 p->transitions[newStates[0]][newStates[1]] = sub;
272 case expression::operation::symbol :
273 case expression::operation::empty :
289 for (
auto const& nodes : splittable) {
295 for (
auto const& from : p->transitions) {
296 for (
auto const& to : from.second) {
297 if (to.second != empty) {
298 b.
addTransition(from.first, to.first, to.second->extractSymbol());
309 auto const& re = p->getTransition(qLabel, pLabel);
313 auto const& selfRe = p->getTransition(qLabel, qLabel);
314 for (
auto const& from : p->transitions) {
315 if (from.first != qLabel) {
316 auto to = from.second.find(qLabel);
317 if (to != from.second.end() && to->second) {
332 p->transitions[qLabel].erase(pLabel);
337 forward_list<reference_wrapper<string const>> right;
338 for (
auto const& to : p->transitions[qLabel]) {
339 right.push_front(to.first);
341 for (
auto const& to : right) {
342 if (to.get() != qLabel) {
346 for (
auto& from : p->transitions) {
347 from.second.erase(qLabel);
349 p->transitions.erase(qLabel);
373 p.reset(
new pImpl(*(n.p)));
388 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.
std::vector< std::pair< std::reference_wrapper< std::string const >, std::reference_wrapper< std::string const > > > getSplittableTransitions() const
Reveals this GNFA's splittable transitions.
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.
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.