9 using std::make_unique;
51 return expression::empty;
70 return expression::symbols.insert(make_pair(symbol,
exptr(
new expression(symbol)))).first->second;
75 char32_t u32Symbol(
converter.from_bytes(utf8Symbol)[0]);
76 return expression::symbols.insert(make_pair(u32Symbol,
exptr(
new expression(u32Symbol)))).first->second;
142 return l->size() > r->size() ? r : l;
194 for (
exptr const& re : *
this) {
209 #ifndef DOXYGEN_SHOULD_SKIP_THIS 210 expression::operator
nfa const&()
const {
214 return *acceptingNfa;
216 #endif // DOXYGEN_SHOULD_SKIP_THIS 223 return other.operator==(*this);
236 return *
this ==
static_cast<nfa const&
>(other);
241 return *
this !=
static_cast<nfa const&
>(other);
250 auto it = std::find_if(
251 expression::symbols.
begin(),
252 expression::symbols.
end(),
255 if (it == expression::symbols.
end()) {
256 throw std::logic_error(
"This RE does not seem to be a valid symbol expression.");
268 if (symbol == U
'\0') {
278 case operation::alternation :
return subExpressions[0]->to_u32string() +
A + subExpressions[1]->to_u32string();
279 case operation::concatenation : {
281 if (subExpressions[0]->op >= operation::alternation) {
286 if (subExpressions[1]->op >= operation::alternation) {
293 case operation::kleene : {
294 if (subExpressions[0]->op >= operation::concatenation) {
297 return subExpressions[0]->to_u32string() +
K;
300 case operation::symbol : {
304 case operation::empty :
return u32string(1,
N);
316 return subExpressions.cbegin();
321 return subExpressions.cend();
368 #define TOKEN(T) static_cast<unsigned char>(reg::expression::parser::token::T) 390 for (
unsigned char c(0); c <
TOKEN(END); c++) {
393 closure.set(static_cast<unsigned char>(s));
410 for (
unsigned char li(0); li <
TOKEN(END); li++) {
411 if (leftClosure[li]) {
412 for (
unsigned char ri(0); ri <
TOKEN(END); ri++) {
413 if (rightClosure[ri]) {
414 for (
unsigned char ci(0); ci <
TOKEN(END); ci++) {
416 for (
unsigned char successor(ci); successor !=
TOKEN(END); successor =
static_cast<unsigned char>(
inverseUnitGraph[successor])) {
417 if (static_cast<unsigned char>(symbol) == successor) {
440 for (
size_t i(0); i < diag; i++) {
442 for (
unsigned char fi(0); fi < first.size(); fi++) {
445 for (
unsigned char si(0); si < second.size(); si++) {
467 size_t numberOfTokens(re.length());
469 table.reserve(numberOfTokens);
471 for (char32_t symbol : re) {
473 if (symbol == L) {
table[row][0].set(
TOKEN(L)); }
474 else if (symbol == R) {
table[row][0].set(
TOKEN(R)); }
475 else if (symbol == A) {
table[row][0].set(
TOKEN(P)); }
476 else if (symbol == K) {
table[row][0].set(
TOKEN(S)); }
484 for (
size_t diag(1); diag < numberOfTokens; diag++) {
485 for (
size_t row(0); row < numberOfTokens - diag; row++) {
511 for (
size_t i = 0; i < diag; i++) {
512 size_t leftDiag = diag-i-1;
513 size_t rightDiag = i;
514 size_t rightRow = diag+row-rightDiag;
516 return make_pair(make_unique<tree>(row, leftDiag,
p), make_unique<tree>(rightRow, rightDiag,
p));
531 for (
unsigned char i(
TOKEN(END)); i > 0; i--) {
532 if (
p->
table[row][diag][i-1]) {
533 return static_cast<token>(i-1);
546 return (*
children.second)(optimized, aggressive);
552 return (*
children.second)(optimized, aggressive);
554 return (*
children.first)(optimized, aggressive);
577 return tree(0,
table.size()-1,
this)(optimized, aggressive);
584 graph.fill(token::END);
585 graph[
TOKEN(Σ)] = token::E;
586 graph[
TOKEN(E)] = token::K;
587 graph[
TOKEN(K)] = token::C;
588 graph[
TOKEN(C)] = token::A;
595 noPredecessor.fill(tokens());
597 rules.fill(noPredecessor);
625 result = stringParser(optimized, aggressive);
660 return stringParser(optimized, aggressive);
671 expression::expression() : op(operation::empty) {}
672 expression::expression(char32_t symbol) : op(operation::symbol) {}
673 expression::expression(exptr
const& l, exptr
const& r, operation op) : subExpressions({l, r}), op(op) {}
674 expression::expression(exptr
const& b) : subExpressions({b}), op(operation::kleene) {}
Represents the table entries as binary trees.
static array< array< tokens, TOKEN(END)>, TOKEN(END)> const inverseBinaryRules
Maps pairs of symbols to the symbols that derive them.
char32_t const S
The Kleene star.
static bool canDerive(token symbol, tokens const &left, tokens const &right)
Checks if a token could derive a pair of tokens from two other entries.
Represents nondeterministic finite automata with ε-moves.
static tokens getUClosure(tokens const &m)
Constructs the reflexive-transitive closure of the inverse unit relation for a given set of symbols...
pair< unique_ptr< tree >, unique_ptr< tree > > findNextPair(token symbol, size_t row, size_t diag, parser const *p)
Finds the child trees that can be derived from a given entry.
token symbol
This tree's root symbol.
static char32_t N
The symbol used to represent the Null/empty set in a regular expression.
Represents deterministic finite automata.
char32_t const EMPTY
Neutral element of alternation and annihilating element of concatenation, a.k.a. empty set...
bool operator==(nfa const &other) const
Checks whether this RE describes the same regular language as another object.
std::vector< exptr >::const_iterator begin() const
Returns an iterator pointing to this RE's first subexpression.
Beginning of a new subexpression.
static exptr const & spawnEmptyString()
Gives an RE representing the empty string ε.
nfa splitAllTransitions()
Splits all transitions until only ∅, ε, and symbol REs remain and builds the resulting NFA...
#define TOKEN(T)
Gives casting to base type back to scoped enums, as God intended.
bool operator!=(nfa const &other) const
Checks whether this RE describes a different regular language than another object.
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
vector< vector< tokens > > table
The table of sets of symbols that derive a subsentence.
exptr operator()(bool optimized, bool aggressive)
Gives the RE encoded in this tree.
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.
Second part of an alternation expression.
Represents formal regular expressions.
std::u32string to_u32string() const
Describes this RE in UTF-32-encoded human-readable form.
bool badRegularExpression
Signifies that the RE used to initialize this object was invalid.
char32_t extractSymbol() const
Reports this symbol expression's UTF-32-encoded symbol.
static exptr const & spawnSymbol(char32_t symbol)
Gives an RE representing the given UTF-32-encoded symbol.
pair< unique_ptr< tree >, unique_ptr< tree > > children
Trees with the symbols of the entry's derived pair as root.
vector< char32_t > symbolMapping
Stores the actual symbols encountered in the RE while parsing.
operation
The different purposes an RE may fulfill.
Parses regular expressions.
Contains the reg::nfa class definition.
token
Tokens the grammar deals with.
operation getOperation() const
Reports this RE's function.
Contains the reg::expression class defintion.
std::vector< exptr >::const_iterator end() const
Returns an iterator pointing behind this RE's last subexpression.
bitset< TOKEN(END)> tokens
Tokens don't usually come alone.
static char32_t K
The symbol used to represent the Kleene star in a regular expression.
Represents generalized nondeterministic finite automata.
std::string to_string() const
Describes this RE in UTF-8-encoded human-readable form.
size_t symbolMappingIndex
Index for when symbols have to be extracted from the mapping.
char32_t const L
The left parenthesis.
parser const * p
Points to the parser this tree belongs to.
static nfa::builder subtract(nfa const &n1, nfa const &n2)
Creates a builder for an NFA accepting the set difference of the languages of two NFAs...
Contains the reg::gnfa class definition.
parser(u32string const &re)
Initializes with a string to parse.
Number of elements in this enumeration, NOT AN ACTUAL TOKEN!
char32_t const P
The alternation symbol.
size_t size() const
Reports the size of this RE's tree representation.
static exptr spawnFromString(std::u32string const &re, literals lits, bool optimized=false, bool aggressive=false)
static char32_t L
The symbol used to represent the Left parenthesis in a regular expression.
Where this library lives.
static void compileTableEntry(size_t row, size_t diag, vector< vector< tokens >> &table)
Fills a table entry.
char32_t const EPSILON
Neutral element of concatenation, a.k.a. empty string.
Second part of a new subexpression.
static void reset()
Resets the symbols used for RE operators to their defaults.
static char32_t E
The symbol used to represent the Empty string in a regular expression.
exptr operator()(bool optimized, bool aggressive)
Gives the RE resulting from parsing.
tree(size_t row, size_t diag, parser const *p)
Initializes a tree with a given table entry as root.
Beginning of an alternation expression.
std::shared_ptr< expression const > exptr
This is the type used to handle regular expressions.
static exptr const & spawnEmptySet()
Gives an RE representing the empty set ∅.
char32_t const R
The right parenthesis.
Contains the reg::dfa class definition.
std::string extractUtf8Symbol() const
Reports this symbol expression's UTF-8-encoded symbol.
static array< token, TOKEN(END)> const inverseUnitGraph
Maps symbols that may be derived in-place to their predecessing symbols.
static char32_t R
The symbol used to represent the Right parenthesis in a regular expression.
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.
static char32_t A
The symbol used to represent the Alternation in a regular expression.
A concatenation expression.