diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-02-06 21:56:33 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-02-06 21:56:33 +0100 |
commit | 8828bd96989d839a1d34c5cfc625fefdfa80a136 (patch) | |
tree | 7c6478279a9303abaa821814680c8bc527ceca5d | |
parent | f1b23e34bee172e0c0ed8e65bcd7904baef9e857 (diff) |
Fixed class
-rw-r--r-- | grammer.cpp | 23 | ||||
-rw-r--r-- | grammer.h | 27 |
2 files changed, 35 insertions, 15 deletions
diff --git a/grammer.cpp b/grammer.cpp index 4bdf8f4..8f2b42b 100644 --- a/grammer.cpp +++ b/grammer.cpp @@ -22,7 +22,8 @@ bool Compiler::IsRootNode(index_t node_id) const return node.parent_node_id == node.node_id; } -void Compiler::Validate() const { +void Compiler::Validate() const +{ // A program is non empty if (nodes.size() == 0) throw std::runtime_error(""); @@ -50,11 +51,13 @@ void Compiler::DumpTree() } } -bool RootIsStartSymbol() +bool Compiler::RootIsStartSymbol() const { return GetTypeOfNode(root_node_id) == Top; } +namespace { + bool ChildIdIsToken(int32_t child_id) { return child_i < 0; @@ -70,12 +73,14 @@ int32_t ChildIdFromTokenId(index_t token_id) return -1 - int32_t(token_id); } -bool AllTokensUsed() +} // namespace + +bool Compiler::AllTokensUsed() const { return tokens_used == tokens.size(); } -bool Compiler::treeIsComplete() +bool Compiler::treeIsComplete() const { return RootIsStartSymbol() && AllTokensUsed(); } @@ -199,7 +204,7 @@ bool Compiler::AddRootNode() return true; } -void RemoveLastNode() +void Compiler::RemoveLastNode() { TreeNode& node {nodes.back()}; index_t node_id = node.node_id; @@ -225,7 +230,7 @@ void RemoveLastNode() } // Change type of last node according to alternatives -void ChangeNodeType() +void Compiler::ChangeNodeType() { TreeNode& node {nodes.back()}; index_t node_id = node.node_id; @@ -289,7 +294,8 @@ std::map<std::string, std::string> Compiler::traverse(lower, upper) // returns list from lower (excluding) to upper (including) // returns empty list on fail -std::vector<std::string> Compiler::GetPath(std::string upper, std::string lower) { +std::vector<std::string> Compiler::GetPath(std::string upper, std::string lower) +{ std::vector<std::string> result; // traverse bnf from lower to upper @@ -327,7 +333,8 @@ index_t Compiler::AddNode(const std::string& name, const std::string& child_type return index; } -void Compiler::AddPath(const std::vector<std::string>& path, index_t current_index) { +void Compiler::AddPath(const std::vector<std::string>& path, index_t current_index) +{ for (int i = path.size() - 1; i > 0; i--) { std::string child_name = path[i - 1]; current_index = AddNode(path[i], child_name, current_index); @@ -40,22 +40,35 @@ private: std::map<std::string, std::set<std::string>> ReverseBNF; // possible parent types of a given type -public: + // Tree specific void clear(); // Node specific std::string GetTypeOfNode(index_t node_id) const; - + bool IsRootNode(index_t node_id) const; + bool RootIsStartSymbol() const; + bool AllTokensUsed() const; + bool treeIsComplete() const; + std::vector<std::string>& getNodeExpectedChilds(node_id); + bool subTreeIsComplete(index_t node_id, index_t& to_fill); + size_t CommonPrefix(const std::vector<Token>& tokens, const std::vector<std::string>& types); + void AddFirstNode(); + bool AddRootNode(); + void RemoveLastNode(); + void ChangeNodeType(); index_t TrackBack(); - - void Validate(const std::string& Top, const BNF& bnf) const; - - void Dump(); + void Validate() const; + std::map<std::string, std::string> traverse(lower, upper); + std::vector<std::string> GetPath(std::string upper, std::string lower); + index_t AddNode(const std::string& name, const std::string& child_type, index_t parent_index); + void AddPath(const std::vector<std::string>& path, index_t current_index); + bool FillTree(); +public: Compiler(const BNF& bnf, const std::string& Top); - std::pair<index_t, std::vector<TreeNode>> compile(std::vector<Token> Tokens); + void DumpTree(); }; } // namespace Gram |