reglibcpp  1.7.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 495 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 528 of file expression.cpp.

528  :
529  p(p),
530  symbol([&]()->token{
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);
534  }
535  }
536  return token::END;
537  }()),
538  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:510
token symbol
This tree&#39;s root symbol.
Definition: expression.cpp:497
#define TOKEN(T)
Gives casting to base type back to scoped enums, as God intended.
Definition: expression.cpp:368
vector< vector< tokens > > table
The table of sets of symbols that derive a subsentence.
Definition: expression.cpp:457
pair< unique_ptr< tree >, unique_ptr< tree > > children
Trees with the symbols of the entry's derived pair as root.
Definition: expression.cpp:498
token
Tokens the grammar deals with.
Definition: expression.cpp:352
parser const * p
Points to the parser this tree belongs to.
Definition: expression.cpp:496
Number of elements in this enumeration, NOT AN ACTUAL TOKEN!

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

510  {
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;
515  if (canDerive(symbol, p->table[row][leftDiag], p->table[rightRow][rightDiag])) {
516  return make_pair(make_unique<tree>(row, leftDiag, p), make_unique<tree>(rightRow, rightDiag, p));
517  }
518  }
520  }
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:407
token symbol
This tree&#39;s root symbol.
Definition: expression.cpp:497
vector< vector< tokens > > table
The table of sets of symbols that derive a subsentence.
Definition: expression.cpp:457
T make_pair(T... args)
parser const * p
Points to the parser this tree belongs to.
Definition: expression.cpp:496

◆ operator()()

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

Gives the RE encoded in this tree.

Definition at line 541 of file expression.cpp.

541  {
542  switch (symbol) {
543  case token::A:
544  return spawnAlternation((*children.first)(optimized, aggressive), (*children.second)(optimized, aggressive), optimized, aggressive);
545  case token::B:
546  return (*children.second)(optimized, aggressive);
547  case token::C:
548  return spawnConcatenation((*children.first)(optimized, aggressive), (*children.second)(optimized, aggressive), optimized, aggressive);
549  case token::K:
550  return spawnKleene((*children.first)(optimized, aggressive), optimized, aggressive);
551  case token::E:
552  return (*children.second)(optimized, aggressive);
553  case token::F:
554  return (*children.first)(optimized, aggressive);
555  case token::Σ: {
556  char32_t symbol = p->symbolMapping[p->symbolMappingIndex++];
557  if (p->symbolMappingIndex >= p->symbolMapping.size()) { p->symbolMappingIndex = 0; }
558  if (symbol == E) { return spawnEmptyString(); }
559  else if (symbol == N) { return spawnEmptySet(); }
560  else { return spawnSymbol(symbol); }
561  }
562  default:
563  return exptr();
564  }
565  }
token symbol
This tree&#39;s root symbol.
Definition: expression.cpp:497
static char32_t N
The symbol used to represent the Null/empty set in a regular expression.
Definition: expression.h:41
Beginning of a new subexpression.
static exptr const & spawnEmptyString()
Gives an RE representing the empty string ε.
Definition: expression.cpp:59
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:123
Second part of an alternation expression.
static exptr const & spawnSymbol(char32_t symbol)
Gives an RE representing the given UTF-32-encoded symbol.
Definition: expression.cpp:69
pair< unique_ptr< tree >, unique_ptr< tree > > children
Trees with the symbols of the entry's derived pair as root.
Definition: expression.cpp:498
vector< char32_t > symbolMapping
Stores the actual symbols encountered in the RE while parsing.
Definition: expression.cpp:455
size_t symbolMappingIndex
Index for when symbols have to be extracted from the mapping.
Definition: expression.cpp:456
parser const * p
Points to the parser this tree belongs to.
Definition: expression.cpp:496
Second part of a new subexpression.
Beginning of an alternation expression.
std::shared_ptr< expression const > exptr
This is the type used to handle regular expressions.
Definition: expression.h:40
static exptr const & spawnEmptySet()
Gives an RE representing the empty set ∅.
Definition: expression.cpp:50
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:164
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:88
A concatenation expression.

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

◆ p

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

Points to the parser this tree belongs to.

Definition at line 496 of file expression.cpp.

◆ symbol

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

This tree's root symbol.

Definition at line 497 of file expression.cpp.


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