From 0950c449066770292beae220ba4289ce124b4680 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 15 Feb 2020 15:42:20 +0100 Subject: Bugfixing --- grammer.cpp | 22 ++++++++++------------ test-lexer.cpp | 5 +++-- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/grammer.cpp b/grammer.cpp index bbaaa79..55e31c8 100644 --- a/grammer.cpp +++ b/grammer.cpp @@ -77,7 +77,7 @@ void Compiler::DumpTree() std::cout << "--- Nodes ------------------------------------" << std::endl; std::cout << "root_node_id=" << root_node_id << std::endl; for (const auto& node : nodes) { - std::cout << node.type << "(" << node.node_id << "): "; + std::cout << node.type << "(" << node.node_id << "):"; for (const auto& child : node.child_ids) { std::cout << " " << child; } @@ -382,26 +382,24 @@ index_t Compiler::AddNode(const std::string& child_type, index_t parent_index) index_t index = nodes.size(); parent.child_ids.push_back(index); - index_t variant; + index_t variant{}; std::deque> alternatives; - const auto& lists { bnf[parent.type] }; + const auto& variants { bnf[child_type] }; bool found{false}; - for (int i = 0; i < lists.size(); i++) { // variants - if (lists[i].size() > 0 && lists[i][0] == child_type) { - if (!found) { // use first match - variant = i; - found = true; - } else { // defer all others - alternatives.emplace_back(child_type, i); - } + for (int i = 0; i < variants.size(); i++) { // variants + if (!found) { // use first match + variant = i; + found = true; + } else { // defer all others + alternatives.emplace_back(child_type, i); } } nodes.emplace_back(TreeNode{parent_index, index, child_type, variant, alternatives, std::vector{}}); //root stays, tokens_used stays - std::cout << "AddNode(): " << index << std::endl; + std::cout << "AddNode(): " << parent.type << "->" << child_type << ": "<< index << std::endl; DumpTree(); return index; diff --git a/test-lexer.cpp b/test-lexer.cpp index 6733081..0449af9 100644 --- a/test-lexer.cpp +++ b/test-lexer.cpp @@ -61,7 +61,7 @@ TEST_F(Test, BNF) { // implicit? //std::set Terminals{"identifier", "=", ";"}; - std::string Code{"a = bc ; c = 123 ; esd = Ff ; 1 = XYZ"}; + std::string Code{"a = bc ; c = 123 ; esd = Ff ; 1 = XYZ ;"}; std::vector tokens_reference{ {"identifier", "a", { 1, 1} }, {"preprocessing-op-or-punc", "=", { 1, 3}}, @@ -77,7 +77,8 @@ TEST_F(Test, BNF) { {"preprocessing-op-or-punc", ";", { 1, 29}}, {"pp-number", "1", { 1, 31}}, {"preprocessing-op-or-punc", "=", { 1, 33}}, - {"identifier", "XYZ", { 1, 34}}, + {"identifier", "XYZ", { 1, 35}}, + {"preprocessing-op-or-punc", ";", { 1, 38}}, }; Lex::Lexer lexer(LexBNF, LexTop); -- cgit v1.2.3