15 #ifndef TASK_GRAMMAR_NONTERMINALHIERARCHY 16 #define TASK_GRAMMAR_NONTERMINALHIERARCHY 36 unordered_set<std::string> h_;
38 unordered_set<std::string> nt_;
48 h_.insert ( identityrule );
67 unordered_set<std::string> haux = h_;
68 unordered_set<std::string> nt = nt_;
70 if ( !nt.size() )
return true;
74 previoussize = haux.size();
75 LINFO (
"round " << ++count <<
": haux.size=" << previoussize );
76 ntlist += identify_highest_nt ( haux, nt );
78 LERROR (
"Incorrect grammar? S is expected to have the highest hierarchy!" );
79 boost::algorithm::trim ( ntlist );
83 }
while ( haux.size() < previoussize );
84 boost::algorithm::trim ( ntlist );
86 LINFO (
"Conflict!" );
87 LINFO (
"Partial list of ordered non-terminals:" << ntlist );
88 LINFO (
"Rules containing a cycle:" );
89 for ( unordered_set<std::string>::iterator itx = haux.begin();
90 itx != haux.end(); ++itx )
91 LINFO (
"===" << *itx );
94 ntlist =
"S " + ntlist;
103 void populate_nt ( unordered_set<std::string>& nt ) {
104 for ( unordered_set<std::string>::iterator itx = h_.begin(); itx != h_.end();
106 std::vector<std::string> aux;
107 boost::algorithm::split ( aux, *itx, boost::algorithm::is_any_of (
" " ) );
109 nt.insert ( aux[0] );
110 nt.insert ( aux[1] );
112 LINFO (
"Number of non-terminals=" << nt.size() );
119 std::string identify_highest_nt ( unordered_set<std::string>& h,
120 unordered_set<std::string>& nt ) {
121 std::map<std::string, uint> ntorder;
123 for ( std::unordered_set<std::string>::iterator itx = nt.begin();
124 itx != nt.end(); ++itx ) {
126 LINFO (
"nt:" << *itx );
128 for ( std::unordered_set<std::string>::iterator itx = h.begin();
129 itx != h.end(); ++itx ) {
130 std::vector<std::string> aux;
131 boost::algorithm::split ( aux, *itx, boost::algorithm::is_any_of (
" " ) );
135 LINFO (
"Extract highest nt" );
136 std::string ntstring;
137 std::unordered_set<std::string> nttodelete;
138 for ( std::map<std::string, uint>::iterator itx = ntorder.begin();
139 itx != ntorder.end(); ++itx ) {
140 if ( !itx->second ) {
141 if ( itx->first !=
"S" ) {
142 LINFO (
"Found " << itx->first );
143 ntstring +=
" " + itx->first;
145 LINFO (
"Found category S" );
148 nt.erase ( itx->first );
149 nttodelete.insert ( itx->first );
152 unordered_set<std::string> nttodelete2;
153 for ( unordered_set<std::string>::iterator itx = h.begin(); itx != h.end();
155 std::vector<std::string> aux;
156 boost::algorithm::split ( aux, *itx, boost::algorithm::is_any_of (
" " ) );
157 if ( nttodelete.find ( aux[0] ) != nttodelete.end() ) {
158 nttodelete2.insert ( *itx );
161 for ( unordered_set<std::string>::iterator itx = nttodelete2.begin();
162 itx != nttodelete2.end(); ++itx ) {
165 LINFO (
"ntstring=" << ntstring <<
", HSIZE=" << h.size() <<
"NTSIZE=" <<
bool operator()(std::string &ntlist)
Determines hierarchical list of non-terminals and flags whether there has been a cycle or not Example...
NonTerminalHierarchy()
Constructor.
void insertIdentityRule(const std::string &identityrule)
Method to store identity rules, i.e. S -> X X , etc.
This is a functor with additional methods to include relevant rules (i.e. identify SCFG rules...
void find_and_replace(std::string &haystack, const std::string &needle, const std::string &replace)
void insertLHS(const std::string &nt)
void getFilteredNonTerminal(std::string &word)
Return the filtered non-terminal name. For example, for the rule Z 3_XT2_5 XT2, getFilteredNonTermina...