diff options
-rw-r--r-- | grammer.cpp | 56 |
1 files changed, 39 insertions, 17 deletions
diff --git a/grammer.cpp b/grammer.cpp index 7b26e02..65a4be3 100644 --- a/grammer.cpp +++ b/grammer.cpp @@ -45,19 +45,6 @@ void Compiler::Validate() const } } -void Compiler::DumpTree() -{ - std::cout << "=== Tree =====================================" << std::endl; - std::cout << "root_node_id=" << root_node_id << std::endl; - for (const auto& node : nodes) { - std::cout << node.type << "(" << node.node_id << "): "; - for (const auto& child : node.child_ids) { - std::cout << " " << child; - } - std::cout << std::endl; - } -} - bool Compiler::rootIsStartSymbol() const { return GetTypeOfNode(root_node_id) == Top; @@ -82,6 +69,41 @@ namespace { } // namespace +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 << "): "; + for (const auto& child : node.child_ids) { + std::cout << " " << child; + } + std::cout << std::endl; + } + std::cout << "=== Tree =====================================" << std::endl; + std::deque<std::pair<int32_t, size_t>> todo{std::pair<int32_t, size_t>{static_cast<int32_t>(root_node_id), 0}}; // id, indent + while (!todo.empty()) { + auto [current_index, indent] {todo.front()}; + todo.pop_front(); + + std::cout << std::string(indent, ' '); + if (ChildIdIsToken(current_index)) { + index_t token_id {TokenIdFromChildId(current_index)}; + std::cout << "Token(" << token_id << "): " << tokens[token_id].type << "(" << tokens[token_id].value << ")"; + } else { + std::cout << "Node(" << current_index << "): " << nodes[current_index].type; + + auto child_ids{nodes[current_index].child_ids}; + for (int i = 0; i < child_ids.size(); i++) { + todo.insert(todo.begin() + i, std::pair<int32_t, size_t>{child_ids[i], indent + 1}); + } + } + std::cout << std::endl; + + } + std::cout << "==============================================" << std::endl; +} + bool Compiler::AllTokensUsed() const { return tokens_used == tokens.size(); @@ -102,8 +124,6 @@ std::vector<std::string>& Compiler::getNodeExpectedChilds(index_t node_id) // returns true if all childs are filled, recursively. Else false with to_fill as hint to node to fill bool Compiler::subTreeIsComplete(index_t node_id, index_t& to_fill) { - std::cout << "DEBUG: " << node_id << " " << nodes.size() << std::endl; - DumpTree(); for (const auto& i : nodes[node_id].child_ids) { if (!ChildIdIsToken(i)) { // consider subtrees if (!subTreeIsComplete(i, to_fill)) @@ -184,7 +204,7 @@ bool Compiler::AddRootNode() std::string node_type; index_t node_variant; std::deque<std::pair<std::string, index_t>> alternatives; // only for valid elements from alternatives_set - std::vector<int32_t> child_ids{size_t(1), ChildIdFromTokenId(old_root_node_id)}; + std::vector<int32_t> child_ids{static_cast<int>(old_root_node_id)}; for (const auto& type : alternatives_set) { const auto& variants{bnf[type]}; @@ -227,8 +247,10 @@ void Compiler::RemoveLastNode() root_node_id = node.child_ids[0]; } nodes.pop_back(); - } else + } else { + DumpTree(); throw std::runtime_error("Backtrack not possible: Root not empty"); // ICE + } } else if (node.child_ids.empty()) { // No children -> remove leaf // We have a parent, otherwise we would have taken previous branch TreeNode& parent {nodes[node.parent_node_id]}; |