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

Represents the table entries as binary trees. More...

Public Member Functions

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. More...
 
 tree (size_t row, size_t diag, parser const *p)
 Initializes a tree with a given table entry as root. More...
 
exptr operator() (bool optimized, bool aggressive)
 Gives the RE encoded in this tree. More...
 

Public Attributes

parser const * p
 Points to the parser this tree belongs to. More...
 
token symbol
 This tree's root symbol. More...
 
pair< unique_ptr< tree >, unique_ptr< tree > > children
 Trees with the symbols of the entry's derived pair as root. More...
 

Detailed Description

Represents the table entries as binary trees.

Definition at line 485 of file expression.cpp.

Constructor & Destructor Documentation

◆ tree()

reg::expression::parser::tree::tree ( size_t  row,
size_t  diag,
parser const *  p 
)
inline

Initializes a tree with a given table entry as root.

Parameters
rowthe row of the entry
diagthe diagonal of the entry
ppointer to the calling parser

Definition at line 520 of file expression.cpp.

520  :
521  p(p),
522  symbol([&]()->token{
523  for (unsigned char i(token::END); i > 0; i--) {
524  if (p->table[row][diag][i-1]) {
525  return static_cast<token>(i-1);
526  }
527  }
528  return token::END;
529  }()),
530  children(findNextPair(symbol, row, diag, p)) {}
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.
Definition: expression.cpp:501
token symbol
This tree&#39;s root symbol.
Definition: expression.cpp:487
vector< vector< tokens > > table
The table of sets of symbols that derive a subsentence.
Definition: expression.cpp:447
pair< unique_ptr< tree >, unique_ptr< tree > > children
Trees with the symbols of the entry&#39;s derived pair as root.
Definition: expression.cpp:488
token
Tokens the grammar deals with.
Definition: expression.cpp:340
parser const * p
Points to the parser this tree belongs to.
Definition: expression.cpp:486

Member Function Documentation

◆ findNextPair()

pair<unique_ptr<tree>,unique_ptr<tree> > reg::expression::parser::tree::findNextPair ( token  symbol,
size_t  row,
size_t  diag,
parser const *  p 
)
inline

Finds the child trees that can be derived from a given entry.

Parameters
symbolthe entry's symbol
rowthe row of the entry
diagthe diagonal of the entry
ppoints to this tree's owning parser
Returns
pair of trees, both of which can be derived from the entry (may both be
null
if the entry is nullable)

Definition at line 501 of file expression.cpp.

501  {
502  for (size_t i = 0; i < diag; i++) {
503  size_t leftDiag = diag-i-1;
504  size_t rightDiag = i;
505  size_t rightRow = diag+row-rightDiag;
506  if (canDerive(symbol, p->table[row][leftDiag], p->table[rightRow][rightDiag])) {
507  return make_pair(unique_ptr<tree>(new tree(row, leftDiag, p)), unique_ptr<tree>(new tree(rightRow, rightDiag, p)));
508  }
509  }
510  return make_pair(unique_ptr<tree>(), unique_ptr<tree>());
511  }
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.
Definition: expression.cpp:394
token symbol
This tree&#39;s root symbol.
Definition: expression.cpp:487
vector< vector< tokens > > table
The table of sets of symbols that derive a subsentence.
Definition: expression.cpp:447
parser const * p
Points to the parser this tree belongs to.
Definition: expression.cpp:486
tree(size_t row, size_t diag, parser const *p)
Initializes a tree with a given table entry as root.
Definition: expression.cpp:520

◆ operator()()

exptr reg::expression::parser::tree::operator() ( bool  optimized,
bool  aggressive 
)
inline

Gives the RE encoded in this tree.

Definition at line 533 of file expression.cpp.

533  {
534  switch (symbol) {
535  case token::A:
536  return spawnAlternation((*children.first)(optimized, aggressive), (*children.second)(optimized, aggressive), optimized, aggressive);
537  case token::B:
538  return (*children.second)(optimized, aggressive);
539  case token::C:
540  return spawnConcatenation((*children.first)(optimized, aggressive), (*children.second)(optimized, aggressive), optimized, aggressive);
541  case token::K:
542  return spawnKleene((*children.first)(optimized, aggressive), optimized, aggressive);
543  case token::E:
544  return (*children.second)(optimized, aggressive);
545  case token::F:
546  return (*children.first)(optimized, aggressive);
547  case token::Σ: {
548  char32_t symbol = p->symbolMapping[p->symbolMappingIndex++];
549  if (p->symbolMappingIndex >= p->symbolMapping.size()) { p->symbolMappingIndex = 0; }
550  if (symbol == p->lits.EPSILON) { return spawnEmptyString(); }
551  else if (symbol == p->lits.EMPTY) { return spawnEmptySet(); }
552  else { return spawnSymbol(symbol); }
553  }
554  default:
555  return exptr();
556  }
557  }
token symbol
This tree&#39;s root symbol.
Definition: expression.cpp:487
char32_t const EMPTY
Neutral element of alternation and annihilating element of concatenation, a.k.a. empty set...
Definition: expression.h:53
static exptr const & spawnEmptyString()
Gives an RE representing the empty string ε.
Definition: expression.cpp:50
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
literals lits
Stores interpretations for characters encountered in the parsed string.
Definition: expression.cpp:444
static exptr const & spawnSymbol(char32_t symbol)
Gives an RE representing the given UTF-32-encoded symbol.
Definition: expression.cpp:61
pair< unique_ptr< tree >, unique_ptr< tree > > children
Trees with the symbols of the entry&#39;s derived pair as root.
Definition: expression.cpp:488
vector< char32_t > symbolMapping
Stores the actual symbols encountered in the RE while parsing.
Definition: expression.cpp:445
size_t symbolMappingIndex
Index for when symbols have to be extracted from the mapping.
Definition: expression.cpp:446
parser const * p
Points to the parser this tree belongs to.
Definition: expression.cpp:486
char32_t const EPSILON
Neutral element of concatenation, a.k.a. empty string.
Definition: expression.h:53
std::shared_ptr< expression const > exptr
This is the type used to handle regular expressions.
Definition: expression.h:43
static exptr const & spawnEmptySet()
Gives an RE representing the empty set ∅.
Definition: expression.cpp:40
static exptr spawnKleene(exptr const &b, bool optimized=true, bool aggressive=false)
Gives an RE representing the Kleene closure of a given RE.
Definition: expression.cpp:152
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.
Definition: expression.cpp:81

Member Data Documentation

◆ children

pair<unique_ptr<tree>,unique_ptr<tree> > reg::expression::parser::tree::children

Trees with the symbols of the entry's derived pair as root.

Definition at line 488 of file expression.cpp.

◆ p

parser const* reg::expression::parser::tree::p

Points to the parser this tree belongs to.

Definition at line 486 of file expression.cpp.

◆ symbol

token reg::expression::parser::tree::symbol

This tree's root symbol.

Definition at line 487 of file expression.cpp.


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