Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ToGrammarRightRGGlushkov.h
Go to the documentation of this file.
1
6#pragma once
7
11
12#include <ext/algorithm>
13
15
16#include "../glushkov/GlushkovFollow.h"
17#include "../glushkov/GlushkovIndexate.h"
18#include "../glushkov/GlushkovFirst.h"
19#include "../glushkov/GlushkovLast.h"
20
22
23namespace regexp {
24
25namespace convert {
26
32public:
42 template < class SymbolType >
44
48 template < class SymbolType >
50};
51
52template < class SymbolType >
54 ext::pair < SymbolType, unsigned > S = ext::make_pair ( alphabet::InitialSymbol::instance < SymbolType > ( ), 0 );
56
57 // step 1
58 grammar.setTerminalAlphabet ( regexp.getAlphabet ( ) );
59
61
62 // steps 2, 3, 4
65
66 // \e in q0 check is in step 7
67
68 // step 5
69 for ( const auto & nonterminal : indexedRegExp.getAlphabet ( ) )
70 grammar.addNonterminalSymbol ( nonterminal );
71
72 // step 6
74 grammar.addRule ( S, ext::make_pair ( symbol.getSymbol ( ).first, symbol.getSymbol ( ) ) );
75
76 for ( const ext::pair < SymbolType, unsigned > & x : indexedRegExp.getAlphabet ( ) )
79 const ext::pair < SymbolType, unsigned > & b = f.getSymbol ( );
80
81 grammar.addRule ( a, ext::make_pair ( b.first, b ) );
82 }
83
84 // step 7
85
86 /*
87 * for all rules where ns.m_nonTerminal is on rightSide:
88 * add Rule: leftSide -> symbol.getSymbol( )
89 * unless it already exists
90 */
91 for ( const auto & rule : grammar.getRules ( ) )
92 for ( const auto & rhs : rule.second ) {
93 if ( ! rhs.template is < ext::pair < SymbolType, ext::pair < SymbolType, unsigned > > > ( ) )
94 continue;
95
96 const ext::pair < SymbolType, ext::pair < SymbolType, unsigned > > & rawRhs = rhs.template get < ext::pair < SymbolType, ext::pair < SymbolType, unsigned > > > ( );
97
99 if ( rawRhs.second == symbol.getSymbol ( ) )
100 grammar.addRule ( rule.first, rawRhs.first );
101 }
102
104 grammar.setGeneratesEpsilon ( true );
105
106 return grammar;
107}
108
109template < class SymbolType >
112}
113
114} /* namespace convert */
115
116} /* namespace regexp */
117
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
Right regular grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular language...
Definition: RightRG.h:70
Formal regular expression represents regular expression. It describes regular languages....
Definition: FormalRegExp.h:78
static ext::set< UnboundedRegExpSymbol< SymbolType > > first(const regexp::UnboundedRegExp< SymbolType > &re)
Definition: GlushkovFirst.h:69
static ext::set< UnboundedRegExpSymbol< SymbolType > > follow(const regexp::UnboundedRegExp< SymbolType > &re, const UnboundedRegExpSymbol< SymbolType > &symbol)
Definition: GlushkovFollow.h:77
static regexp::UnboundedRegExp< ext::pair< SymbolType, unsigned > > index(const regexp::UnboundedRegExp< SymbolType > &re)
static ext::set< UnboundedRegExpSymbol< SymbolType > > last(const regexp::UnboundedRegExp< SymbolType > &re)
Definition: GlushkovLast.h:69
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
const ext::set< SymbolType > & getAlphabet() const &
Definition: UnboundedRegExp.h:122
Definition: ToGrammarRightRGGlushkov.h:31
static grammar::RightRG< SymbolType, ext::pair< SymbolType, unsigned > > convert(const regexp::FormalRegExp< SymbolType > &regexp)
Definition: ToGrammarRightRGGlushkov.h:110
static bool languageContainsEpsilon(const regexp::FormalRegExpElement< SymbolType > &regexp)
Definition: RegExpEpsilon.h:91
return grammar
Definition: ToGrammarLeftRG.h:99
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
Definition: converterCommon.hpp:8
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
Definition: ToAutomaton.h:24
Definition: ToAutomaton.h:15