reglibcpp  1.0.0
(Naïve) C++ implementation of models for regular languages
Public Member Functions | Public Attributes | List of all members
reg::gnfa::pImpl Struct Reference

Private implementation details of GNFAs. More...

Public Member Functions

 pImpl ()
 Constructs private implementation object for a GNFA accepting the empty language ∅. More...
 
 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. More...
 
expression::exptr const & getTransition (string const &qLabel, string const &pLabel) const
 Safely fetches a transition RE between two states. More...
 
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. More...
 
string const & generateState (char prefix='q', char suffix='\0')
 Generates a unique new state name with given prefix (and suffix). More...
 

Public Attributes

unordered_map< string, unordered_map< string, expression::exptr > > transitions
 Holds the transition table viz state × state → regular expression. More...
 
string initial
 Holds the name of the initial state. More...
 
string accepting
 Holds the name of the accept state. More...
 

Detailed Description

Private implementation details of GNFAs.

Definition at line 26 of file gnfa.cpp.

Constructor & Destructor Documentation

◆ pImpl() [1/2]

reg::gnfa::pImpl::pImpl ( )
inline

Constructs private implementation object for a GNFA accepting the empty language ∅.

Definition at line 35 of file gnfa.cpp.

35  : initial("p0"), accepting("pF") {
38  }
unordered_map< string, unordered_map< string, expression::exptr > > transitions
Holds the transition table viz state × state → regular expression.
Definition: gnfa.cpp:27
string initial
Holds the name of the initial state.
Definition: gnfa.cpp:29
string accepting
Holds the name of the accept state.
Definition: gnfa.cpp:31

◆ pImpl() [2/2]

reg::gnfa::pImpl::pImpl ( unordered_map< string, unordered_map< string, expression::exptr >> &  transitionMap,
string const &  initialState,
forward_list< reference_wrapper< string const >> &  acceptingStates 
)
inline

Constructs private implementation object with provided members.

Definition at line 41 of file gnfa.cpp.

43  : transitions(move(transitionMap)) {
44  initial = generateState('p', '0');
45  auto const& epsilon = expression::spawnEmptyString();
46  transitions[initial][initialState] = epsilon;
47  accepting = generateState('p', 'F');
48  for (string const& a : acceptingStates) {
49  transitions[a][accepting] = epsilon;
50  }
51  }
unordered_map< string, unordered_map< string, expression::exptr > > transitions
Holds the transition table viz state × state → regular expression.
Definition: gnfa.cpp:27
static exptr const & spawnEmptyString()
Gives an RE representing the empty string ε.
Definition: expression.cpp:50
string initial
Holds the name of the initial state.
Definition: gnfa.cpp:29
string const & generateState(char prefix='q', char suffix='\0')
Generates a unique new state name with given prefix (and suffix).
Definition: gnfa.cpp:97
string accepting
Holds the name of the accept state.
Definition: gnfa.cpp:31

Member Function Documentation

◆ addTransition()

void reg::gnfa::pImpl::addTransition ( string const &  qLabel,
string const &  pLabel,
expression::exptr  re,
bool  optimized = true,
bool  aggressive = false 
)
inline

Safely adds a transition RE between two states.

The resulting transition will be labelled with the alternation of the provided RE and the former RE labelling the transition.

Parameters
qLabelbeginning of the transition
pLabelend of the transition
rethe RE that should be added to the transition
optimizedpass-through to expression::spawnAlternation
aggressivepass-through to expression::spawnAlternation

Definition at line 81 of file gnfa.cpp.

81  {
82  transitions[qLabel][pLabel] = expression::spawnAlternation(getTransition(qLabel, pLabel), re, optimized, aggressive);
83  }
unordered_map< string, unordered_map< string, expression::exptr > > transitions
Holds the transition table viz state × state → regular expression.
Definition: gnfa.cpp:27
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.
Definition: expression.cpp:117
expression::exptr const & getTransition(string const &qLabel, string const &pLabel) const
Safely fetches a transition RE between two states.
Definition: gnfa.cpp:60

◆ generateState()

string const& reg::gnfa::pImpl::generateState ( char  prefix = 'q',
char  suffix = '\0' 
)
inline

Generates a unique new state name with given prefix (and suffix).

State name generation has two modes:

  1. If suffix == \0: The name begins with prefix and ends with an ever-increasing number.
  2. Else: The name begins with prefix and ends with an ever-expanding repetition of suffix.
    Parameters
    prefixprintable char the new state's name should begin with
    suffixprintable char the new state's name should end with or \0 for a number
    Returns
    the first state name generated by the method above that is not present in the table of transitions

Definition at line 97 of file gnfa.cpp.

97  {
98  string state;
99  if (suffix == '\0') {
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));
103  }
104  } else {
105  for (state = string(1, prefix).append(1, suffix);
106  transitions.count(state) != 0;
107  state.append(1, suffix)
108  ) {}
109  }
110  return transitions.insert(std::make_pair(state, unordered_map<string, expression::exptr>())).first->first;
111  }
unordered_map< string, unordered_map< string, expression::exptr > > transitions
Holds the transition table viz state × state → regular expression.
Definition: gnfa.cpp:27

◆ getTransition()

expression::exptr const& reg::gnfa::pImpl::getTransition ( string const &  qLabel,
string const &  pLabel 
) const
inline

Safely fetches a transition RE between two states.

Parameters
qLabelbegin state of the transition
pLabelend state of the transition
Returns
the RE corresponding to the transition between the states, if undefined

Definition at line 60 of file gnfa.cpp.

60  {
61  auto const& from = transitions.at(qLabel);
62  auto reIt = from.find(pLabel);
63  if (reIt == from.end() || !reIt->second) {
65  }
66  return reIt->second;
67  }
unordered_map< string, unordered_map< string, expression::exptr > > transitions
Holds the transition table viz state × state → regular expression.
Definition: gnfa.cpp:27
static exptr const & spawnEmptySet()
Gives an RE representing the empty set ∅.
Definition: expression.cpp:40

Member Data Documentation

◆ accepting

string reg::gnfa::pImpl::accepting

Holds the name of the accept state.

Definition at line 31 of file gnfa.cpp.

◆ initial

string reg::gnfa::pImpl::initial

Holds the name of the initial state.

Definition at line 29 of file gnfa.cpp.

◆ transitions

unordered_map<string,unordered_map<string,expression::exptr> > reg::gnfa::pImpl::transitions

Holds the transition table viz state × state → regular expression.

Definition at line 27 of file gnfa.cpp.


The documentation for this struct was generated from the following file: