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

Parses regular expressions. More...

Classes

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

Public Types

enum  token : unsigned char {
  token::A, token::B, token::C, token::K,
  token::E, token::F, token::Σ, token::P,
  token::L, token::R, token::S, token::END
}
 Tokens the grammar deals with. More...
 
typedef bitset< TOKEN(END)> tokens
 Tokens don't usually come alone. More...
 

Public Member Functions

 parser (u32string const &re)
 Initializes with a string to parse. More...
 
exptr operator() (bool optimized, bool aggressive)
 Gives the RE resulting from parsing. More...
 

Static Public Member Functions

static tokens getUClosure (tokens const &m)
 Constructs the reflexive-transitive closure of the inverse unit relation for a given set of symbols. More...
 
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. More...
 
static void compileTableEntry (size_t row, size_t diag, vector< vector< tokens >> &table)
 Fills a table entry. More...
 

Public Attributes

vector< char32_t > symbolMapping
 Stores the actual symbols encountered in the RE while parsing. More...
 
size_t symbolMappingIndex
 Index for when symbols have to be extracted from the mapping. More...
 
vector< vector< tokens > > table
 The table of sets of symbols that derive a subsentence. More...
 
bool badRegularExpression = false
 Signifies that the RE used to initialize this object was invalid. More...
 

Static Public Attributes

static array< token, TOKEN(END)> const inverseUnitGraph
 Maps symbols that may be derived in-place to their predecessing symbols. More...
 
static array< array< tokens, TOKEN(END)>, TOKEN(END)> const inverseBinaryRules
 Maps pairs of symbols to the symbols that derive them. More...
 

Detailed Description

Parses regular expressions.

Recognizes REs generated by the following context-free grammar, where P stands for the alternative symbol (literal "+" or "|"), L for a left parenthesis (literal "("), R for a right parenthesis (literal ")") and S for the Kleene star (literal "*"). Σ is a placeholder for any symbol that makes up the regular language described by the RE:

GRE=(
       {A, B, C, K, E, F},
       {Σ, P, L, R, S}, A,
       {
         (A,C), (A,AB),
         (B,PC),
         (C,K), (C,CK),
         (K,E), (K,ES),
         (E,Σ), (E,LF),
         (F,AR)
       }
    )

This parser is based on the article To CNF or not to CNF? An Efficient Yet Presentable Version of the CYK Algorithm by Martin Lange and Hans Leiß, published in informatica didactica No. 8. Conventions will be based on this article.

Definition at line 350 of file expression.cpp.

Member Typedef Documentation

◆ tokens

Tokens don't usually come alone.

Definition at line 371 of file expression.cpp.

Member Enumeration Documentation

◆ token

enum reg::expression::parser::token : unsigned char
strong

Tokens the grammar deals with.

Enumerator

Beginning of an alternation expression.

Second part of an alternation expression.

A concatenation expression.

Kleene expression.

Beginning of a new subexpression.

Second part of a new subexpression.

Σ 

A symbol expression.

An alternation symbol.

A left parenthesis.

A right parenthesis.

A Kleene star symbol.

END 

Number of elements in this enumeration, NOT AN ACTUAL TOKEN!

Definition at line 352 of file expression.cpp.

352  : unsigned char {
353  A,
354  B,
355  C,
356  K,
357  E,
358  F,
359  Σ,
360  P,
361  L,
362  R,
363  S,
364  END
365  };

Constructor & Destructor Documentation

◆ parser()

reg::expression::parser::parser ( u32string const &  re)
inline

Initializes with a string to parse.

Definition at line 461 of file expression.cpp.

