Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
RightRegularEquationSolver.h
Go to the documentation of this file.
1
6#pragma once
7
10
12
13namespace equations {
14
15template < class TerminalSymbolType, class VariableSymbolType >
16class RightRegularEquationSolver : public RegularEquationSolver < TerminalSymbolType, VariableSymbolType > {
21
22};
23
24template < class TerminalSymbolType, class VariableSymbolType >
26 for ( auto itA = this->nonterminalSymbolsByDepth.rbegin ( ); itA != this->nonterminalSymbolsByDepth.rend ( ); ++ itA ) {
27 const VariableSymbolType & a = * itA;
28
29 /*
30 * Apply Arden's Lemma
31 * A = 0A + 1B + 2C
32 * => A = 0*1B + 0*2C
33 */
34 regexp::UnboundedRegExpIteration < TerminalSymbolType > loop ( std::move ( this->equationTransition.at ( std::make_pair ( a, a ) ) ) );
36
37 // for all transitions from A apply Arden's Lemma
38 for ( auto itB = std::next ( itA ); itB != this->nonterminalSymbolsByDepth.rend ( ); ++ itB ) {
39 const VariableSymbolType & b = * itB;
40
42 concat.appendElement ( loop );
43 concat.appendElement ( std::move ( this->equationTransition.at ( std::make_pair ( a, b ) ) ) );
45 alt.appendElement ( std::move ( concat ) );
46 this->equationTransition.at ( std::make_pair ( a, b ) ) = std::move ( alt );
47 regexp::simplify::RegExpOptimize::optimize ( this->equationTransition.at ( std::make_pair ( a, b ) ) );
48 }
49
50 {
52 concat.appendElement ( std::move ( loop ) );
53 concat.appendElement ( std::move ( this->equationFinal.at ( a ) ) );
55 alt.appendElement ( std::move ( concat ) );
56 this->equationFinal.at ( a ) = std::move ( alt );
57 regexp::simplify::RegExpOptimize::optimize ( this->equationFinal.at ( a ) );
58 }
59
60 /*
61 * eliminate A from rest of the equations using this pattern:
62 * B->C = B->C + concatenate(B->A, A->C)
63 */
64 for ( auto itB = std::next ( itA ); itB != this->nonterminalSymbolsByDepth.rend ( ); ++ itB ) {
65 const VariableSymbolType & b = * itB;
66
67 for ( auto itC = std::next ( itA ); itC != this->nonterminalSymbolsByDepth.rend ( ); ++ itC ) {
68 const VariableSymbolType & c = * itC;
69
71 concat.appendElement ( this->equationTransition.at ( std::make_pair ( b, a ) ) );
72 concat.appendElement ( this->equationTransition.at ( std::make_pair ( a, c ) ) );
74 alt.appendElement ( std::move ( this->equationTransition.at ( std::make_pair ( b, c ) ) ) );
75 alt.appendElement ( std::move ( concat ) );
76 this->equationTransition.at ( std::make_pair ( b, c ) ) = std::move ( alt );
77 regexp::simplify::RegExpOptimize::optimize ( this->equationTransition.at ( std::make_pair ( b, c ) ) );
78 }
79
80 {
82 concat.appendElement ( std::move ( this->equationTransition.at ( std::make_pair ( b, a ) ) ) );
83 concat.appendElement ( this->equationFinal.at ( a ) );
85 alt.appendElement ( std::move ( this->equationFinal.at ( b ) ) );
86 alt.appendElement ( std::move ( concat ) );
87 this->equationFinal.at ( b ) = std::move ( alt );
88 regexp::simplify::RegExpOptimize::optimize ( this->equationFinal.at ( b ) );
89 }
90 }
91 }
92
93 return regexp::UnboundedRegExp < TerminalSymbolType > ( regexp::simplify::RegExpOptimize::optimize ( regexp::UnboundedRegExpStructure < TerminalSymbolType > ( std::move ( this->equationFinal.at ( * this->nonterminalSymbolsByDepth.begin ( ) ) ) ) ) );
94}
95
96} /* namespace equations */
97
Definition: RegularEquationSolver.h:27
Definition: RightRegularEquationSolver.h:16
Represents the alternation operator in the regular expression. The node can have 0 to n children in l...
Definition: UnboundedRegExpAlternation.h:44
void appendElement(UnboundedRegExpElement< SymbolType > &&element)
Definition: UnboundedRegExpAlternation.h:195
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
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
Unbounded regular expression represents regular expression. It describes regular languages....
Definition: UnboundedRegExp.h:80
static regexp::UnboundedRegExp< SymbolType > optimize(const regexp::UnboundedRegExp< SymbolType > &regexp)
Definition: LeftRegularEquationSolver.h:13
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79