summaryrefslogtreecommitdiffhomepage
path: root/grammer.h
diff options
context:
space:
mode:
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);