461  {
462  if (re.empty()) {
463  badRegularExpression = true;
464  return;
465  }
466  symbolMappingIndex = 0;
467  size_t numberOfTokens(re.length());
468  symbolMapping.reserve(numberOfTokens);
469  table.reserve(numberOfTokens);
470  size_t row(0);
471  for (char32_t symbol : re) {
472  table.push_back(vector<tokens>(numberOfTokens-row, tokens()));
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)); }
477  else {
478  table[row][0].set(TOKEN(Σ));
479  symbolMapping.push_back(symbol);
480  }
481  row++;
482  }
483  symbolMapping.shrink_to_fit();
484  for (size_t diag(1); diag < numberOfTokens; diag++) {
485  for (size_t row(0); row < numberOfTokens - diag; row++) {
486  compileTableEntry(row, diag, table);
487  }
488  }
489  if (!getUClosure(table[0][table[0].size()-1])[TOKEN(A)]) {
490  badRegularExpression = true;
491  }
492  }
static tokens getUClosure(tokens const &m)
Constructs the reflexive-transitive closure of the inverse unit relation for a given set of symbols...
Definition: expression.cpp:388
#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
bool badRegularExpression
Signifies that the RE used to initialize this object was invalid.
Definition: expression.cpp:458
vector< char32_t > symbolMapping
Stores the actual symbols encountered in the RE while parsing.
Definition: expression.cpp:455
bitset< TOKEN(END)> tokens
Tokens don't usually come alone.
Definition: expression.cpp:371
size_t symbolMappingIndex
Index for when symbols have to be extracted from the mapping.
Definition: expression.cpp:456
size_t size() const
Reports the size of this RE's tree representation.
Definition: expression.cpp:192
static void compileTableEntry(size_t row, size_t diag, vector< vector< tokens >> &table)
Fills a table entry.
Definition: expression.cpp:439

Member Function Documentation

◆ canDerive()

static bool reg::expression::parser::canDerive ( token  symbol,
tokens const &  left,
tokens const &  right 
)
inlinestatic

Checks if a token could derive a pair of tokens from two other entries.

Parameters
symbolthe original token
leftthe set of tokens allowed for the first half of the result pair
rightthe set of tokens allowed for the second half of the result pair
Returns
true if the original token may lead to any such pair, false else

Definition at line 407 of file expression.cpp.

407  {
408  tokens leftClosure(getUClosure(left));
409  tokens rightClosure(getUClosure(right));
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++) {
415  if (inverseBinaryRules[li][ri][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) {
418  return true;
419  }
420  }
421  }
422  }
423  }
424  }
425  }
426  }
427  return false;
428  }
static array< array< tokens, TOKEN(END)>, TOKEN(END)> const inverseBinaryRules
Maps pairs of symbols to the symbols that derive them.
Definition: expression.cpp:377
static tokens getUClosure(tokens const &m)
Constructs the reflexive-transitive closure of the inverse unit relation for a given set of symbols...
Definition: expression.cpp:388
#define TOKEN(T)
Gives casting to base type back to scoped enums, as God intended.
Definition: expression.cpp:368
bitset< TOKEN(END)> tokens
Tokens don't usually come alone.
Definition: expression.cpp:371
static array< token, TOKEN(END)> const inverseUnitGraph
Maps symbols that may be derived in-place to their predecessing symbols.
Definition: expression.cpp:373

◆ compileTableEntry()

static void reg::expression::parser::compileTableEntry ( size_t  row,
size_t  diag,
vector< vector< tokens >> &  table 
)
inlinestatic

Fills a table entry.

The entry will be filled with the set of symbols deriving the subsentence beginning at the diagonal entry in the same row and ending at the diagonal entry in the same column.

Parameters
rowthe row of the entry to fill
diagthe diagonal of the entry to fill
tablethe table to operate on

Definition at line 439 of file expression.cpp.

439  {
440  for (size_t i(0); i < diag; i++) {
441  tokens first(getUClosure(table[row][i]));
442  for (unsigned char fi(0); fi < first.size(); fi++) {
443  if (first[fi]) {
444  tokens second(getUClosure(table[row+1+i][diag-i-1]));
445  for (unsigned char si(0); si < second.size(); si++) {
446  if (second[si]) {
447  table[row][diag] |= inverseBinaryRules[fi][si];
448  }
449  } // Going through the tokens in second.
450  }
451  } // Going through the tokens in first.
452  }
453  }
static array< array< tokens, TOKEN(END)>, TOKEN(END)> const inverseBinaryRules
Maps pairs of symbols to the symbols that derive them.
Definition: expression.cpp:377
static tokens getUClosure(tokens const &m)
Constructs the reflexive-transitive closure of the inverse unit relation for a given set of symbols...
Definition: expression.cpp:388
vector< vector< tokens > > table
The table of sets of symbols that derive a subsentence.
Definition: expression.cpp:457
bitset< TOKEN(END)> tokens
Tokens don't usually come alone.
Definition: expression.cpp:371

◆ getUClosure()

