reglibcpp  1.3.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;
16 
27 class expression {
28 public:
30 
39  typedef std::shared_ptr<expression const> exptr;
40  static exptr const& spawnEmptySet();
41  static exptr const& spawnEmptyString();
42  static exptr const& spawnSymbol(char32_t symbol);
43  static exptr const& spawnSymbol(std::string const& utf8Symbol);
44  static exptr spawnKleene(exptr const& b, bool optimized = true, bool aggressive = false);
45  static exptr spawnConcatenation(exptr const& l, exptr const& r, bool optimized = true, bool aggressive = false);
46  static exptr spawnAlternation(exptr const& l, exptr const& r, bool optimized = true, bool aggressive = false);
48  struct literals {
49  char32_t const L,
50  R,
51  S,
52  P,
53  EPSILON,
54  EMPTY;
55 
64  literals(char32_t plus = U'+', char32_t empty = U'∅', char32_t epsilon = U'ε',
65  char32_t star = U'*', char32_t rPar = U')', char32_t lPar = U'(')
66  : L(lPar), R(rPar), S(star), P(plus), EPSILON(epsilon), EMPTY(empty){}
67  };
68  static exptr spawnFromString(std::u32string const& re, literals lits = literals(),
69  bool optimized = false, bool aggressive = false);
70  static exptr spawnFromString(std::string const& utf8Re, literals lits = literals(),
71  bool optimized = false, bool aggressive = false);
79  enum struct operation { empty, symbol, kleene, concatenation, alternation };
80  size_t size() const;
81  operation getOperation() const;
82  bool operator==(expression const& r) const;
83  bool operator!=(expression const& r) const;
84  char32_t extractSymbol() const;
85  std::string extractUtf8Symbol() const;
86  std::u32string to_u32string() const;
87  std::string to_string() const;
88  std::vector<exptr>::const_iterator begin() const;
89  std::vector<exptr>::const_iterator end() const;
90 private:
91  expression();
92  expression(char32_t symbol);
93  expression(exptr const& l, exptr const& r, operation op);
94  expression(exptr const& b);
95  expression(expression& e) = delete;
96  expression(expression&& e) = delete;
97  expression& operator=(expression& e) = delete;
98  expression& operator=(expression& e) const = delete;
99  expression& operator=(expression&& e) = delete;
100  expression& operator=(expression&& e) const = delete;
101  static exptr empty;
102  static std::unordered_map<char32_t, exptr> symbols;
103  std::vector<exptr> const subExpressions;
104  operation const op;
105  std::unique_ptr<dfa const> mutable acceptingDfa;
106  struct parser;
107 };
108 }
109 #endif
char32_t const S
The Kleene star.
Definition: expression.h:49
bool operator!=(expression const &r) const
Checks whether this RE is semantically different from another one.
Definition: expression.cpp:203
Token literals as used in Introduction to Automata Theory, Languages, and Computation by Hopcroft...
Definition: expression.h:48
char32_t const EMPTY
Neutral element of alternation and annihilating element of concatenation, a.k.a. empty set...
Definition: expression.h:49
std::vector< exptr >::const_iterator begin() const
Returns an iterator pointing to this RE's first subexpression.
Definition: expression.cpp:284
static exptr const & spawnEmptyString()
Gives an RE representing the empty string ε.
Definition: expression.cpp:42
static exptr spawnFromString(std::u32string const &re, literals lits=literals(), bool optimized=false, bool aggressive=false)
Gives an RE encoded in a given string.
Definition: expression.cpp:587
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
Represents formal regular expressions.
Definition: expression.h:27
std::u32string to_u32string() const
Describes this RE in UTF-32-encoded human-readable form.
Definition: expression.cpp:241
char32_t extractSymbol() const
Reports this symbol expression's UTF-32-encoded symbol.
Definition: expression.cpp:212
static exptr const & spawnSymbol(char32_t symbol)
Gives an RE representing the given UTF-32-encoded symbol.
Definition: expression.cpp:52
operation
The different purposes an RE may fulfill.
Definition: expression.h:79
operation getOperation() const
Reports this RE's function.
Definition: expression.cpp:181
std::vector< exptr >::const_iterator end() const
Returns an iterator pointing behind this RE's last subexpression.
Definition: expression.cpp:289
std::string to_string() const
Describes this RE in UTF-8-encoded human-readable form.
Definition: expression.cpp:279
bool operator==(expression const &r) const
Checks whether this RE is semantically equivalent to another one.
Definition: expression.cpp:189
char32_t const L
The left parenthesis.
Definition: expression.h:49
char32_t const P
The alternation symbol.
Definition: expression.h:49
size_t size() const
Reports the size of this RE's tree representation.
Definition: expression.cpp:168
Definition: dfa.cpp:29
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
char32_t const R
The right parenthesis.
Definition: expression.h:49
std::string extractUtf8Symbol() const
Reports this symbol expression's UTF-8-encoded symbol.
Definition: expression.cpp:231
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:64
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