summaryrefslogtreecommitdiffhomepage
path: root/grammer.h
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-03-23 22:02:40 +0100
committerRoland Reichwein <mail@reichwein.it>2020-03-23 22:02:40 +0100
commitfce1c18103f208857af477ca47af9aa908bd69b6 (patch)
tree3d0affeca1328825e81be8950f19e956f39ab213 /grammer.h
parent365183e243d164185bca6ad9fa4e0d75664800f4 (diff)
Helper functions for grammer
Diffstat (limited to 'grammer.h')
-rw-r--r--grammer.h19
1 files changed, 15 insertions, 4 deletions
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 <cstdint>
+#include <unordered_set>
#include <utility>
+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<std::string, std::set<std::string>> ReverseBNF; // possible parent types of a given type; unused now: remove?
std::unordered_map<std::string, std::set<std::string>> reversedFirst; // possible parent types of first childs of a given type
@@ -67,10 +69,19 @@ private:
void AddPath(const std::vector<std::string>& path, index_t current_index);
bool FillTree();
+ // top-down algorithm
+ std::unordered_map<std::string, size_t> m_min; // cache
+ size_t minimumSymbolsNeeded(std::string symbol);
+ size_t minimumSymbolsNeeded(std::vector<std::string> symbol_list);
+ bool match(std::string symbol, size_t begin, size_t end);
+ bool match(std::vector<std::string> symbol_list, size_t begin, size_t end);
+
public:
- Compiler(BNF& bnf, const std::string& Top);
- std::pair<index_t, std::vector<TreeNode>> compile(std::vector<Token> Tokens);
+ Compiler(BNF& bnf, const std::string& top);
+ std::pair<index_t, std::vector<TreeNode>> compile(std::vector<Token> p_tokens);
void DumpTree();
+
+ friend class ::GrammerTest;
};
bool ChildIdIsToken(int32_t child_id);