Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
UnreachableSymbolsRemover.h
Go to the documentation of this file.
1
6#pragma once
7
17
18#include <ext/algorithm>
19
20#include <alib/set>
21
23
24#include <grammar/RawRules.h>
25#include <grammar/AddRawRule.h>
26
27namespace grammar {
28
29namespace simplify {
30
39public:
40 /*
41 * Removes unreachable symbols.
42 *
43 * \tparam T the type of modified grammar
44 * \tparam TerminalSymbolType the type of terminal symbols of the grammar
45 * \tparam NonterminalSymbolType the type of nonterminal symbols of the grammar
46 *
47 * \param grammar the modified grammar
48 *
49 * \return grammar equivalent to @p grammar without unreachable symbols
50 */
51 template < class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > >
52 static T remove( const T & grammar );
53};
54
55template < class T, class TerminalSymbolType, class NonterminalSymbolType >
57 // 1.
59
60 T ret ( grammar.getInitialSymbol ( ) );
61
64
65 set_intersection( Vt.begin( ), Vt.end( ), grammar.getNonterminalAlphabet( ).begin( ), grammar.getNonterminalAlphabet( ).end( ), std::inserter( newNonTerminals, newNonTerminals.begin( ) ) );
66 for( const auto & symbol : newNonTerminals )
67 ret.addNonterminalSymbol( symbol.template get < NonterminalSymbolType > ( ) );
68
69 set_intersection( Vt.begin( ), Vt.end( ), grammar.getTerminalAlphabet( ).begin( ), grammar.getTerminalAlphabet( ).end( ), std::inserter( newTerminals, newTerminals.begin( ) ) );
70 for( const auto & symbol : newTerminals )
71 ret.addTerminalSymbol( symbol.template get < TerminalSymbolType > ( ) );
72
73 auto rawRules = grammar::RawRules::getRawRules ( grammar );
74
75 auto testCallback = [ & ] ( const ext::variant < TerminalSymbolType, NonterminalSymbolType > & symb ) -> bool {
76 return Vt.count( symb );
77 };
78
79 // A->\alpha: if A \in N' and \alpha in V_i*, then A->\alpha in P
80 for( const auto & rule : rawRules ) {
81 if( newNonTerminals.count( rule.first ) ) {
82 for( const auto& rhs : rule.second ) {
83 if( all_of( rhs.begin( ), rhs.end( ), testCallback ) )
84 grammar::AddRawRule::addRawRule ( ret, rule.first, rhs );
85 }
86 }
87 }
88
89 // 2.
90 return ret;
91}
92
93} /* namespace simplify */
94
95} /* namespace grammar */
96
Definition: set.hpp:44
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: set.hpp:99
auto end() &
Inherited behavior of end for non-const instance.
Definition: set.hpp:129
Implementation of the variant class allowing to store any type of those listed in the template parame...
Definition: variant.hpp:98
static bool addRawRule(LG< TerminalSymbolType, NonterminalSymbolType > &grammar, NonterminalSymbolType leftHandSide, ext::vector< ext::variant< TerminalSymbolType, NonterminalSymbolType > > rightHandSide)
Definition: AddRawRule.h:92
static ext::map< NonterminalSymbolType, ext::set< ext::vector< ext::variant< TerminalSymbolType, NonterminalSymbolType > > > > getRawRules(const LG< TerminalSymbolType, NonterminalSymbolType > &grammar)
Definition: RawRules.h:92
static ext::set< ext::variant< TerminalSymbolType, NonterminalSymbolType > > getReachableSymbols(const T &grammar)
Definition: ReachableSymbols.h:42
Definition: UnreachableSymbolsRemover.h:38
static T remove(const T &grammar)
Definition: UnreachableSymbolsRemover.h:56
ext::set< ext::pair< StateType, StateType > > ret(const ext::set< ext::pair< StateType, StateType > > &S, const DeterministicPushdownStoreSymbolType &pdaSymbol, const InputSymbolType &input, const N &nondeterministic)
Definition: RHDPDACommon.h:57
all_of(T &&...) -> all_of< T... >
Definition: ToAutomaton.h:24