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 variables; ///< the variables std::set terminals; ///< the terminals - std::map > rules; ///< the replacement rules + std::vector 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 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; } }