Move rule precedence attribution to after reading terminals from lexesis

This commit is contained in:
Robin Jadoul 2017-01-28 15:53:22 +01:00
parent 95db330460
commit c26510bdef
2 changed files with 16 additions and 19 deletions

View File

@ -55,7 +55,22 @@ namespace pds {
LOG(WARNING) << "Terminal '" << term << "' is specified, but never used." << std::endl;
}
cnf.grammar.terminals.insert("error");
// Set precedence of each rule
for(std::shared_ptr<Rule>& rule : cnf.grammar.rules) {
if (rule->precedence.first) continue;
if (rule->tail.size() == 0)
rule->precedence = {false,{0,PrecedenceType::RIGHT}};
std::string rightmostTerm;
for (const std::string& term : rule->tail) {
if (cnf.grammar.terminals.count(term)) {
rightmostTerm = term;
}
}
auto prec = cnf.grammar.precedence.find(rightmostTerm);
if (prec != cnf.grammar.precedence.end())
rule->precedence = {true,prec->second};
else rule->precedence = {false,{0, PrecedenceType::RIGHT}};
}
return cnf;
}

View File

@ -155,24 +155,6 @@ std::unique_ptr<Config> Parser::reduce_0(std::deque<Token> subparts) {
// REMARK: Everything is now put into subparts[0]
// Set precedence of each rule
for(std::shared_ptr<Rule>& rule : subparts[0].value->grammar.rules) {
if (rule->precedence.first) continue;
if (rule->tail.size() == 0)
rule->precedence = {false,{0,PrecedenceType::RIGHT}};
std::string rightmostTerm;
for (const std::string& term : rule->tail) {
if (subparts[0].value->grammar.terminals.count(term)) {
rightmostTerm = term;
}
}
auto prec = subparts[0].value->grammar.precedence.find(rightmostTerm);
if (prec != subparts[0].value->grammar.precedence.end())
rule->precedence = {true,prec->second};
else rule->precedence = {false,{0, PrecedenceType::RIGHT}};
}
// REMARK: No option yet for explicit rule precedence
return std::move(subparts[0].value);
}
std::unique_ptr<Config> Parser::reduce_1(std::deque<Token>) {