reglibcpp  2.0.0
A C++ implementation of models for regular languages
expression.h
Go to the documentation of this file.
1 // Copyright 2017, 2018 Tom Kranz
2 //
3 // This file is part of reglibcpp.
4 //
5 // reglibcpp is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // reglibcpp is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with reglibcpp. If not, see <https://www.gnu.org/licenses/>.
17 
18 #ifndef REG_EXP_H
19 #define REG_EXP_H
20 
22 #include <vector>
23 
24 #include <unordered_map>
25 
26 #include <string>
27 
28 #include <memory>
29 
30 namespace reg {
31 class dfa;
32 class nfa;
35 
46 class expression {
47  public:
49 
59  static char32_t L, R, K, A, E, N;
60  static void reset();
61  static exptr const& spawnEmptySet();
62  static exptr const& spawnEmptyString();
63  static exptr const& spawnSymbol(std::string const& symbol);
64  static exptr const& spawnSymbol_(char32_t u32symbol);
65  static exptr spawnKleene(exptr const& b, bool optimized = true,
66  bool aggressive = false);
67  static exptr spawnConcatenation(exptr const& l, exptr const& r,
68  bool optimized = true,
69  bool aggressive = false);
70  static exptr spawnAlternation(exptr const& l, exptr const& r,
71  bool optimized = true, bool aggressive = false);
72  static exptr spawnFromString(std::string const& re, bool optimized = false,
73  bool aggressive = false);
74  static exptr spawnFromString_(std::u32string const& u32re,
75  bool optimized = false,
76  bool aggressive = false);
84  enum struct operation { empty, symbol, kleene, concatenation, alternation };
85  size_t size() const;
86  operation getOperation() const;
88  operator nfa const&() const;
89  bool operator==(nfa const& other) const;
90  bool operator!=(nfa const& other) const;
91  std::string extractSymbol() const;
92  char32_t extractSymbol_() const;
94  std::string to_string() const;
97 
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 } // namespace reg
117 #endif
Represents nondeterministic finite automata with ε-moves.
Definition: nfa.h:38
static char32_t N
The symbol used to represent the Null/empty set in a regular expression.
Definition: expression.h:59
bool operator==(nfa const &other) const
Checks whether this RE describes the same regular language as another object.
Definition: expression.cpp:286
std::vector< exptr >::const_iterator begin() const
Returns an iterator pointing to this RE's first subexpression.
Definition: expression.cpp:378
static exptr const & spawnEmptyString()
Gives an RE representing the empty string ε.
Definition: expression.cpp:89
bool operator!=(nfa const &other) const
Checks whether this RE describes a different regular language than another object.
Definition: expression.cpp:296
static exptr const & spawnSymbol(std::string const &symbol)
Gives an RE representing the given UTF-32-encoded symbol.
Definition: expression.cpp:116
std::string extractSymbol() const
Reports this symbol expression's UTF-8-encoded symbol.
Definition: expression.cpp:329
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:178
Represents formal regular expressions.
Definition: expression.h:46
static exptr spawnFromString_(std::u32string const &u32re, bool optimized=false, bool aggressive=false)
Gives an RE encoded in a given UTF-32 string.
Definition: expression.cpp:720
std::u32string to_u32string() const
Describes this RE in UTF-32-encoded human-readable form.
Definition: expression.cpp:335
static exptr const & spawnSymbol_(char32_t u32symbol)
Gives an RE representing the given UTF-32-encoded symbol.
Definition: expression.cpp:100
static exptr spawnFromString(std::string const &re, bool optimized=false, bool aggressive=false)
Gives an RE encoded in a given UTF-8 string.
Definition: expression.cpp:746
operation
The different purposes an RE may fulfill.
Definition: expression.h:84
operation getOperation() const
Reports this RE's function.
Definition: expression.cpp:269
std::vector< exptr >::const_iterator end() const
Returns an iterator pointing behind this RE's last subexpression.
Definition: expression.cpp:383
static char32_t K
The symbol used to represent the Kleene star in a regular expression.
Definition: expression.h:59
std::string to_string() const
Describes this RE in UTF-8-encoded human-readable form.
Definition: expression.cpp:373
size_t size() const
Reports the size of this RE's tree representation.
Definition: expression.cpp:255
static char32_t L
The symbol used to represent the Left parenthesis in a regular expression.
Definition: expression.h:59
Where this library lives.
Definition: dfa.cpp:48
static void reset()
Resets the symbols used for RE operators to their defaults.
Definition: expression.cpp:66
static char32_t E
The symbol used to represent the Empty string in a regular expression.
Definition: expression.h:59
char32_t extractSymbol_() const
Reports this symbol expression's UTF-32-encoded symbol.
Definition: expression.cpp:309
std::shared_ptr< expression const > exptr
This is the type used to handle regular expressions.
Definition: expression.h:58
static exptr const & spawnEmptySet()
Gives an RE representing the empty set ∅.
Definition: expression.cpp:80
static char32_t R
The symbol used to represent the Right parenthesis in a regular expression.
Definition: expression.h:59
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:224
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:135
static char32_t A
The symbol used to represent the Alternation in a regular expression.
Definition: expression.h:59