Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ToGrammarRightRG.h
Go to the documentation of this file.
1
6#pragma once
7
10
12
13namespace grammar {
14
15namespace convert {
16
21public:
32 template < class TerminalSymbolType, class NonterminalSymbolType >
34};
35
36template < class TerminalSymbolType, class NonterminalSymbolType >
38 // 1.
39 NonterminalSymbolType s = common::createUnique( grammar.getInitialSymbol( ), grammar.getNonterminalAlphabet(), grammar.getTerminalAlphabet() );
40
42
43 for(const auto & nonterminalSymbol : grammar.getNonterminalAlphabet() ) {
44 rrg.addNonterminalSymbol( nonterminalSymbol );
45 }
46
47 rrg.setTerminalAlphabet( grammar.getTerminalAlphabet( ) );
48
49 rrg.setGeneratesEpsilon( grammar.getGeneratesEpsilon( ) );
50
51 // 2
52 for( const auto & rule : grammar.getRules( ) ) {
53 const NonterminalSymbolType& lhs = rule.first;
54
55 for ( const auto & ruleRHS : rule.second ) {
56 if ( ruleRHS.template is < ext::pair < NonterminalSymbolType, TerminalSymbolType > > ( ) ) {
57 const ext::pair < NonterminalSymbolType, TerminalSymbolType > & rhs = ruleRHS.template get < ext::pair < NonterminalSymbolType, TerminalSymbolType > > ( );
58
60 rrg.addRule( rhs.first, std::move ( rightSide ) );
61
62 if ( lhs == grammar.getInitialSymbol ( ) )
63 rrg.addRule ( rhs.first, rhs.second );
64 } else {
65 const TerminalSymbolType & rhs = ruleRHS.template get < TerminalSymbolType > ( );
66
68 rrg.addRule ( rrg.getInitialSymbol ( ), std::move ( rightSide ) );
69
70 if ( lhs == grammar.getInitialSymbol ( ) )
71 rrg.addRule ( rrg.getInitialSymbol ( ), rhs );
72 }
73 }
74 }
75 return rrg;
76}
77
78} /* namespace convert */
79
80} /* namespace grammar */
81
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Left regular grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular languages...
Definition: LeftRG.h:70
Right regular grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular language...
Definition: RightRG.h:70
void setGeneratesEpsilon(bool genEps)
Definition: RightRG.h:404
bool addNonterminalSymbol(NonterminalSymbolType symbol)
Definition: RightRG.h:218
bool addRule(NonterminalSymbolType leftHandSide, ext::variant< TerminalSymbolType, ext::pair< TerminalSymbolType, NonterminalSymbolType > > rightHandSide)
Add a new rule of a grammar.
Definition: RightRG.h:333
void setTerminalAlphabet(ext::set< TerminalSymbolType > symbols)
Definition: RightRG.h:265
const NonterminalSymbolType & getInitialSymbol() const &
Definition: RightRG.h:169
Definition: ToGrammarRightRG.h:20
static grammar::RightRG< TerminalSymbolType, NonterminalSymbolType > convert(const grammar::LeftRG< TerminalSymbolType, NonterminalSymbolType > &grammar)
Definition: ToGrammarRightRG.h:37
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