reglibcpp  1.7.0
(Naïve) C++ implementation of models for regular languages
expression.h
Go to the documentation of this file.
1 #ifndef REG_EXP_H
2 #define REG_EXP_H
3 
5 #include <vector>
6 
7 #include <unordered_map>
8 
9 #include <string>
10 
11 #include <memory>
12 
13 namespace reg {
14 class dfa;
15 class nfa;
17 
28 class expression {
29 public:
31 
41  static char32_t L, R, K, A, E, N;
42  static void reset();
43  static exptr const& spawnEmptySet();
44  static exptr const& spawnEmptyString();
45  static exptr const& spawnSymbol(char32_t symbol);
46  static exptr const& spawnSymbol(std::string const& utf8Symbol);
47  static exptr spawnKleene(exptr const& b, bool optimized = true, bool aggressive = false);
48  static exptr spawnConcatenation(exptr const& l, exptr const& r, bool optimized = true, bool aggressive = false);
49  static exptr spawnAlternation(exptr const& l, exptr const& r, bool optimized = true, bool aggressive = false);
51  struct literals {
52  char32_t const L,
53  R,
54  S,
55  P,
56  EPSILON,
57  EMPTY;
58 
67  literals(char32_t plus = U'+', char32_t empty = U'∅', char32_t epsilon = U'ε',
68  char32_t star = U'*', char32_t rPar = U')', char32_t lPar = U'(')
69  : L(lPar), R(rPar), S(star), P(plus), EPSILON(epsilon), EMPTY(empty){}
70  };
71  static exptr spawnFromString(std::u32string const& re, literals lits,
72  bool optimized = false, bool aggressive = false);
73  static exptr spawnFromString(std::string const& utf8Re, literals lits,
74  bool optimized = false, bool aggressive = false);
75  static exptr spawnFromString(std::u32string const& re, bool optimized = false, bool aggressive = false);
76  static exptr spawnFromString(std::string const& utf8Re, bool optimized = false, bool aggressive = false);
84  enum struct operation { empty, symbol, kleene, concatenation, alternation };
85  size_t size() const;
86  operation getOperation() const;
87  operator nfa const&() const;
88  bool operator==(nfa const& other) const;
89  bool operator!=(nfa const& other) const;
90  bool operator==(expression const& r) const;
91  bool operator!=(expression const& r) const;
92  char32_t extractSymbol() const;
95  std::string to_string() const;
98 private:
99  expression();
100  expression(char32_t symbol);
101  expression(exptr const& l, exptr const& r, operation op);
102  expression(exptr const& b);
103  expression(expression& e) = delete;
104  expression(expression&& e) = delete;
105  expression& operator=(expression& e) = delete;
106  expression& operator=(expression& e) const = delete;
107  expression& operator=(expression&& e) = delete;
108  expression& operator=(expression&& e) const = delete;
109  static exptr empty;
111  std::vector<exptr> const subExpressions;
112  operation const op;
113  std::unique_ptr<nfa const> mutable acceptingNfa;
114  struct parser;
115 };
116 }
117 #endif
char32_t const S
The Kleene star.
Definition: expression.h:52
Represents nondeterministic finite automata with ε-moves.
Definition: nfa.h:23
static char32_t N
The symbol used to represent the Null/empty set in a regular expression.
Definition: expression.h:41
char32_t const EMPTY
Neutral element of alternation and annihilating element of concatenation, a.k.a. empty set...
Definition: expression.h:52
bool operator==(nfa const &other) const
Checks whether this RE describes the same regular language as another object.
Definition: expression.cpp:222
std::vector< exptr >::const_iterator begin() const
Returns an iterator pointing to this RE's first subexpression.
Definition: expression.cpp:315
static exptr const & spawnEmptyString()
Gives an RE representing the empty string ε.
Definition: expression.cpp:59
bool operator!=(nfa const &other) const
Checks whether this RE describes a different regular language than another object.
Definition: expression.cpp:230
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
Represents formal regular expressions.
Definition: expression.h:28
std::u32string to_u32string() const
Describes this RE in UTF-32-encoded human-readable form.
Definition: expression.cpp:276
char32_t extractSymbol() const
Reports this symbol expression's UTF-32-encoded symbol.
Definition: expression.cpp:249
static exptr const & spawnSymbol(char32_t symbol)
Gives an RE representing the given UTF-32-encoded symbol.
Definition: expression.cpp:69
operation
The different purposes an RE may fulfill.
Definition: expression.h:84
operation getOperation() const
Reports this RE's function.
Definition: expression.cpp:205
std::vector< exptr >::const_iterator end() const
Returns an iterator pointing behind this RE's last subexpression.
Definition: expression.cpp:320
static char32_t K
The symbol used to represent the Kleene star in a regular expression.
Definition: expression.h:41
std::string to_string() const
Describes this RE in UTF-8-encoded human-readable form.
Definition: expression.cpp:310
char32_t const L
The left parenthesis.
Definition: expression.h:52
char32_t const P
The alternation symbol.
Definition: expression.h:52
size_t size() const
Reports the size of this RE's tree representation.
Definition: expression.cpp:192
static exptr spawnFromString(std::u32string const &re, literals lits, bool optimized=false, bool aggressive=false)
Definition: expression.cpp:609
static char32_t L
The symbol used to represent the Left parenthesis in a regular expression.
Definition: expression.h:41
Where this library lives.
Definition: dfa.cpp:51
char32_t const EPSILON
Neutral element of concatenation, a.k.a. empty string.
Definition: expression.h:52
static void reset()
Resets the symbols used for RE operators to their defaults.
Definition: expression.cpp:36
static char32_t E
The symbol used to represent the Empty string in a regular expression.
Definition: expression.h:41
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
char32_t const R
The right parenthesis.
Definition: expression.h:52
std::string extractUtf8Symbol() const
Reports this symbol expression's UTF-8-encoded symbol.
Definition: expression.cpp:266
literals(char32_t plus=U'+', char32_t empty=U '∅', char32_t epsilon=U 'ε', char32_t star=U' *', char32_t rPar=U')', char32_t lPar=U'(')
Definition: expression.h:67
static char32_t R
The symbol used to represent the Right parenthesis in a regular expression.
Definition: expression.h:41
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
static char32_t A
The symbol used to represent the Alternation in a regular expression.
Definition: expression.h:41