static tokens reg::expression::parser::getUClosure ( tokens const &  m)
inlinestatic

Constructs the reflexive-transitive closure of the inverse unit relation for a given set of symbols.

See Lemma 5 in the article.

Parameters
mthe set of symbols that may be derived in-place
Returns
the set of symbols that may be predecessors to any of the symbols in m

Definition at line 388 of file expression.cpp.

388  {
389  tokens closure;
390  for (unsigned char c(0); c < TOKEN(END); c++) {
391  if (m[c]) {
392  for (token s(static_cast<token>(c)); s != token::END; s = inverseUnitGraph[static_cast<unsigned char>(s)]) {
393  closure.set(static_cast<unsigned char>(s));
394  }
395  }
396  } // Going through the tokens in m.
397  return closure;
398  }
#define TOKEN(T)
Gives casting to base type back to scoped enums, as God intended.
Definition: expression.cpp:368
token
Tokens the grammar deals with.
Definition: expression.cpp:352
bitset< TOKEN(END)> tokens
Tokens don't usually come alone.
Definition: expression.cpp:371
Number of elements in this enumeration, NOT AN ACTUAL TOKEN!
static array< token, TOKEN(END)> const inverseUnitGraph
Maps symbols that may be derived in-place to their predecessing symbols.
Definition: expression.cpp:373

◆ operator()()

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

Gives the RE resulting from parsing.

Returns
exptr to the RE represented by the initialization string
Exceptions
std::bad_function_callif the regular expression string was malformed

Definition at line 573 of file expression.cpp.

573  {
574  if (badRegularExpression) {
575  throw std::bad_function_call();
576  }
577  return tree(0, table.size()-1, this)(optimized, aggressive);
578  }
vector< vector< tokens > > table
The table of sets of symbols that derive a subsentence.
Definition: expression.cpp:457
bool badRegularExpression
Signifies that the RE used to initialize this object was invalid.
Definition: expression.cpp:458

Member Data Documentation

◆ badRegularExpression

bool reg::expression::parser::badRegularExpression = false

Signifies that the RE used to initialize this object was invalid.

Definition at line 458 of file expression.cpp.

◆ inverseBinaryRules

auto const reg::expression::parser::inverseBinaryRules
static
Initial value:
=
[]()->array<array<expression::parser::tokens,TOKEN(END)>,TOKEN(END)>{
array<tokens,TOKEN(END)> noPredecessor;
noPredecessor.fill(tokens());
array<array<tokens,TOKEN(END)>,TOKEN(END)> rules;
rules.fill(noPredecessor);
rules[TOKEN(A)][TOKEN(B)].set(TOKEN(A));
rules[TOKEN(P)][TOKEN(C)].set(TOKEN(B));
rules[TOKEN(C)][TOKEN(K)].set(TOKEN(C));
rules[TOKEN(E)][TOKEN(S)].set(TOKEN(K));
rules[TOKEN(L)][TOKEN(F)].set(TOKEN(E));
rules[TOKEN(A)][TOKEN(R)].set(TOKEN(F));
return rules;
}()

Maps pairs of symbols to the symbols that derive them.

Definition at line 377 of file expression.cpp.

◆ inverseUnitGraph

auto const reg::expression::parser::inverseUnitGraph
static
Initial value:
=
[]()->array<expression::parser::token,TOKEN(END)>{
array<token,TOKEN(END)> graph;
graph.fill(token::END);
graph[TOKEN(Σ)] = token::E;
graph[TOKEN(E)] = token::K;
graph[TOKEN(K)] = token::C;
graph[TOKEN(C)] = token::A;
return graph;
}()

Maps symbols that may be derived in-place to their predecessing symbols.

See Definition 2 and Lemma 4 in the article.

Definition at line 373 of file expression.cpp.

◆ symbolMapping

vector<char32_t> reg::expression::parser::symbolMapping

Stores the actual symbols encountered in the RE while parsing.

Definition at line 455 of file expression.cpp.

◆ symbolMappingIndex

size_t reg::expression::parser::symbolMappingIndex
mutable

Index for when symbols have to be extracted from the mapping.

Definition at line 456 of file expression.cpp.

◆ table

vector<vector<tokens> > reg::expression::parser::table

The table of sets of symbols that derive a subsentence.

Definition at line 457 of file expression.cpp.


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