Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
RandomizeGrammar.h
Go to the documentation of this file.
1
6#pragma once
7
13
15
16namespace grammar {
17
18namespace generate {
19
21public:
32 template < class TerminalSymbolType, class NonterminalSymbolType >
34
38 template < class TerminalSymbolType, class NonterminalSymbolType >
40
44 template < class TerminalSymbolType, class NonterminalSymbolType >
46
50 template < class TerminalSymbolType, class NonterminalSymbolType >
52
56 template < class TerminalSymbolType, class NonterminalSymbolType >
58
59};
60
61template < class TerminalSymbolType, class NonterminalSymbolType >
64
66
67 res.setNonterminalAlphabet ( gram.getNonterminalAlphabet ( ) );
68 res.setTerminalAlphabet ( gram.getTerminalAlphabet ( ) );
69
70 for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, TerminalSymbolType > > > > & rule : gram.getRules ( ) )
71 for ( const ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, NonterminalSymbolType > > & rhs : rule.second )
72 if ( rhs.template is < TerminalSymbolType > ( ) )
73 res.addRule ( symbolPermutationMap.at ( rule.first ), rhs );
74 else
75 res.addRule ( symbolPermutationMap.at ( rule.first ), ext::make_pair ( symbolPermutationMap.at ( rhs.template get < ext::pair < NonterminalSymbolType, TerminalSymbolType > > ( ).first ), rhs.template get < ext::pair < NonterminalSymbolType, TerminalSymbolType > > ( ).second ) );
76
77 return res;
78}
79
80template < class TerminalSymbolType, class NonterminalSymbolType >
83
85
86 res.setNonterminalAlphabet ( gram.getNonterminalAlphabet ( ) );
87 res.setTerminalAlphabet ( gram.getTerminalAlphabet ( ) );
88
89 for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < ext::vector < TerminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > > > & rule : gram.getRules ( ) )
90 for ( const ext::variant < ext::vector < TerminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > & rhs : rule.second )
91 if ( rhs.template is < ext::vector < TerminalSymbolType > > ( ) )
92 res.addRule ( symbolPermutationMap.at ( rule.first ), rhs );
93 else
94 res.addRule ( symbolPermutationMap.at ( rule.first ), ext::make_pair ( symbolPermutationMap.at ( rhs.template get < ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > ( ).first ), rhs.template get < ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > ( ).second ) );
95
96 return res;
97}
98
99template < class TerminalSymbolType, class NonterminalSymbolType >
102
104
105 res.setNonterminalAlphabet ( gram.getNonterminalAlphabet ( ) );
106 res.setTerminalAlphabet ( gram.getTerminalAlphabet ( ) );
107
108 for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, ext::pair < TerminalSymbolType, NonterminalSymbolType > > > > & rule : gram.getRules ( ) )
109 for ( const ext::variant < TerminalSymbolType, ext::pair < TerminalSymbolType, NonterminalSymbolType > > & rhs : rule.second )
110 if ( rhs.template is < TerminalSymbolType > ( ) )
111 res.addRule ( symbolPermutationMap.at ( rule.first ), rhs );
112 else
113 res.addRule ( symbolPermutationMap.at ( rule.first ), ext::make_pair ( rhs.template get < ext::pair < TerminalSymbolType, NonterminalSymbolType > > ( ).first, symbolPermutationMap.at ( rhs.template get < ext::pair < TerminalSymbolType, NonterminalSymbolType > > ( ).second ) ) );
114
115 return res;
116}
117
118template < class TerminalSymbolType, class NonterminalSymbolType >
121
123
124 res.setNonterminalAlphabet ( gram.getNonterminalAlphabet ( ) );
125 res.setTerminalAlphabet ( gram.getTerminalAlphabet ( ) );
126
127 for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > > > & rule : gram.getRules ( ) )
128 for ( const ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > & rhs : rule.second )
129 if ( rhs.template is < ext::vector < TerminalSymbolType > > ( ) )
130 res.addRule ( symbolPermutationMap.at ( rule.first ), rhs );
131 else
132 res.addRule ( symbolPermutationMap.at ( rule.first ), ext::make_pair ( rhs.template get < ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > ( ).first, symbolPermutationMap.at ( rhs.template get < ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > ( ).second ) ) );
133
134 return res;
135}
136
137template < class TerminalSymbolType, class NonterminalSymbolType >
140
141 grammar::CFG < TerminalSymbolType, NonterminalSymbolType > res ( symbolPermutationMap.find ( gram.getInitialSymbol ( ) )->second );
142
143 res.setNonterminalAlphabet ( gram.getNonterminalAlphabet ( ) );
144 res.setTerminalAlphabet ( gram.getTerminalAlphabet ( ) );
145
146 for ( const std::pair < const NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > & rule : gram.getRules ( ) )
148 res.addRule ( symbolPermutationMap.find ( rule.first )->second, ext::transform < ext::variant < TerminalSymbolType, NonterminalSymbolType > > ( rhs, [ & ] ( const ext::variant < TerminalSymbolType, NonterminalSymbolType > & elem ) {
149 if ( gram.getNonterminalAlphabet ( ).count ( elem ) )
150 return ext::variant < TerminalSymbolType, NonterminalSymbolType > ( symbolPermutationMap.find ( elem )->second );
151 else
152 return elem;
153 } ) );
154
155 return res;
156}
157
158} /* namespace generate */
159
160} /* namespace grammar */
161
static ext::map< T, T > permutationMap(const ext::set< T > &data)
Definition: Permutation.hpp:23
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
R & at(const T &key, R &defaultValue)
Definition: map.hpp:163
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Definition: set.hpp:44
Implementation of the variant class allowing to store any type of those listed in the template parame...
Definition: variant.hpp:98
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Context free grammar in Chomsky hierarchy or type 2 in Chomsky hierarchy. Generates context free lang...
Definition: CFG.h:67
const NonterminalSymbolType & getInitialSymbol() const &
Definition: CFG.h:148
const ext::set< TerminalSymbolType > & getTerminalAlphabet() const &
Definition: CFG.h:215
const ext::set< NonterminalSymbolType > & getNonterminalAlphabet() const &
Definition: CFG.h:177
const ext::map< NonterminalSymbolType, ext::set< ext::vector< ext::variant< TerminalSymbolType, NonterminalSymbolType > > > > & getRules() const &
Definition: CFG.h:332
Left linear grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular languages.
Definition: LeftLG.h:66
const NonterminalSymbolType & getInitialSymbol() const &
Definition: LeftLG.h:160
const ext::set< NonterminalSymbolType > & getNonterminalAlphabet() const &
Definition: LeftLG.h:189
const ext::set< TerminalSymbolType > & getTerminalAlphabet() const &
Definition: LeftLG.h:227
const ext::map< NonterminalSymbolType, ext::set< ext::variant< ext::vector< TerminalSymbolType >, ext::pair< NonterminalSymbolType, ext::vector< TerminalSymbolType > > > > > & getRules() const &
Definition: LeftLG.h:359
Left regular grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular languages...
Definition: LeftRG.h:70
const ext::set< TerminalSymbolType > & getTerminalAlphabet() const &
Definition: LeftRG.h:236
const ext::map< NonterminalSymbolType, ext::set< ext::variant< TerminalSymbolType, ext::pair< NonterminalSymbolType, TerminalSymbolType > > > > & getRules() const &
Definition: LeftRG.h:376
const NonterminalSymbolType & getInitialSymbol() const &
Definition: LeftRG.h:169
const ext::set< NonterminalSymbolType > & getNonterminalAlphabet() const &
Definition: LeftRG.h:198
Right linear grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular languages...
Definition: RightLG.h:65
const ext::map< NonterminalSymbolType, ext::set< ext::variant< ext::vector< TerminalSymbolType >, ext::pair< ext::vector< TerminalSymbolType >, NonterminalSymbolType > > > > & getRules() const &
Definition: RightLG.h:356
const NonterminalSymbolType & getInitialSymbol() const &
Definition: RightLG.h:159
const ext::set< NonterminalSymbolType > & getNonterminalAlphabet() const &
Definition: RightLG.h:188
const ext::set< TerminalSymbolType > & getTerminalAlphabet() const &
Definition: RightLG.h:226
Right regular grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular language...
Definition: RightRG.h:70
const ext::map< NonterminalSymbolType, ext::set< ext::variant< TerminalSymbolType, ext::pair< TerminalSymbolType, NonterminalSymbolType > > > > & getRules() const &
Definition: RightRG.h:375
const ext::set< NonterminalSymbolType > & getNonterminalAlphabet() const &
Definition: RightRG.h:198
const ext::set< TerminalSymbolType > & getTerminalAlphabet() const &
Definition: RightRG.h:236
const NonterminalSymbolType & getInitialSymbol() const &
Definition: RightRG.h:169
Definition: RandomizeGrammar.h:20
static grammar::LeftRG< TerminalSymbolType, NonterminalSymbolType > randomize(const grammar::LeftRG< TerminalSymbolType, NonterminalSymbolType > &gram)
Definition: RandomizeGrammar.h:62
return res
Definition: MinimizeByPartitioning.h:145
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 make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
Definition: ToAutomaton.h:24
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693