From e172ed9f799501c234c8da18cef829244473f1d7 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Thu, 23 Jan 2020 21:53:57 +0100 Subject: Added grammer --- grammer.h | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'grammer.h') diff --git a/grammer.h b/grammer.h index 01ea681..87aa1ad 100644 --- a/grammer.h +++ b/grammer.h @@ -5,18 +5,49 @@ namespace Gram { +struct TreeNode { + index_t parent{}; + std::vector childs; // fill Token by Token + std::vector child_types; // fill always + Token token; +}; + +class Tree { +private: + std::map nodes; // index 0 = non existing; index starting at 1 + index_t node_num{}; + index_t root{}; + index_t last{}; + +public: + void clear(); + bool Valid(const std::string& Top) const; + bool AddFirstNode(const Token& token, const BNF& bnf, const std::map>& reverseBNF); + std::vector getParentTreeNode(const BNF& bnf, const std::map>& reverseBNF); + index_t GetLast(); + void AddRootNode(const TreeNode& newRootNode); + void RemoveRootNode(); + std::vector GetPath(std::string a, std::string b, const BNF& bnf, const std::map>& reverseBNF); + index_t AddNode(const std::string& name, const std::string& child_name, index_t parent_index, const BNF& bnf, const std::map>& reverseBNF); + void AddPath(const std::vector& path, index_t current_index, const BNF& bnf, const std::map>& reverseBNF); + bool Add(const Token& token, const BNF& bnf, const std::map>& reverseBNF); + void Resolve(const BNF& bnf, const std::map>& reverseBNF); + + void Dump(); +}; + class Compiler { private: - const BNF &m_bnf; - const std::string& m_Top; + const BNF &bnf; + const std::string& Top; std::map> ReverseBNF; public: Compiler(const BNF& bnf, const std::string& Top); - ProgramNode compile(std::vector Tokens); + Tree compile(std::vector Tokens); }; } // namespace Gram -- cgit v1.2.3