#pragma once #include "bnf.h" #include "minicc.h" 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 &bnf; const std::string& Top; std::map> ReverseBNF; public: Compiler(const BNF& bnf, const std::string& Top); Tree compile(std::vector Tokens); }; } // namespace Gram