reglibcpp  1.3.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 463 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 496 of file expression.cpp.

496  :
497  p(p),
498  symbol([&]()->token{
499  for (unsigned char i(token::END); i > 0; i--) {
500  if (p->table[row][diag][i-1]) {
501  return static_cast<token>(i-1);
502  }
503  }
504  return token::END;
505  }()),
506  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:478
token symbol
This tree&#39;s root symbol.
Definition: expression.cpp:465
vector< vector< tokens > > table
The table of sets of symbols that derive a subsentence.
Definition: expression.cpp:425
pair< unique_ptr< tree >, unique_ptr< tree > > children
Trees with the symbols of the entry's derived pair as root.
Definition: expression.cpp:466
token
Tokens the grammar deals with.
Definition: expression.cpp:321
parser const * p
Points to the parser this tree belongs to.
Definition: expression.cpp:464

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 478 of file expression.cpp.

478  {
479  for (size_t i = 0; i < diag; i++) {
480  size_t leftDiag = diag-i-1;
481  size_t rightDiag = i;
482  size_t rightRow = diag+row-rightDiag;
483  if (canDerive(symbol, p->table[row][leftDiag], p->table[rightRow][rightDiag])) {
484  return make_pair(make_unique<tree>(row, leftDiag, p), make_unique<tree>(rightRow, rightDiag, p));
485  }
486  }
487  return make_pair(unique_ptr<tree>(), unique_ptr<tree>());
488  }
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:373
token symbol
This tree&#39;s root symbol.
Definition: expression.cpp:465
vector< vector< tokens > > table
The table of sets of symbols that derive a subsentence.
Definition: expression.cpp:425
parser const * p
Points to the parser this tree belongs to.
Definition: expression.cpp:464

◆ operator()()

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

Gives the RE encoded in this tree.

Definition at line 509 of file expression.cpp.

509  {
510  switch (symbol) {
511  case token::A:
512  return spawnAlternation((*children.first)(optimized, aggressive), (*children.second)(optimized, aggressive), optimized, aggressive);
513  case token::B:
514  return (*children.second)(optimized, aggressive);
515  case token::C:
516  return spawnConcatenation((*children.first)(optimized, aggressive), (*children.second)(optimized, aggressive), optimized, aggressive);
517  case token::K:
518  return spawnKleene((*children.first)(optimized, aggressive), optimized, aggressive);
519  case token::E:
520  return (*children.second)(optimized, aggressive);
521  case token::F:
522  return (*children.first)(optimized, aggressive);
523  case token::Σ: {
524  char32_t symbol = p->symbolMapping[p->symbolMappingIndex++];
525  if (p->symbolMappingIndex >= p->symbolMapping.size()) { p->symbolMappingIndex = 0; }
526  if (symbol == p->lits.EPSILON) { return spawnEmptyString(); }
527  else if (symbol == p->lits.EMPTY) { return spawnEmptySet(); }
528  else { return spawnSymbol(symbol); }
529  }
530  default:
531  return exptr();
532  }
533  }
token symbol
This tree&#39;s root symbol.
Definition: expression.cpp:465
char32_t const EMPTY
Neutral element of alternation and annihilating element of concatenation, a.k.a. empty set...
Definition: expression.h:49
static exptr const & spawnEmptyString()
Gives an RE representing the empty string ε.
Definition: expression.cpp:42
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:106
literals lits
Stores interpretations for characters encountered in the parsed string.
Definition: expression.cpp:422
static exptr const & spawnSymbol(char32_t symbol)
Gives an RE representing the given UTF-32-encoded symbol.
Definition: expression.cpp:52
pair< unique_ptr< tree >, unique_ptr< tree > > children
Trees with the symbols of the entry's derived pair as root.
Definition: expression.cpp:466
vector< char32_t > symbolMapping
Stores the actual symbols encountered in the RE while parsing.
Definition: expression.cpp:423
size_t symbolMappingIndex
Index for when symbols have to be extracted from the mapping.
Definition: expression.cpp:424
parser const * p
Points to the parser this tree belongs to.
Definition: expression.cpp:464
char32_t const EPSILON
Neutral element of concatenation, a.k.a. empty string.
Definition: expression.h:49
std::shared_ptr< expression const > exptr
This is the type used to handle regular expressions.
Definition: expression.h:39
static exptr const & spawnEmptySet()
Gives an RE representing the empty set ∅.
Definition: expression.cpp:33
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:140
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:71

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 466 of file expression.cpp.

◆ p

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

Points to the parser this tree belongs to.

Definition at line 464 of file expression.cpp.

◆ symbol

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

This tree's root symbol.

Definition at line 465 of file expression.cpp.


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