From e1208101b9681c35d94489d72ea52e37cbddb7aa Mon Sep 17 00:00:00 2001 From: Robin Jadoul Date: Mon, 16 Jan 2017 19:04:21 +0100 Subject: [PATCH] Backend change to allow for reduce rules with the same name --- include/Parsodus/backends/cppLR.h | 13 ++++++++++++- templates/c++/lr.h | 7 +++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/include/Parsodus/backends/cppLR.h b/include/Parsodus/backends/cppLR.h index c5ccfe5..bcbb46f 100644 --- a/include/Parsodus/backends/cppLR.h +++ b/include/Parsodus/backends/cppLR.h @@ -59,16 +59,27 @@ namespace backends { topLevel["symbols"] = std::move(symbols); std::vector rules; + std::set rulenames; for (std::size_t i = 0; i < config.grammar.rules.size(); i++) { std::map r; r["index"] = templ::make_string(std::to_string(i)); r["rhs_length"] = templ::make_string(std::to_string(config.grammar.rules[i]->tail.size())); - if (false /* the rule has a name */) + if (false /* the rule has a name */) { r["rname"] = templ::make_string(""); //The name + rulenames.insert(""); //The name + } else { + rulenames.insert(std::to_string(i)); + } rules.push_back(templ::make_map(std::move(r))); } topLevel["rules"] = templ::make_array(std::move(rules)); + std::vector rulenamesT; + for (auto& s : rulenames) { + rulenamesT.emplace_back(templ::make_map({{"rname", templ::make_string(s)}})); + } + topLevel["rulenames"] = templ::make_array(std::move(rulenamesT)); + std::set terminals = config.grammar.terminals; terminals.insert(util::EOF_PLACEHOLDER); std::vector states(table.act.size()); diff --git a/templates/c++/lr.h b/templates/c++/lr.h index 7b221bc..1d7685f 100644 --- a/templates/c++/lr.h +++ b/templates/c++/lr.h @@ -51,10 +51,9 @@ class {{name}} { /** * Apply a reduction (a grammar rule in reverse) */ - {{#rules}} - {{#rname}}virtual Value reduce_{{rname}}(std::deque subparts) = 0;{{/rname}} - {{^rname}}virtual Value reduce_{{index}}(std::deque subparts) = 0;{{/rname}} - {{/rules}} + {{#rulenames}} + virtual Value reduce_{{rname}}(std::deque subparts) = 0; + {{/rulenames}} private: };