From b526d957502cb87421c7c07a757daefd461a6872 Mon Sep 17 00:00:00 2001 From: Robin Jadoul <robin.jadoul@gmail.com> Date: Fri, 16 Dec 2016 19:02:32 +0100 Subject: [PATCH] Change grammar to use a size_t indexable structure for rules (std::vector) --- include/Parsodus/grammar.h | 2 +- src/inputparser.cpp | 7 +------ src/main.cpp | 11 +++++------ 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/include/Parsodus/grammar.h b/include/Parsodus/grammar.h index c4b194f..532fc2a 100644 --- a/include/Parsodus/grammar.h +++ b/include/Parsodus/grammar.h @@ -32,7 +32,7 @@ namespace pds { std::string start; ///< the starting variable std::set<std::string> variables; ///< the variables std::set<std::string> terminals; ///< the terminals - std::map<std::string, std::set<Rule> > rules; ///< the replacement rules + std::vector<Rule> rules; ///< the replacement rules }; } diff --git a/src/inputparser.cpp b/src/inputparser.cpp index 5d9546a..f96bf50 100644 --- a/src/inputparser.cpp +++ b/src/inputparser.cpp @@ -72,10 +72,6 @@ namespace pds { config.grammar.variables.insert(token.content); std::string current_head = token.content; - std::set<Rule> current_rules; - if(config.grammar.rules.count(current_head)) { - current_rules = config.grammar.rules[current_head]; - } // Parsing rule token = lex.nextToken(); if(token.type != ParsodusLexer::ARROW) @@ -96,14 +92,13 @@ namespace pds { parsing_head = false; case ParsodusLexer::PIPE: rule.tail.shrink_to_fit(); - current_rules.insert(rule); + config.grammar.rules.push_back(rule); rule.tail.clear(); break; default: throw InputParserException("Expecting to find a variable, terminal, pipe or a semicolon, but found '" + token.content + "' instead"); } } - config.grammar.rules[current_head] = current_rules; } else throw InputParserException("Found a variable outside a grammar section: " + token.content); break; diff --git a/src/main.cpp b/src/main.cpp index b0f5958..3157d81 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,14 +35,13 @@ int main(int argc, char** argv) { std::cout << "Terminal: " << a << std::endl; for(auto a: config.grammar.variables) std::cout << "Variable: " << a << std::endl; + std::cout << "Rules: " << std::endl; for(auto a: config.grammar.rules) { - std::cout << "Starting rule with head: " << a.first << std::endl; - for(auto b: a.second) { - std::cout << "\tRule with head: " << b.head << std::endl; - for(auto c: b.tail) { - std::cout << "\t\tFound replacement rule: " << c << std::endl; - } + std::cout << "\t" << a.head << " -> "; + for(auto c: a.tail) { + std::cout << c << " "; } + std::cout << std::endl; } }