Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ToGrammarLeftRG.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 lrg.addNonterminalSymbol( nonterminalSymbol );
45 }
46
47 lrg.setTerminalAlphabet( grammar.getTerminalAlphabet( ) );
48 lrg.setGeneratesEpsilon( grammar.getGeneratesEpsilon( ) );
49
50 // 2.
51 for( const auto & rule : grammar.getRules( ) ) {
52 const NonterminalSymbolType & lhs = rule.first;
53
54 for ( const auto & ruleRHS : rule.second ) {
55 if ( ruleRHS.template is < ext::pair < TerminalSymbolType, NonterminalSymbolType > > ( ) ) {
56 const ext::pair < TerminalSymbolType, NonterminalSymbolType > & rhs = ruleRHS.template get < ext::pair < TerminalSymbolType, NonterminalSymbolType > > ( );
57
59 lrg.addRule( rhs.second, std::move ( rightSide ) );
60
61 if( lhs == grammar.getInitialSymbol( ) )
62 lrg.addRule( rhs.second, rhs.first );
63 } else {
64 const TerminalSymbolType & rhs = ruleRHS.template get < TerminalSymbolType > ( );
65
67 lrg.addRule ( lrg.getInitialSymbol ( ), std::move ( rightSide ) );
68
69 if ( lhs == grammar.getInitialSymbol ( ) )
70 lrg.addRule( lrg.getInitialSymbol ( ), rhs );
71 }
72 }
73 }
74
75 return lrg;
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
void setTerminalAlphabet(ext::set< TerminalSymbolType > symbols)
Definition: LeftRG.h:265
const NonterminalSymbolType & getInitialSymbol() const &
Definition: LeftRG.h:169
void setGeneratesEpsilon(bool genEps)
Definition: LeftRG.h:405
bool addNonterminalSymbol(NonterminalSymbolType symbol)
Definition: LeftRG.h:218
bool addRule(NonterminalSymbolType leftHandSide, ext::variant< TerminalSymbolType, ext::pair< NonterminalSymbolType, TerminalSymbolType > > rightHandSide)
Add a new rule of a grammar.
Definition: LeftRG.h:333
Right regular grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular language...
Definition: RightRG.h:70
Definition: ToGrammarLeftRG.h:20
static grammar::LeftRG< TerminalSymbolType, NonterminalSymbolType > convert(const grammar::RightRG< TerminalSymbolType, NonterminalSymbolType > &grammar)
Definition: ToGrammarLeftRG.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