Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ToGrammarRightRGDerivation.h
Go to the documentation of this file.
1
6#pragma once
7
9#include <regexp/RegExp.h>
12
13#include <ext/hexavigesimal>
14
15#include <alib/set>
16#include <alib/deque>
17#include <alib/vector>
18
20
24
25namespace regexp {
26
27namespace convert {
28
34public:
45 template < class T, class SymbolType = typename regexp::SymbolTypeOfRegexp < T > >
47
48};
49
50template < class T, class SymbolType >
52 // 1.
54
55 // 2., 3.
56 unsigned nonterminalId = 0;
57 ext::map < T, unsigned > nonterminalMap;
58
59 unsigned ntV = common::createUnique ( nonterminalId ++, regexp.getAlphabet ( ) );
60 nonterminalMap.insert ( std::make_pair ( V, ntV ) );
61
63 grammar.setTerminalAlphabet ( regexp.getAlphabet ( ) );
64
66
67 Ni.push_back ( V );
68
69 while(! Ni.empty()) {
70 T r = std::move ( Ni.back ( ) );
71 Ni.pop_back ( );
72
73 for(const auto & a : regexp.getAlphabet()) {
76
77 // this will also add \emptyset as a regexp (and as FA state)
78 if ( ! nonterminalMap.contains ( derived ) ) { // if this state has already been found, do not add
79 Ni.push_back(derived);
80 unsigned nt = common::createUnique ( nonterminalId ++, grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ) );
81 grammar.addNonterminalSymbol ( nt );
82 nonterminalMap.insert ( derived, nt );
83 }
84
86 grammar.addRule(nonterminalMap.at(r), a);
87
88 grammar.addRule(nonterminalMap.at(r), ext::make_pair(a, nonterminalMap.at(derived)));
89 }
90 }
91
93 grammar.setGeneratesEpsilon(true); // okay, because of this feature we do not have to bother with extending the grammar with new rules and nonterminals. YAY!
94
95 return grammar;
96}
97
98} /* namespace convert */
99
100} /* namespace regexp */
101
Class extending the deque class from the standard library. Original reason is to allow printing of th...
Definition: deque.hpp:44
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
std::pair< iterator, bool > insert(const T &key, const R &value)
Insert variant with explicit key and value parameters.
Definition: map.hpp:118
Right regular grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular language...
Definition: RightRG.h:70
Definition: ToGrammarRightRGDerivation.h:33
static grammar::RightRG< SymbolType, unsigned > convert(const T &regexp)
Definition: ToGrammarRightRGDerivation.h:51
static bool languageContainsEpsilon(const regexp::FormalRegExpElement< SymbolType > &regexp)
Definition: RegExpEpsilon.h:91
static regexp::UnboundedRegExp< SymbolType > optimize(const regexp::UnboundedRegExp< SymbolType > &regexp)
static regexp::FormalRegExp< SymbolType > derivation(const regexp::FormalRegExp< SymbolType > &regexp, const string::LinearString< SymbolType > &string)
Definition: RegExpDerivation.h:110
return grammar
Definition: ToGrammarLeftRG.h:99
T createUnique(T object, const Alphabets &... alphabets)
Definition: createUnique.hpp:46
Definition: converterCommon.hpp:8
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
Definition: ToAutomaton.h:24
Definition: ToAutomaton.h:15