From fce1c18103f208857af477ca47af9aa908bd69b6 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 23 Mar 2020 22:02:40 +0100 Subject: Helper functions for grammer --- grammer.h | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'grammer.h') diff --git a/grammer.h b/grammer.h index ee2897d..e5c2f11 100644 --- a/grammer.h +++ b/grammer.h @@ -4,8 +4,11 @@ #include "minicc.h" #include +#include #include +class GrammerTest; + namespace Gram { // TreeNodes are only intermediate. Terminal symbols don't get of TreeNodes @@ -23,7 +26,6 @@ struct TreeNode { class Compiler { - private: // The result index_t root_node_id{}; @@ -36,7 +38,7 @@ private: index_t tokens_used{0}; // number of tokens used, this is also the next token index to use BNF &bnf; // not const for access via operator[] - const std::string& Top; + const std::string m_top; std::unordered_map> ReverseBNF; // possible parent types of a given type; unused now: remove? std::unordered_map> reversedFirst; // possible parent types of first childs of a given type @@ -67,10 +69,19 @@ private: void AddPath(const std::vector& path, index_t current_index); bool FillTree(); + // top-down algorithm + std::unordered_map m_min; // cache + size_t minimumSymbolsNeeded(std::string symbol); + size_t minimumSymbolsNeeded(std::vector symbol_list); + bool match(std::string symbol, size_t begin, size_t end); + bool match(std::vector symbol_list, size_t begin, size_t end); + public: - Compiler(BNF& bnf, const std::string& Top); - std::pair> compile(std::vector Tokens); + Compiler(BNF& bnf, const std::string& top); + std::pair> compile(std::vector p_tokens); void DumpTree(); + + friend class ::GrammerTest; }; bool ChildIdIsToken(int32_t child_id); -- cgit v1.2.3