Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
Rename.h
Go to the documentation of this file.
1
6#pragma once
7
10
11#include <ext/algorithm>
12
13#include <alib/map>
14#include <alib/vector>
15
16namespace grammar {
17
18namespace simplify {
19
20class Rename {
21public:
32 template < class TerminalSymbolType, class NonterminalSymbolType >
34
38 template < class TerminalSymbolType, class NonterminalSymbolType >
40};
41
42template < class TerminalSymbolType, class NonterminalSymbolType >
44 unsigned counter = 0;
45
47
48 for ( const NonterminalSymbolType & symbol : rrg.getNonterminalAlphabet ( ) )
49 renamingData.insert ( std::make_pair ( symbol, counter++ ) );
50
52 result.setGeneratesEpsilon ( rrg.getGeneratesEpsilon ( ) );
53 result.setTerminalAlphabet ( rrg.getTerminalAlphabet ( ) );
54
55 for ( const NonterminalSymbolType & symbol : rrg.getNonterminalAlphabet ( ) )
56 result.addNonterminalSymbol ( renamingData.at ( symbol ) );
57
58 for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, ext::pair < TerminalSymbolType, NonterminalSymbolType > > > > & rule : rrg.getRules ( ) )
59 for ( const ext::variant < TerminalSymbolType, ext::pair < TerminalSymbolType, NonterminalSymbolType > > & rhs : rule.second )
60 if ( rhs.template is < TerminalSymbolType > ( ) ) {
61 result.addRule ( renamingData.at ( rule.first ), rhs.template get < TerminalSymbolType > ( ) );
62 } else {
63 const ext::pair < TerminalSymbolType, NonterminalSymbolType > & realRhs = rhs.template get < ext::pair < TerminalSymbolType, NonterminalSymbolType > > ( );
64 result.addRule ( renamingData.at ( rule.first ), ext::make_pair ( realRhs.first, renamingData.at ( realRhs.second ) ) );
65 }
66
67 return result;
68}
69
70template < class TerminalSymbolType, class NonterminalSymbolType >
72 unsigned counter = 0;
73
75
76 for ( const NonterminalSymbolType & symbol : lrg.getNonterminalAlphabet ( ) )
77 renamingData.insert ( std::make_pair ( symbol, counter++ ) );
78
80 result.setGeneratesEpsilon ( lrg.getGeneratesEpsilon ( ) );
81 result.setTerminalAlphabet ( lrg.getTerminalAlphabet ( ) );
82
83 for ( const NonterminalSymbolType & symbol : lrg.getNonterminalAlphabet ( ) )
84 result.addNonterminalSymbol ( renamingData.at ( symbol ) );
85
86 for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, TerminalSymbolType > > > > & rule : lrg.getRules ( ) )
87 for ( const ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, TerminalSymbolType > > & rhs : rule.second )
88 if ( rhs.template is < TerminalSymbolType > ( ) ) {
89 result.addRule ( renamingData.at ( rule.first ), rhs.template get < TerminalSymbolType > ( ) );
90 } else {
91 const ext::pair < NonterminalSymbolType, TerminalSymbolType > & realRhs = rhs.template get < ext::pair < NonterminalSymbolType, TerminalSymbolType > > ( );
92 result.addRule ( renamingData.at ( rule.first ), ext::make_pair ( renamingData.at ( realRhs.first ), realRhs.second ) );
93 }
94
95 return result;
96}
97
98} /* namespace simplify */
99
100} /* namespace grammar */
101
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
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
Implementation of the variant class allowing to store any type of those listed in the template parame...
Definition: variant.hpp:98
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
bool getGeneratesEpsilon() const
Definition: LeftRG.h:410
const NonterminalSymbolType & getInitialSymbol() const &
Definition: LeftRG.h:169
const ext::set< NonterminalSymbolType > & getNonterminalAlphabet() const &
Definition: LeftRG.h:198
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
bool getGeneratesEpsilon() const
Definition: RightRG.h:409
Definition: Rename.h:20
static grammar::RightRG< TerminalSymbolType, unsigned > rename(const grammar::RightRG< TerminalSymbolType, NonterminalSymbolType > &rrg)
Definition: Rename.h:43
unsigned counter
Definition: Rename.h:247
ext::map< StateType, unsigned > renamingData
Definition: Rename.h:248
for(const StateType &state :fsm.getStates()) renamingData.insert(std Rename::RenamedAutomaton< T > result(renamingData.at(fsm.getInitialState()))
Definition: Rename.h:253
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
Definition: ToAutomaton.h:24