65 for (
auto pair: kindToAttrsMap)
67 for (
auto attri: pair.second)
69 nodeSet.insert(attri);
74 for(
auto prod: symProdsPair.second)
79 tempP.insert(tempP.begin(), symProdsPair.first);
82 for (
auto filledProd : normalProds)
100 for(
auto itr : rawProductions)
103 for(
auto rule: head.second)
105 if (rule.size() < 3)
continue;
116 if ((X ==
u32_t(-1)) ==
false)
124 std::ostringstream ss;
126 tempStr.append(ss.str());
132 for (
unsigned i = 0; i < long_run.size(); i++)
135 if ( variableAttribute != 0)
137 variableAttributeSet.insert(variableAttribute);
140 if ( variableAttributeSet.size() == 1)
143 tempStr += char(*variableAttributeSet.begin());
146 rule = {first, tempSym};
153 if (long_run.size() ==2)
155 new_grammar[X].insert(temp_p);
160 new_grammar[X].insert(long_run);
163 while (long_run.size() > 2)
167 long_run.erase(long_run.begin());
173 if (RHX ==
u32_t(-1))
180 std::ostringstream ss;
182 tempStr.append(ss.str());
184 for (
unsigned i = 0; i < long_run.size(); i++)
187 if ( variableAttribute != 0)
189 variableAttributeSet.insert(variableAttribute);
192 if ( variableAttributeSet.size() == 1)
195 tempStr += char(*variableAttributeSet.begin());
198 auto it = new_grammar[X].find(prev_rule);
199 new_grammar[X].erase(it);
200 new_grammar[X].insert({first, tempSym});
201 new_grammar[tempSym].insert(long_run);
207 for (
auto new_head : new_grammar)
209 for (
auto prod : new_head.second)
229 while( worklist.
empty() ==
false )
237 if ( currentVariableAttribute == 0 )
239 currentVariableAttribute = symbol.variableAttribute;
243 if ( currentVariableAttribute == 0)
245 normalProds.insert(currentProduction);
250 for (
auto attribute : nodeSet)
255 if ( symbol.variableAttribute == currentVariableAttribute)
257 symbol.attribute = attribute;
258 symbol.variableAttribute = 0;
262 bool continueToFill =
false;
265 if ( symbol.variableAttribute != 0 )
267 continueToFill =
true;
270 if ( continueToFill ==
false)
272 normalProds.insert(fillingProduction);
276 worklist.
push(fillingProduction);
302 for (
auto ebnfProduction : symbolToProductionsPair.second)
306 while (i < ebnfProduction.size())
308 if (grammar->
kindToStr(ebnfProduction[i].kind) ==
"|")
311 tempPro.insert(tempPro.begin(), symbolToProductionsPair.first );
312 productions.insert(tempPro);
318 tempPro.insert(tempPro.begin(), symbolToProductionsPair.first );
319 productions.insert(tempPro);
321 symbolToProductionsPair.second.clear();
322 symbolToProductionsPair.second = productions;
336 for (
auto ebnfProduction : symbolToProductionsPair.second)
339 while (i < ebnfProduction.size())
341 s32_t signGroupStart = -1;
345 assert(i != 1 &&
"sign in grammar associate with no symbol");
348 signGroupStart = i - 1;
356 for (
size_t j = signGroupStart; j < i; j++)
358 groupString.append(grammar->
kindToStr(ebnfProduction[j].kind));
359 groupString.append(
" ");
361 groupString.append(grammar->
kindToStr(ebnfProduction[i].kind));
362 if (newProductions.find(groupString) != newProductions.end())
364 productions.erase(ebnfProduction);
365 ebnfProduction.erase(ebnfProduction.begin() + signGroupStart, ebnfProduction.begin() + i + 1);
366 ebnfProduction.insert(ebnfProduction.begin() + signGroupStart, grammar->
strToSymbol(newProductions[groupString]));
367 productions.insert(ebnfProduction);
369 else if ( (signGroupStart == 1) && (i == ebnfProduction.size() -1))
371 newProductions[groupString] = grammar->
kindToStr(ebnfProduction[0].kind);
372 productions.erase(ebnfProduction);
373 ebnfProduction.erase(ebnfProduction.begin() + signGroupStart, ebnfProduction.begin() + i + 1);
378 tempNonterminal =
"X";
379 std::ostringstream ss;
381 tempNonterminal.append(ss.str());
383 productions.erase(ebnfProduction);
384 ebnfProduction.erase(ebnfProduction.begin() + signGroupStart, ebnfProduction.begin() + i + 1);
385 ebnfProduction.insert(ebnfProduction.begin() + signGroupStart, tempSym);
386 newProductions[groupString] = tempNonterminal;
387 productions.insert(ebnfProduction);
395 symbolToProductionsPair.second = productions;
397 for(
auto rep: newProductions)
404 temp_list = {grammar->
strToSymbol(new_nonterminal)};
405 if (sign ==
'*' || sign ==
'?')
409 strTrans(rep.first, grammar, normalProd);
413 for (
auto &word : normalProd)
417 withoutSign.push_back(word);
420 withoutSign.push_back(grammar->
strToSymbol(rep.second));
424 for (
auto &word : normalProd)
428 withoutSign.push_back(word);
432 temp_list.insert(temp_list.end(), withoutSign.begin(), withoutSign.end());
441 std::regex LHSReg(
"\\s*(.*)");
445 std::regex_search(LHS, matches, LHSReg);
446 LHS = matches.str(1);
448 while ((pos = LHS.find(delimiter)) != std::string::npos)
450 word = LHS.substr(0, pos);
451 LHS.erase(0, pos + delimiter.length());
459 for(
auto symProdPair: grammar)
461 for(
auto prod: symProdPair.second)
465 return symProdPair.first;
476 if (prod.size() == 2)
478 if ((std::find(prod.begin(), prod.end(), grammar->
strToKind(
"epsilon")) != prod.end()))
490 if (prod.size() == 3)
502 for(
auto rule: head.second)
506 long_run.erase(long_run.begin());
void barReplace(CFGrammar *grammar)
void ebnf_bin(CFGrammar *grammar)
Add nonterminal to tranfer long rules to binary rules.
void ebnfSignReplace(char sign, CFGrammar *grammar)
void insertToCFLGrammar(CFGrammar *grammar, GrammarBase::Production &prod)
Based on prod size to add on suitable member field of grammar.
void removeFirstSymbol(CFGrammar *grammar)
CFGrammar * normalize(GrammarBase *generalGrammar)
Binary Normal Form(BNF) normalization with variable attribute expanded.
int ebnfBracketMatch(GrammarBase::Production &prod, int i, CFGrammar *grammar)
CFGrammar * fillAttribute(CFGrammar *grammar, const Map< CFGrammar::Kind, Set< CFGrammar::Attribute >> &kindToAttrsMap)
Expand every variable attribute in rawProductions of grammarbase.
void getFilledProductions(GrammarBase::Production &prod, const NodeSet &nodeSet, CFGrammar *grammar, GrammarBase::Productions &normalProds)
void strTrans(std::string strPro, CFGrammar *grammar, GrammarBase::Production &normalProd)
GrammarBase::Symbol check_head(GrammarBase::SymbolMap< GrammarBase::Symbol, GrammarBase::Productions > &grammar, GrammarBase::Production &rule)
SymbolMap< Symbol, Productions > & getSecondRHSToProds()
const u32_t num_generator()
Productions & getEpsilonProds()
SymbolMap< Symbol, Productions > & getSingleRHSToProds()
SymbolMap< Symbol, Productions > & getFirstRHSToProds()
Map< std::string, Kind > & getNonterminals()
void setKindToAttrsMap(const Map< Kind, Set< Attribute >> &kindToAttrsMap)
void setStartKind(Kind startKind)
void setAttributeKinds(const Set< Kind > &attributeKind)
const Set< Kind > & getAttrSyms() const
void setRawProductions(SymbolMap< Symbol, Productions > &rawProductions)
void setTotalKind(Kind totalKind)
Kind strToKind(std::string str) const
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > SymbolMap
Map< std::string, Kind > & getTerminals()
SymbolMap< Symbol, Productions > & getRawProductions()
Symbol strToSymbol(const std::string str) const
Map< std::string, Kind > & getEBNFSigns()
Symbol insertNonTerminalSymbol(std::string strLit)
SymbolSet< Production > Productions
std::string kindToStr(Kind kind) const
const Map< Kind, Set< Attribute > > & getKindToAttrsMap() const
void setEBNFSigns(Map< std::string, Kind > &EBNFSigns)
void setTerminals(Map< std::string, Kind > &terminals)
std::vector< Symbol > Production
void setNonterminals(Map< std::string, Kind > &nonterminals)
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set