reglibcpp  1.0.0
(Naïve) C++ implementation of models for regular languages
Classes | Public Member Functions | List of all members
reg::nfa::builder Class Reference

Constructs NFAs step by step. More...

#include <nfa.h>

Classes

struct  pImpl
 Private implementation details of NFA builders. More...
 

Public Member Functions

 builder ()
 Constructs a blank builder object. More...
 
 builder (nfa const &nfa)
 Constructs a builder object with exactly the same states, symbols, transitions, initial state and accept states as a given NFA. More...
 
 builder (dfa const &dfa)
 Constructs a builder object with exactly the same states, symbols, transitions, initial state and accept states as a given DFA. More...
 
 builder (builder &b)
 Copy-constructs a builder by copying another one's private implementation object. More...
 
 builder (builder &&b)
 Move-constructs a builder by stealing another one's private implementation object. More...
 
builderoperator= (builder const &b)
 Copy-assigns a builder by copying another one's private implementation object. More...
 
builderoperator= (builder &&b)
 Move-assigns a builder by stealing another one's private implementation object. More...
 
builderaddSymbol (char32_t symbol)
 Adds a symbol to the prospective NFA's alphabet. More...
 
builderaddSymbol (std::string const &utf8Symbol)
 Same as above for a UTF-8-encoded symbol. More...
 
buildersetAccepting (std::string const &state, bool accept)
 Sets whether or not a state will be accepting within the prospective NFA. More...
 
buildermakeInitial (std::string const &state)
 Resets the initial state for the prospective NFA. More...
 
builderaddTransition (std::string const &from, std::string const &to, char32_t symbol)
 Adds a transition for the prospective NFA. More...
 
builderaddTransition (std::string const &from, std::string const &to, std::string const &utf8Symbol)
 Same as above for a UTF-8-encoded symbol. More...
 
nfa build ()
 Builds the NFA, as defined by previous operations. More...
 

Detailed Description

Constructs NFAs step by step.

Any mention of a symbol or state will add them to the alphabet/set of states. The first state mentioned will be designated initial state.

Definition at line 52 of file nfa.h.

Constructor & Destructor Documentation

◆ builder() [1/5]

reg::nfa::builder::builder ( )

Constructs a blank builder object.

Definition at line 353 of file nfa.cpp.

353 : p(new pImpl) {}

◆ builder() [2/5]

reg::nfa::builder::builder ( nfa const &  nfa)

Constructs a builder object with exactly the same states, symbols, transitions, initial state and accept states as a given NFA.

Definition at line 356 of file nfa.cpp.

356  : p(new pImpl) {
357  makeInitial(nfa.getLabelOf(0));
358  for (char32_t symbol : nfa.getAlphabet()) {
359  addSymbol(symbol);
360  }
361  for (size_t qi = 0; qi < nfa.p->labels.size(); ++qi) {
362  string const& qLabel = nfa.getLabelOf(qi);
363  for (char32_t symbol : p->alphabet) {
364  valarray<bool> ps = nfa.delta(qi, symbol);
365  size_t pi(0);
366  for (bool pb : ps) {
367  if (pb) {
368  addTransition(qLabel, nfa.getLabelOf(pi), symbol);
369  }
370  pi++;
371  }
372  }
373  if (nfa.isAccepting(qi)) {
374  setAccepting(qLabel, true);
375  }
376  }
377 }
builder & addSymbol(char32_t symbol)
Adds a symbol to the prospective NFA&#39;s alphabet.
Definition: nfa.cpp:425
builder & setAccepting(std::string const &state, bool accept)
Sets whether or not a state will be accepting within the prospective NFA.
Definition: nfa.cpp:442
builder & makeInitial(std::string const &state)
Resets the initial state for the prospective NFA.
Definition: nfa.cpp:461
nfa()
Constructs an NFA accepting the empty language ∅.
Definition: nfa.cpp:82
builder & addTransition(std::string const &from, std::string const &to, char32_t symbol)
Adds a transition for the prospective NFA.
Definition: nfa.cpp:475

◆ builder() [3/5]

reg::nfa::builder::builder ( dfa const &  dfa)

Constructs a builder object with exactly the same states, symbols, transitions, initial state and accept states as a given DFA.

Transition destinations will be converted to singleton sets of the respective reached states.

Definition at line 381 of file nfa.cpp.

381  {
382  string initial(dfa.getLabelOf(0));
383  unordered_set<string> acceptingStates;
384  unordered_set<char32_t> alphabet(dfa.getAlphabet().begin(), dfa.getAlphabet().end());
385  unordered_map<string,unordered_map<char32_t,unordered_set<string>>> transitions;
386  for (size_t q(0); q < dfa.getNumberOfStates(); q++) {
387  for (char32_t symbol : alphabet) {
388  transitions[dfa.getLabelOf(q)][symbol].insert(dfa.getLabelOf(dfa.delta(q, symbol)));
389  }
390  }
391  p = unique_ptr<pImpl>(new pImpl(initial, acceptingStates, alphabet, transitions));
392 }

◆ builder() [4/5]

reg::nfa::builder::builder ( builder b)

Copy-constructs a builder by copying another one's private implementation object.

Definition at line 395 of file nfa.cpp.

395 : p(new pImpl(*(b.p))) {}

◆ builder() [5/5]

reg::nfa::builder::builder ( builder &&  b)

Move-constructs a builder by stealing another one's private implementation object.

The other builder will be blank afterwards.

Definition at line 399 of file nfa.cpp.

399 : p(new pImpl) {p.swap(b.p);}

Member Function Documentation

◆ addSymbol() [1/2]

nfa::builder & reg::nfa::builder::addSymbol ( char32_t  symbol)

Adds a symbol to the prospective NFA's alphabet.

