Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
RegExpDerivation.h
Go to the documentation of this file.
1
6#pragma once
7
12
13#include <string/LinearString.h>
14
17
19
20namespace regexp {
21
22namespace transform {
23
32public:
43 template < class SymbolType >
45
49 template < class SymbolType >
51
62 template < class SymbolType >
64
68 template < class SymbolType >
70
71private:
72 template < class SymbolType >
73 class Formal {
74 public:
75 static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpAlternation < SymbolType > & alternation, const SymbolType& argument);
76 static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpConcatenation < SymbolType > & concatenation, const SymbolType& argument);
77 static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpIteration < SymbolType > & iteration, const SymbolType& argument);
78 static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpSymbol < SymbolType > & symbol, const SymbolType& argument);
79 static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpEpsilon < SymbolType > & epsilon, const SymbolType& argument);
80 static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpEmpty < SymbolType > & empty, const SymbolType& argument);
81 };
82
83 template < class SymbolType >
84 class Unbounded {
85 public:
86 static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpAlternation < SymbolType > & alternation, const SymbolType& argument);
87 static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpConcatenation < SymbolType > & concatenation, const SymbolType& argument);
88 static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpIteration < SymbolType > & iteration, const SymbolType& argument);
89 static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpSymbol < SymbolType > & symbol, const SymbolType& argument);
90 static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpEpsilon < SymbolType > & epsilon, const SymbolType& argument);
91 static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpEmpty < SymbolType > & empty, const SymbolType& argument);
92 };
93};
94
95template < class SymbolType >
97 std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > newRegExp = regexp.getRegExp().getStructure().template accept<std::unique_ptr < regexp::FormalRegExpElement < SymbolType > >, regexp::transform::RegExpDerivation::Formal < SymbolType >>(symbol);
98
100}
101
102template < class SymbolType >
104 std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > newRegExp = regexp.getRegExp().getStructure().template accept<std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > >, regexp::transform::RegExpDerivation::Unbounded < SymbolType > > ( symbol );
105
107}
108
109template < class SymbolType >
111 std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > newRegExp ( regexp.getRegExp().getStructure().clone() );
112
113 for(const auto& symbol : string.getContent())
114 newRegExp = newRegExp->template accept<std::unique_ptr < regexp::FormalRegExpElement < SymbolType > >, regexp::transform::RegExpDerivation::Formal < SymbolType >>(symbol);
115
117}
118
119template < class SymbolType >
121 std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > newRegExp ( regexp.getRegExp().getStructure().clone() );
122
123 for(const auto& symbol : string.getContent())
124 newRegExp = newRegExp->template accept<std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > >, regexp::transform::RegExpDerivation::Unbounded < SymbolType > > ( symbol );
125
127}
128
129// ----------------------------------------------------------------------------
130
131template < class SymbolType >
132std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpDerivation::Formal < SymbolType >::visit(const regexp::FormalRegExpAlternation < SymbolType > & alternation, const SymbolType& argument) {
133 std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > leftDerivative = alternation.getLeftElement().template accept<std::unique_ptr < regexp::FormalRegExpElement < SymbolType > >, regexp::transform::RegExpDerivation::Formal < SymbolType >>(argument);
134 std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > rightDerivative = alternation.getRightElement().template accept<std::unique_ptr < regexp::FormalRegExpElement < SymbolType > >, regexp::transform::RegExpDerivation::Formal < SymbolType >>(argument);
135
136 return std::unique_ptr < FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpAlternation < SymbolType > ( std::move ( * leftDerivative ), std::move ( * rightDerivative ) ) );
137}
138
139template < class SymbolType >
140std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpDerivation::Formal < SymbolType >::visit(const regexp::FormalRegExpConcatenation < SymbolType > & concatenation, const SymbolType& argument) {
141 std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > leftDerivative = concatenation.getLeftElement().template accept<std::unique_ptr < regexp::FormalRegExpElement < SymbolType > >, regexp::transform::RegExpDerivation::Formal < SymbolType >>(argument);
142
143 std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > res ( new regexp::FormalRegExpConcatenation < SymbolType > ( std::move ( * leftDerivative ), ext::move_copy ( concatenation.getRightElement ( ) ) ) );
144
145 if(regexp::properties::RegExpEpsilon::languageContainsEpsilon(concatenation.getLeftElement())) {
146 std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > rightDerivative = concatenation.getRightElement().template accept<std::unique_ptr < regexp::FormalRegExpElement < SymbolType > >, regexp::transform::RegExpDerivation::Formal < SymbolType >>(argument);
147
148 res = std::unique_ptr < FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpAlternation < SymbolType > ( std::move ( * res ), std::move( * rightDerivative ) ) );
149 }
150
151 return res;
152}
153
154template < class SymbolType >
155std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpDerivation::Formal < SymbolType >::visit(const regexp::FormalRegExpIteration < SymbolType > & iteration, const SymbolType& argument) {
156 std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > elementDerivative = iteration.getElement().template accept<std::unique_ptr < regexp::FormalRegExpElement < SymbolType > >, regexp::transform::RegExpDerivation::Formal < SymbolType > > ( argument );
157 return std::unique_ptr < FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpConcatenation < SymbolType > ( * elementDerivative, iteration ) );
158}
159
160template < class SymbolType >
161std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpDerivation::Formal < SymbolType >::visit(const regexp::FormalRegExpSymbol < SymbolType > & symbol, const SymbolType& argument) {
162 if(argument == symbol.getSymbol())
163 return std::unique_ptr < FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpEpsilon < SymbolType > () );
164 else
165 return std::unique_ptr < FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpEmpty < SymbolType > () );
166}
167
168template < class SymbolType >
169std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpDerivation::Formal < SymbolType >::visit(const regexp::FormalRegExpEpsilon < SymbolType > &, const SymbolType&) {
170 return std::unique_ptr < FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpEmpty < SymbolType > () );
171}
172
173template < class SymbolType >
174std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpDerivation::Formal < SymbolType >::visit(const regexp::FormalRegExpEmpty < SymbolType > &, const SymbolType&) {
175 return std::unique_ptr < FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpEmpty < SymbolType > () );
176}
177
178// ----------------------------------------------------------------------------
179
180template < class SymbolType >
181std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpDerivation::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpAlternation < SymbolType > & alternation, const SymbolType& argument) {
182 std::unique_ptr < regexp::UnboundedRegExpAlternation < SymbolType > > ret ( new regexp::UnboundedRegExpAlternation < SymbolType > () );
183
184 for(const UnboundedRegExpElement < SymbolType > & child : alternation.getElements())
185 ret->appendElement( * ( child.template accept<std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > >, regexp::transform::RegExpDerivation::Unbounded < SymbolType > > ( argument ) ) );
186
187 return std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > ( std::move ( ret ) );
188}
189
190template < class SymbolType >
191std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpDerivation::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpConcatenation < SymbolType > & concatenation, const SymbolType& argument) {
192 std::unique_ptr < regexp::UnboundedRegExpAlternation < SymbolType > > ret ( new regexp::UnboundedRegExpAlternation < SymbolType > () );
193
194 for(auto child = concatenation.getElements().begin(); child != concatenation.getElements().end(); ++ child) {
196 concat.appendElement( * ( child->template accept<std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > >, regexp::transform::RegExpDerivation::Unbounded < SymbolType > > ( argument ) ) );
197
198 auto succeedingElement = child;
199 while ( ++ succeedingElement != concatenation.getElements().end())
200 concat.appendElement ( * succeedingElement );
201
202 ret->appendElement ( std::move ( concat ) );
203
205 break;
206 }
207
208 return std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > ( std::move ( ret ) );
209}
210
211template < class SymbolType >
212std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpDerivation::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpIteration < SymbolType > & iteration, const SymbolType& argument) {
213 std::unique_ptr < UnboundedRegExpConcatenation < SymbolType > > con ( new regexp::UnboundedRegExpConcatenation < SymbolType > ( ) );
214 con->appendElement ( * ( iteration.getElement().template accept<std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > >, regexp::transform::RegExpDerivation::Unbounded < SymbolType > > ( argument ) ) );
215 con->appendElement ( iteration );
216 return std::unique_ptr < UnboundedRegExpElement < SymbolType > > ( std::move ( con ) );
217}
218
219template < class SymbolType >
220std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpDerivation::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpSymbol < SymbolType > & symbol, const SymbolType& argument) {
221 if(argument == symbol.getSymbol())
222 return std::unique_ptr < UnboundedRegExpElement < SymbolType > > ( new regexp::UnboundedRegExpEpsilon < SymbolType > () );
223 else
224 return std::unique_ptr < UnboundedRegExpElement < SymbolType > > ( new regexp::UnboundedRegExpEmpty < SymbolType > () );
225}
226
227template < class SymbolType >
228std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpDerivation::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpEpsilon < SymbolType > &, const SymbolType&) {
229 return std::unique_ptr < UnboundedRegExpElement < SymbolType > > ( new regexp::UnboundedRegExpEmpty < SymbolType > () );
230}
231
232template < class SymbolType >
233std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpDerivation::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpEmpty < SymbolType > &, const SymbolType&) {
234 return std::unique_ptr < UnboundedRegExpElement < SymbolType > > ( new regexp::UnboundedRegExpEmpty < SymbolType > () );
235}
236
237} /* namespace transform */
238
239} /* namespace regexp */
240
Represents the alternation operator in the regular expression. The node must have exactly two childre...
Definition: FormalRegExpAlternation.h:44
Represents the concatenation operator in the regular expression. The node must have exactly two child...
Definition: FormalRegExpConcatenation.h:44
Represents the empty expression in the regular expression. The node can't have any children.
Definition: FormalRegExpEmpty.h:41
Represents the epsilon expression in the regular expression. The node can't have any children.
Definition: FormalRegExpEpsilon.h:41
Represents the iteration operator in the regular expression. The node has exactly one child.
Definition: FormalRegExpIteration.h:44
Represents formal regular expression structure. Regular expression is stored as a tree of FormalRegEx...
Definition: FormalRegExpStructure.h:45
Represents the symbol in the regular expression. The can't have any children.
Definition: FormalRegExpSymbol.h:42
Formal regular expression represents regular expression. It describes regular languages....
Definition: FormalRegExp.h:78
Represents the alternation operator in the regular expression. The node can have 0 to n children in l...
Definition: UnboundedRegExpAlternation.h:44
Represents the concatenation operator in the regular expression. The node can have 0 to n children in...
Definition: UnboundedRegExpConcatenation.h:44
void appendElement(UnboundedRegExpElement< SymbolType > &&element)
Definition: UnboundedRegExpConcatenation.h:195
Definition: UnboundedRegExpElement.h:62
Represents the empty expression in the regular expression. The node can't have any children.
Definition: UnboundedRegExpEmpty.h:41
Represents the epsilon expression in the regular expression. The node can't have any children.
Definition: UnboundedRegExpEpsilon.h:41
Represents the iteration operator in the regular expression. The node has exactly one child.
Definition: UnboundedRegExpIteration.h:43
Represents unbounded regular expression structure. Regular expression is stored as a tree of Unbounde...
Definition: UnboundedRegExpStructure.h:47
Represents the symbol in the regular expression. The can't have any children.
Definition: UnboundedRegExpSymbol.h:42
Unbounded regular expression represents regular expression. It describes regular languages....
Definition: UnboundedRegExp.h:80
static bool languageContainsEpsilon(const regexp::FormalRegExpElement< SymbolType > &regexp)
Definition: RegExpEpsilon.h:91
Definition: RegExpDerivation.h:31
static regexp::FormalRegExp< SymbolType > derivation(const regexp::FormalRegExp< SymbolType > &regexp, const string::LinearString< SymbolType > &string)
Definition: RegExpDerivation.h:110
Linear string.
Definition: LinearString.h:57
ext::set< ext::pair< StateType, StateType > > ret(const ext::set< ext::pair< StateType, StateType > > &S, const DeterministicPushdownStoreSymbolType &pdaSymbol, const InputSymbolType &input, const N &nondeterministic)
Definition: RHDPDACommon.h:57
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
return res
Definition: MinimizeByPartitioning.h:145
auto move_copy(const T &param)
Allow moving of copied instance of the source.
Definition: utility.hpp:45
ContainerType< ResType > transform(const ContainerType< InType, Ts ... > &in, Callback transform)
In container tranformation of all elements according to the tranform.
Definition: algorithm.hpp:150
constexpr auto visit(Visitor &&vis, Variants &&... vars)
Definition: variant.hpp:42
Definition: ToAutomaton.h:15