reglibcpp  1.6.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 461 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 494 of file expression.cpp.

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

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

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

◆ operator()()

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

Gives the RE encoded in this tree.

Definition at line 507 of file expression.cpp.

507  {
508  switch (symbol) {
509  case token::A:
510  return spawnAlternation((*children.first)(optimized, aggressive), (*children.second)(optimized, aggressive), optimized, aggressive);
511  case token::B:
512  return (*children.second)(optimized, aggressive);
513  case token::C:
514  return spawnConcatenation((*children.first)(optimized, aggressive), (*children.second)(optimized, aggressive), optimized, aggressive);
515  case token::K:
516  return spawnKleene((*children.first)(optimized, aggressive), optimized, aggressive);
517  case token::E:
518  return (*children.second)(optimized, aggressive);
519  case token::F:
520  return (*children.first)(optimized, aggressive);
521  case token::Σ: {
522  char32_t symbol = p->symbolMapping[p->symbolMappingIndex++];
523  if (p->symbolMappingIndex >= p->symbolMapping.size()) { p->symbolMappingIndex = 0; }
524  if (symbol == p->lits.EPSILON) { return spawnEmptyString(); }
525  else if (symbol == p->lits.EMPTY) { return spawnEmptySet(); }
526  else { return spawnSymbol(symbol); }
527  }
528  default:
529  return exptr();
530  }
531  }
token symbol
This tree&#39;s root symbol.
Definition: expression.cpp:463
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:420
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:464
vector< char32_t > symbolMapping
Stores the actual symbols encountered in the RE while parsing.
Definition: expression.cpp:421
size_t symbolMappingIndex
Index for when symbols have to be extracted from the mapping.
Definition: expression.cpp:422
parser const * p
Points to the parser this tree belongs to.
Definition: expression.cpp:462
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 464 of file expression.cpp.

◆ p

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

Points to the parser this tree belongs to.

Definition at line 462 of file expression.cpp.

◆ symbol

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

This tree's root symbol.

Definition at line 463 of file expression.cpp.


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