Parameters
symbolthe symbol to add
Returns
reference to this object, for chaining operations

Definition at line 425 of file nfa.cpp.

425  {
426  p->alphabet.insert(symbol);
427  return *this;
428 }

◆ addSymbol() [2/2]

nfa::builder & reg::nfa::builder::addSymbol ( std::string const &  utf8Symbol)

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 431 of file nfa.cpp.

431  {
432  return addSymbol(expression::converter->from_bytes(utf8Symbol)[0]);
433 }
static std::unique_ptr< std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > > const converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: expression.h:97
builder & addSymbol(char32_t symbol)
Adds a symbol to the prospective NFA&#39;s alphabet.
Definition: nfa.cpp:425

◆ addTransition() [1/2]

nfa::builder & reg::nfa::builder::addTransition ( std::string const &  from,
std::string const &  to,
char32_t  symbol 
)

Adds a transition for the prospective NFA.

Parameters
fromthe starting state for the transition
tothe destination state for the transition
symbolthe symbol triggering the transition or \0 for an ε-transition
Returns
reference to this object, for chaining operations

Definition at line 475 of file nfa.cpp.

475  {
476  if (p->transitions.empty()) {
477  p->initial = from;
478  }
479  p->transitions[to];
480  p->transitions[from][symbol].insert(to);
481  addSymbol(symbol);
482  return *this;
483 }
builder & addSymbol(char32_t symbol)
Adds a symbol to the prospective NFA&#39;s alphabet.
Definition: nfa.cpp:425

◆ addTransition() [2/2]

nfa::builder & reg::nfa::builder::addTransition ( std::string const &  from,
std::string const &  to,
std::string const &  utf8Symbol 
)

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 486 of file nfa.cpp.

486  {
487  return addTransition(from, to, expression::converter->from_bytes(utf8Symbol)[0]);
488 }
static std::unique_ptr< std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > > const converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: expression.h:97
builder & addTransition(std::string const &from, std::string const &to, char32_t symbol)
Adds a transition for the prospective NFA.
Definition: nfa.cpp:475

◆ build()

nfa reg::nfa::builder::build ( )

Builds the NFA, as defined by previous operations.

Returns
an NFA object with exactly the states, alphabet and transitions that were defined

Definition at line 495 of file nfa.cpp.

495  {
496  p->alphabet.insert(U'\0');
497  vector<char32_t> alphabetV(p->alphabet.begin(), p->alphabet.end());
498  sort(alphabetV.begin(), alphabetV.end());
499  vector<string> labelV(p->transitions.size());
500  labelV[0] = p->initial;
501  valarray<bool> acceptingV(p->transitions.size());
502  acceptingV[0] = (p->acceptingStates.find(p->initial) != p->acceptingStates.end());
503  size_t i(1);
504  for (auto entry : p->transitions) {
505  if (entry.first == p->initial) {
506  continue;
507  }
508  labelV[i] = entry.first;
509  acceptingV[i++] = (
510  p->acceptingStates.find(entry.first) != p->acceptingStates.end()
511  );
512  }
513  vector<vector<valarray<bool>>> transitionV(
514  p->transitions.size(),
515  vector<valarray<bool>>(
516  p->alphabet.size()+1,
517  valarray<bool>(labelV.size())
518  )
519  );
520  unordered_set<string> emptySet;
521  size_t qi(0);
522  for (auto const& q : labelV) {
523  unordered_map<char32_t,unordered_set<string>> const& fromQ = p->transitions[q];
524  size_t si(0);
525  for (char32_t symbol : alphabetV) {
526  auto to = fromQ.find(symbol);
527  unordered_set<string> const& viaSymbol(to != fromQ.end() ? to->second : emptySet);
528  i = 0;
529  for (auto const& p : labelV) {
530  transitionV[qi][si][i++] = (viaSymbol.count(p) != 0);
531  }
532  si++;
533  }
534  qi++;
535  }
536  return {alphabetV, transitionV, labelV, acceptingV};
537 }

◆ makeInitial()

nfa::builder & reg::nfa::builder::makeInitial ( std::string const &  state)

Resets the initial state for the prospective NFA.

Parameters
statethe new initial state
Returns
reference to this object, for chaining operations

Definition at line 461 of file nfa.cpp.

461  {
462  p->initial = state;
463  p->transitions[state];
464  return *this;
465 }

◆ operator=() [1/2]

nfa::builder & reg::nfa::builder::operator= ( builder const &  b)

Copy-assigns a builder by copying another one's private implementation object.

Definition at line 402 of file nfa.cpp.

402  {
403  if (this != &b) {
404  p.reset(new pImpl(*(b.p)));
405  }
406  return *this;
407 }

◆ operator=() [2/2]

nfa::builder & reg::nfa::builder::operator= ( nfa::builder &&  b)

Move-assigns a builder by stealing another one's private implementation object.

The other builder will be blank afterwards.

Definition at line 411 of file nfa.cpp.

411  {
412  if (this != &b) {
413  p.reset(new pImpl);
414  p.swap(b.p);
415  }
416  return *this;
417 }

◆ setAccepting()

nfa::builder & reg::nfa::builder::setAccepting ( std::string const &  state,
bool  accept 
)

Sets whether or not a state will be accepting within the prospective NFA.

Parameters
statethe state's name
accepttrue if the state should be an accept state afterwards, false else
Returns
reference to this object, for chaining operations

Definition at line 442 of file nfa.cpp.

442  {
443  if (p->transitions.empty()) {
444  p->initial = state;
445  }
446  p->transitions[state];
447  if (accept) {
448  p->acceptingStates.insert(state);
449  } else {
450  p->acceptingStates.erase(state);
451  }
452  return *this;
453 }

The documentation for this class was generated from the following files: