Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ProductiveNonterminals.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <ext/algorithm>
9
10#include <alib/set>
11#include <alib/deque>
12
13#include <grammar/Grammar.h>
14
15#include <grammar/RawRules.h>
16
17namespace grammar {
18
19namespace properties {
20
25public:
26 /*
27 * Retrieves set of nonterminals N = { A : A => T* }
28 *
29 * \tparam T the type of the tested grammar
30 * \tparam TerminalSymbolType the type of terminal symbols in the tested grammar
31 * \tparam NonterminalSymbolType the type of nonterminal symbols in the tested grammar
32 *
33 * \param grammar the tested grammar
34 *
35 * \returns set of nonterminals that can be rewriten to a sentence
36 */
37 template<class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > >
39};
40
41template < class T, class TerminalSymbolType, class NonterminalSymbolType >
43 auto rawRules = grammar::RawRules::getRawRules ( grammar );
44
45 // 1.
47 Ni.push_back ( ext::set < NonterminalSymbolType > ( ) );
48
49 int i = 0;
50 auto testCallback = [ & ]( const ext::variant < TerminalSymbolType, NonterminalSymbolType > & symbol ) -> bool {
51 return ( symbol.template is < NonterminalSymbolType > ( ) && Ni.at( i - 1 ) . count ( symbol.template get < NonterminalSymbolType > ( ) ) )
52 || ( symbol.template is < TerminalSymbolType > ( ) && grammar.getTerminalAlphabet( ). count ( symbol.template get < TerminalSymbolType > ( ) ) );
53 };
54
55 // 2.
56 do {
57 i = i + 1;
58
59 Ni.push_back ( Ni.at( i - 1 ) );
60
61 for ( const auto & rule : rawRules ) {
62 for ( const auto & rhs : rule.second ) {
63 if ( std::all_of ( rhs.begin ( ), rhs.end ( ), testCallback ) )
64 Ni.at ( i ).insert ( rule.first );
65 }
66 }
67
68 } while ( Ni.at ( i ) != Ni.at ( i - 1 ) );
69
70 // 3.
71 return Ni.at ( i );
72}
73
74} /* namespace properties */
75
76} /* namespace grammar */
77
Class extending the deque class from the standard library. Original reason is to allow printing of th...
Definition: deque.hpp:44
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 ext::map< NonterminalSymbolType, ext::set< ext::vector< ext::variant< TerminalSymbolType, NonterminalSymbolType > > > > getRawRules(const LG< TerminalSymbolType, NonterminalSymbolType > &grammar)
Definition: RawRules.h:92
Definition: ProductiveNonterminals.h:24
static ext::set< NonterminalSymbolType > getProductiveNonterminals(const T &grammar)
Definition: ProductiveNonterminals.h:42
int i
Definition: AllEpsilonClosure.h:118
all_of(T &&...) -> all_of< T... >
Definition: ToAutomaton.h:24