Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Concepts
GrammarDiff.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <compare/DiffAux.h>
10
11#include <ostream>
12
13#include <ext/utility>
14#include <ext/typeinfo>
15#include <ext/iostream>
16#include <ext/algorithm>
17
18#include <alib/set>
19#include <alib/map>
20#include <alib/list>
21#include <alib/vector>
22
36
37namespace compare {
38
40private:
41 template < class SymbolType >
42 static void printDiff(const grammar::LeftLG < SymbolType > & a, const grammar::LeftLG < SymbolType > & b, ext::ostream & out );
43
44 template < class SymbolType >
45 static void printDiff(const grammar::LeftRG < SymbolType > & a, const grammar::LeftRG < SymbolType > & b, ext::ostream & out );
46
47 template < class SymbolType >
48 static void printDiff(const grammar::RightLG < SymbolType > & a, const grammar::RightLG < SymbolType > & b, ext::ostream & out );
49
50 template < class SymbolType >
51 static void printDiff(const grammar::RightRG < SymbolType > & a, const grammar::RightRG < SymbolType > & b, ext::ostream & out );
52
53 template < class SymbolType >
54 static void printDiff(const grammar::LG < SymbolType > & a, const grammar::LG < SymbolType > & b, ext::ostream & out );
55
56 template < class SymbolType >
57 static void printDiff(const grammar::CFG < SymbolType > & a, const grammar::CFG < SymbolType > & b, ext::ostream & out );
58
59 template < class SymbolType >
60 static void printDiff(const grammar::EpsilonFreeCFG < SymbolType > & a, const grammar::EpsilonFreeCFG < SymbolType > & b, ext::ostream & out );
61
62 template < class SymbolType >
63 static void printDiff(const grammar::CNF < SymbolType > & a, const grammar::CNF < SymbolType > & b, ext::ostream & out );
64
65 template < class SymbolType >
66 static void printDiff(const grammar::GNF < SymbolType > & a, const grammar::GNF < SymbolType > & b, ext::ostream & out );
67
68 template < class SymbolType >
69 static void printDiff(const grammar::CSG < SymbolType > & a, const grammar::CSG < SymbolType > & b, ext::ostream & out );
70
71 template < class SymbolType >
73
74 template < class SymbolType >
76
77 template < class SymbolType >
79public:
80 template<class T>
81 static void diff(const T & a, const T & b, ext::ostream & out );
82
83 template < class T >
84 static std::string diff ( const T & a, const T & b );
85};
86
87template < class SymbolType >
88void GrammarDiff::printDiff(const grammar::LeftLG < SymbolType > & a, const grammar::LeftLG < SymbolType > & b, ext::ostream & out ) {
89 out << "GrammarsComparer" << std::endl;
90
92 out << "Nonterminal alphabet" << std::endl;
93
95 }
96
97 if(a.getRules() != b.getRules()){
98 out << "Rules" << std::endl;
99
100 DiffAux::mapDiff ( out, a.getRules(), b.getRules());
101 }
102
103 if(a.getInitialSymbol() != b.getInitialSymbol()){
104 out << "Initial symbol" << std::endl;
105
106 out << "< " << a.getInitialSymbol() << std::endl;
107 out << "---" << std::endl;
108 out << "> " << b.getInitialSymbol() << std::endl;
109 }
110
112 out << "Terminal alphabet" << std::endl;
113
115 }
116}
117
118template < class SymbolType >
119void GrammarDiff::printDiff(const grammar::LeftRG < SymbolType > & a, const grammar::LeftRG < SymbolType > & b, ext::ostream & out ) {
120 out << "GrammarsComparer" << std::endl;
121
123 out << "Nonterminal alphabet" << std::endl;
124
126 }
127
128 if(a.getRules() != b.getRules()){
129 out << "Rules" << std::endl;
130
131 DiffAux::mapDiff ( out, a.getRules(), b.getRules());
132 }
133
134 if(a.getInitialSymbol() != b.getInitialSymbol()){
135 out << "Initial symbol" << std::endl;
136
137 out << "< " << a.getInitialSymbol() << std::endl;
138 out << "---" << std::endl;
139 out << "> " << b.getInitialSymbol() << std::endl;
140 }
141
143 out << "Terminal alphabet" << std::endl;
144
146 }
147}
148
149template < class SymbolType >
150void GrammarDiff::printDiff(const grammar::RightLG < SymbolType > & a, const grammar::RightLG < SymbolType > & b, ext::ostream & out ) {
151 out << "GrammarsComparer" << std::endl;
152
154 out << "Nonterminal alphabet" << std::endl;
155
157 }
158
159 if(a.getRules() != b.getRules()){
160 out << "Rules" << std::endl;
161
162 DiffAux::mapDiff ( out, a.getRules(), b.getRules());
163 }
164
165 if(a.getInitialSymbol() != b.getInitialSymbol()){
166 out << "Initial symbol" << std::endl;
167
168 out << "< " << a.getInitialSymbol() << std::endl;
169 out << "---" << std::endl;
170 out << "> " << b.getInitialSymbol() << std::endl;
171 }
172
174 out << "Terminal alphabet" << std::endl;
175
177 }
178}
179
180template < class SymbolType >
181void GrammarDiff::printDiff(const grammar::RightRG < SymbolType > & a, const grammar::RightRG < SymbolType > & b, ext::ostream & out ) {
182 out << "GrammarsComparer" << std::endl;
183
185 out << "Nonterminal alphabet" << std::endl;
186
188 }
189
190 if(a.getRules() != b.getRules()){
191 out << "Rules" << std::endl;
192
193 DiffAux::mapDiff ( out, a.getRules(), b.getRules());
194 }
195
196 if(a.getInitialSymbol() != b.getInitialSymbol()){
197 out << "Initial symbol" << std::endl;
198
199 out << "< " << a.getInitialSymbol() << std::endl;
200 out << "---" << std::endl;
201 out << "> " << b.getInitialSymbol() << std::endl;
202 }
203
205 out << "Terminal alphabet" << std::endl;
206
208 }
209}
210
211template < class SymbolType >
212void GrammarDiff::printDiff(const grammar::LG < SymbolType > & a, const grammar::LG < SymbolType > & b, ext::ostream & out ) {
213 out << "GrammarsComparer" << std::endl;
214
216 out << "Nonterminal alphabet" << std::endl;
217
219 }
220
221 if(a.getRules() != b.getRules()){
222 out << "Rules" << std::endl;
223
224 DiffAux::mapDiff ( out, a.getRules(), b.getRules());
225 }
226
227 if(a.getInitialSymbol() != b.getInitialSymbol()){
228 out << "Initial symbol" << std::endl;
229
230 out << "< " << a.getInitialSymbol() << std::endl;
231 out << "---" << std::endl;
232 out << "> " << b.getInitialSymbol() << std::endl;
233 }
234
236 out << "Terminal alphabet" << std::endl;
237
239 }
240}
241
242template < class SymbolType >
243void GrammarDiff::printDiff(const grammar::CFG < SymbolType > & a, const grammar::CFG < SymbolType > & b, ext::ostream & out ) {
244 out << "GrammarsComparer" << std::endl;
245
247 out << "Nonterminal alphabet" << std::endl;
248
250 }
251
252 if(a.getRules() != b.getRules()){
253 out << "Rules" << std::endl;
254
255 DiffAux::mapDiff ( out, a.getRules(), b.getRules());
256 }
257
258 if(a.getInitialSymbol() != b.getInitialSymbol()){
259 out << "Initial symbol" << std::endl;
260
261 out << "< " << a.getInitialSymbol() << std::endl;
262 out << "---" << std::endl;
263 out << "> " << b.getInitialSymbol() << std::endl;
264 }
265
267 out << "Terminal alphabet" << std::endl;
268
270 }
271}
272
273template < class SymbolType >
274void GrammarDiff::printDiff(const grammar::EpsilonFreeCFG < SymbolType > & a, const grammar::EpsilonFreeCFG < SymbolType > & b, ext::ostream & out ) {
275 out << "GrammarsComparer" << std::endl;
276
278 out << "Nonterminal alphabet" << std::endl;
279
281 }
282
283 if(a.getRules() != b.getRules()){
284 out << "Rules" << std::endl;
285
286 DiffAux::mapDiff ( out, a.getRules(), b.getRules());
287 }
288
289 if(a.getInitialSymbol() != b.getInitialSymbol()){
290 out << "Initial symbol" << std::endl;
291
292 out << "< " << a.getInitialSymbol() << std::endl;
293 out << "---" << std::endl;
294 out << "> " << b.getInitialSymbol() << std::endl;
295 }
296
298 out << "Terminal alphabet" << std::endl;
299
301 }
302}
303
304template < class SymbolType >
305void GrammarDiff::printDiff(const grammar::CNF < SymbolType > & a, const grammar::CNF < SymbolType > & b, ext::ostream & out ) {
306 out << "GrammarsComparer" << std::endl;
307
309 out << "Nonterminal alphabet" << std::endl;
310
312 }
313
314 if(a.getRules() != b.getRules()){
315 out << "Rules" << std::endl;
316
317 DiffAux::mapDiff ( out, a.getRules(), b.getRules());
318 }
319
320 if(a.getInitialSymbol() != b.getInitialSymbol()){
321 out << "Initial symbol" << std::endl;
322
323 out << "< " << a.getInitialSymbol() << std::endl;
324 out << "---" << std::endl;
325 out << "> " << b.getInitialSymbol() << std::endl;
326 }
327
329 out << "Terminal alphabet" << std::endl;
330
332 }
333}
334
335template < class SymbolType >
336void GrammarDiff::printDiff(const grammar::GNF < SymbolType > & a, const grammar::GNF < SymbolType > & b, ext::ostream & out ) {
337 out << "GrammarsComparer" << std::endl;
338
340 out << "Nonterminal alphabet" << std::endl;
341
343 }
344
345 if(a.getRules() != b.getRules()){
346 out << "Rules" << std::endl;
347
348 DiffAux::mapDiff ( out, a.getRules(), b.getRules());
349 }
350
351 if(a.getInitialSymbol() != b.getInitialSymbol()){
352 out << "Initial symbol" << std::endl;
353
354 out << "< " << a.getInitialSymbol() << std::endl;
355 out << "---" << std::endl;
356 out << "> " << b.getInitialSymbol() << std::endl;
357 }
358
360 out << "Terminal alphabet" << std::endl;
361
363 }
364}
365
366template < class SymbolType >
367void GrammarDiff::printDiff(const grammar::CSG < SymbolType > & a, const grammar::CSG < SymbolType > & b, ext::ostream & out ) {
368 out << "GrammarsComparer" << std::endl;
369
371 out << "Nonterminal alphabet" << std::endl;
372
374 }
375
376 if(a.getRules() != b.getRules()){
377 out << "Rules" << std::endl;
378
379 DiffAux::mapDiff ( out, a.getRules(), b.getRules());
380 }
381
382 if(a.getInitialSymbol() != b.getInitialSymbol()){
383 out << "Initial symbol" << std::endl;
384
385 out << "< " << a.getInitialSymbol() << std::endl;
386 out << "---" << std::endl;
387 out << "> " << b.getInitialSymbol() << std::endl;
388 }
389
391 out << "Terminal alphabet" << std::endl;
392
394 }
395}
396
397template < class SymbolType >
399 out << "GrammarsComparer" << std::endl;
400
402 out << "Nonterminal alphabet" << std::endl;
403
405 }
406
407 if(a.getRules() != b.getRules()){
408 out << "Rules" << std::endl;
409
410 DiffAux::mapDiff ( out, a.getRules(), b.getRules());
411 }
412
413 if(a.getInitialSymbol() != b.getInitialSymbol()){
414 out << "Initial symbol" << std::endl;
415
416 out << "< " << a.getInitialSymbol() << std::endl;
417 out << "---" << std::endl;
418 out << "> " << b.getInitialSymbol() << std::endl;
419 }
420
422 out << "Terminal alphabet" << std::endl;
423
425 }
426}
427
428template < class SymbolType >
430 out << "GrammarsComparer" << std::endl;
431
433 out << "Nonterminal alphabet" << std::endl;
434
436 }
437
438 if(a.getRules() != b.getRules()){
439 out << "Rules" << std::endl;
440
441 DiffAux::mapDiff ( out, a.getRules(), b.getRules());
442 }
443
444 if(a.getInitialSymbol() != b.getInitialSymbol()){
445 out << "Initial symbol" << std::endl;
446
447 out << "< " << a.getInitialSymbol() << std::endl;
448 out << "---" << std::endl;
449 out << "> " << b.getInitialSymbol() << std::endl;
450 }
451
453 out << "Terminal alphabet" << std::endl;
454
456 }
457}
458
459template < class SymbolType >
460void GrammarDiff::printDiff(const grammar::UnrestrictedGrammar < SymbolType > & a, const grammar::UnrestrictedGrammar < SymbolType > & b, ext::ostream & out ) {
461 out << "GrammarsComparer" << std::endl;
462
464 out << "Nonterminal alphabet" << std::endl;
465
467 }
468
469 if(a.getRules() != b.getRules()){
470 out << "Rules" << std::endl;
471
472 DiffAux::mapDiff ( out, a.getRules(), b.getRules());
473 }
474
475 if(a.getInitialSymbol() != b.getInitialSymbol()){
476 out << "Initial symbol" << std::endl;
477
478 out << "< " << a.getInitialSymbol() << std::endl;
479 out << "---" << std::endl;
480 out << "> " << b.getInitialSymbol() << std::endl;
481 }
482
484 out << "Terminal alphabet" << std::endl;
485
487 }
488}
489
490template < class T >
491void GrammarDiff::diff(const T & a, const T & b, ext::ostream & out ) {
492 if(!GrammarCompare::compare(a, b)) {
493 GrammarDiff::printDiff ( a, b, out );
494 }
495}
496
497template < class T >
498std::string GrammarDiff::diff ( const T & a, const T & b ) {
500 diff ( a, b, ss );
501 return ss.str ( );
502}
503
504} /* namespace compare */
505
static void setDiff(ext::ostream &out, const ext::set< T > &a, const ext::set< T > &b)
Definition: DiffAux.h:33
static void mapDiff(ext::ostream &out, const ext::map< T, R > &a, const ext::map< T, R > &b)
Definition: DiffAux.h:84
static bool compare(const grammar::LeftLG< SymbolType > &a, const grammar::LeftLG< SymbolType > &b)
Definition: GrammarCompare.h:67
Definition: GrammarDiff.h:39
static void diff(const T &a, const T &b, ext::ostream &out)
Definition: GrammarDiff.h:491
Definition: ostream.h:14
Definition: sstream.h:15
std::string str() const &
Definition: sstream.cpp:29
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
Chomsky normal form of a context free grammar in Chomsky hierarchy or type 2 in Chomsky hierarchy....
Definition: CNF.h:66
const NonterminalSymbolType & getInitialSymbol() const &
Definition: CNF.h:165
const ext::map< NonterminalSymbolType, ext::set< ext::variant< TerminalSymbolType, ext::pair< NonterminalSymbolType, NonterminalSymbolType > > > > & getRules() const &
Definition: CNF.h:373
const ext::set< NonterminalSymbolType > & getNonterminalAlphabet() const &
Definition: CNF.h:194
const ext::set< TerminalSymbolType > & getTerminalAlphabet() const &
Definition: CNF.h:232
Context sensitive grammar in Chomsky hierarchy or type 1 in Chomsky hierarchy. Generates context sens...
Definition: CSG.h:64
const ext::set< SymbolType > & getNonterminalAlphabet() const &
Definition: CSG.h:178
const ext::set< SymbolType > & getTerminalAlphabet() const &
Definition: CSG.h:216
const SymbolType & getInitialSymbol() const &
Definition: CSG.h:149
const ext::map< ext::tuple< ext::vector< SymbolType >, SymbolType, ext::vector< SymbolType > >, ext::set< ext::vector< SymbolType > > > & getRules() const &
Definition: CSG.h:363
Context preserving unrestricted grammar. Type 0 in Chomsky hierarchy. Generates recursively enumerabl...
Definition: ContextPreservingUnrestrictedGrammar.h:64
const ext::map< ext::tuple< ext::vector< SymbolType >, SymbolType, ext::vector< SymbolType > >, ext::set< ext::vector< SymbolType > > > & getRules() const &
Definition: ContextPreservingUnrestrictedGrammar.h:334
const ext::set< SymbolType > & getNonterminalAlphabet() const &
Definition: ContextPreservingUnrestrictedGrammar.h:173
const ext::set< SymbolType > & getTerminalAlphabet() const &
Definition: ContextPreservingUnrestrictedGrammar.h:211
const SymbolType & getInitialSymbol() const &
Definition: ContextPreservingUnrestrictedGrammar.h:144
Context free grammar without epsilon rules in Chomsky hierarchy or type 2 in Chomsky hierarchy....
Definition: EpsilonFreeCFG.h:65
const ext::set< NonterminalSymbolType > & getNonterminalAlphabet() const &
Definition: EpsilonFreeCFG.h:173
const NonterminalSymbolType & getInitialSymbol() const &
Definition: EpsilonFreeCFG.h:144
const ext::map< NonterminalSymbolType, ext::set< ext::vector< ext::variant< TerminalSymbolType, NonterminalSymbolType > > > > & getRules() const &
Definition: EpsilonFreeCFG.h:340
const ext::set< TerminalSymbolType > & getTerminalAlphabet() const &
Definition: EpsilonFreeCFG.h:211
Greibach normal form of a context free grammar in Chomsky hierarchy or type 2 in Chomsky hierarchy....
Definition: GNF.h:65
const ext::set< TerminalSymbolType > & getTerminalAlphabet() const &
Definition: GNF.h:211
const ext::map< NonterminalSymbolType, ext::set< ext::pair< TerminalSymbolType, ext::vector< NonterminalSymbolType > > > > & getRules() const &
Definition: GNF.h:339
const NonterminalSymbolType & getInitialSymbol() const &
Definition: GNF.h:144
const ext::set< NonterminalSymbolType > & getNonterminalAlphabet() const &
Definition: GNF.h:173
Context free grammar in Chomsky hierarchy or type 2 in Chomsky hierarchy. Generates context free lang...
Definition: LG.h:67
const NonterminalSymbolType & getInitialSymbol() const &
Definition: LG.h:161
const ext::set< NonterminalSymbolType > & getNonterminalAlphabet() const &
Definition: LG.h:190
const ext::map< NonterminalSymbolType, ext::set< ext::variant< ext::vector< TerminalSymbolType >, ext::tuple< ext::vector< TerminalSymbolType >, NonterminalSymbolType, ext::vector< TerminalSymbolType > > > > > & getRules() const &
Definition: LG.h:370
const ext::set< TerminalSymbolType > & getTerminalAlphabet() const &
Definition: LG.h:228
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
Noncontracting grammar in Chomsky hierarchy or type 1 in Chomsky hierarchy. Generates context sensiti...
Definition: NonContractingGrammar.h:64
const ext::set< SymbolType > & getTerminalAlphabet() const &
Definition: NonContractingGrammar.h:210
const ext::set< SymbolType > & getNonterminalAlphabet() const &
Definition: NonContractingGrammar.h:172
const ext::map< ext::vector< SymbolType >, ext::set< ext::vector< SymbolType > > > & getRules() const &
Definition: NonContractingGrammar.h:360
const SymbolType & getInitialSymbol() const &
Definition: NonContractingGrammar.h:143
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
Unrestricted grammar. Type 0 in Chomsky hierarchy. Generates recursively enumerable languages.
Definition: UnrestrictedGrammar.h:64
const ext::map< ext::vector< SymbolType >, ext::set< ext::vector< SymbolType > > > & getRules() const &
Definition: UnrestrictedGrammar.h:330
const SymbolType & getInitialSymbol() const &
Definition: UnrestrictedGrammar.h:144
const ext::set< SymbolType > & getTerminalAlphabet() const &
Definition: UnrestrictedGrammar.h:211
const ext::set< SymbolType > & getNonterminalAlphabet() const &
Definition: UnrestrictedGrammar.h:173
Definition: AutomatonCompare.h:29