Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
UnproductiveSymbolsRemover.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#include <grammar/RawRules.h>
24#include <grammar/AddRawRule.h>
25
27
28namespace grammar {
29
30namespace simplify {
31
40public:
41 /*
42 * Removes unproductive symbols.
43 *
44 * \tparam T the type of modified grammar
45 * \tparam TerminalSymbolType the type of terminal symbols of the grammar
46 * \tparam NonterminalSymbolType the type of nonterminal symbols of the grammar
47 *
48 * \param grammar the modified grammar
49 *
50 * \return grammar equivalent to @p grammar without unproductive symbols
51 */
52 template < class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > >
53 static T remove( const T & grammar );
54};
55
56template < class T, class TerminalSymbolType, class NonterminalSymbolType >
58 // 1.
60
61 T ret ( grammar.getInitialSymbol ( ) );
62
63 for( const auto & symbol : Nt )
64 ret.addNonterminalSymbol ( symbol );
65
66 for( const auto & symbol : grammar.getTerminalAlphabet( ) )
67 ret.addTerminalSymbol ( symbol );
68
69 auto rawRules = grammar::RawRules::getRawRules ( grammar );
70 const ext::set < TerminalSymbolType > & terminals = ret.getTerminalAlphabet( );
71
72 auto testCallback = [ & ] ( const ext::variant < TerminalSymbolType, NonterminalSymbolType > & symbol ) {
73 return ( symbol.template is < NonterminalSymbolType > ( ) && Nt.count ( symbol.template get < NonterminalSymbolType > ( ) ) )
74 || ( symbol.template is < TerminalSymbolType > ( ) && terminals.count ( symbol.template get < TerminalSymbolType > ( ) ) );
75 };
76
77 for( const auto & rule : rawRules ) {
78 if( Nt.count( rule.first ) ) {
79 for( const auto & rhs : rule.second ) {
80 if( all_of( rhs.begin( ), rhs.end( ), testCallback ) )
81 grammar::AddRawRule::addRawRule ( ret, rule.first, rhs );
82 }
83 }
84 }
85
86
87 /* if( ! G1.getNonTerminalSymbols( ) . count( grammar.getInitialSymbol( ) ) )
88 throw CommonException( "Starting symbol of grammar was marked as unproductive and therefore it was removed." ); */
89
90 // 2.
91 return ret;
92}
93
94} /* namespace simplify */
95
96} /* namespace grammar */
97
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
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< NonterminalSymbolType > getProductiveNonterminals(const T &grammar)
Definition: ProductiveNonterminals.h:42
Definition: UnproductiveSymbolsRemover.h:39
static T remove(const T &grammar)
Definition: UnproductiveSymbolsRemover.h:57
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