Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
NonterminalUnitRuleCycle.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <alib/set>
9#include <alib/deque>
10
12
13#include <grammar/RawRules.h>
14
15namespace grammar {
16
17namespace properties {
18
23public:
36 template < class T, class NonterminalSymbolType >
37 static ext::set < NonterminalSymbolType > getNonterminalUnitRuleCycle ( const T& grammar, const NonterminalSymbolType& nonterminal);
38};
39
40template<class T, class NonterminalSymbolType >
42 if(grammar.getNonterminalAlphabet().count(nonterminal) == 0) {
43 throw exception::CommonException("Nonterminal symbol \"" + ext::to_string ( nonterminal ) + "\" is not present in grammar.");
44 }
45
46 auto rawRules = grammar::RawRules::getRawRules ( grammar );
47
49 Ni.push_back(ext::set<NonterminalSymbolType>{nonterminal});
50
51 int i = 0;
52
53 do {
54 i += 1;
55
56 Ni.push_back(Ni.at(i-1));
57 for ( const auto & rule : rawRules ) {
58 const NonterminalSymbolType& lhs = rule.first;
59
60 for(const auto& rhs : rule.second) {
61 if(Ni.at(i-1).count(lhs) && rhs.size() == 1 && rhs.front ( ).template is < NonterminalSymbolType > ( ) && grammar.getNonterminalAlphabet().count(rhs.front().template get < NonterminalSymbolType > ( ) )) {
62 Ni.at(i).insert(rhs.front().template get < NonterminalSymbolType > ( ) );
63 }
64 }
65 }
66
67 } while ( Ni.at ( i ) != Ni.at ( i - 1 ) );
68
69 return Ni.at(i);
70}
71
72} /* namespace properties */
73
74} /* namespace grammar */
75
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
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
static ext::map< NonterminalSymbolType, ext::set< ext::vector< ext::variant< TerminalSymbolType, NonterminalSymbolType > > > > getRawRules(const LG< TerminalSymbolType, NonterminalSymbolType > &grammar)
Definition: RawRules.h:92
Definition: NonterminalUnitRuleCycle.h:22
static ext::set< NonterminalSymbolType > getNonterminalUnitRuleCycle(const T &grammar, const NonterminalSymbolType &nonterminal)
Definition: NonterminalUnitRuleCycle.h:41
int i
Definition: AllEpsilonClosure.h:118
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131
Definition: ToAutomaton.h:24