summaryrefslogtreecommitdiffhomepage
path: root/grammer.h
blob: 87aa1ada46d3a5c4c522e2fc0c8a83de702c24a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once

#include "bnf.h"
#include "minicc.h"

namespace Gram {

struct TreeNode {
 index_t parent{};
 std::vector<index_t> childs; // fill Token by Token
 std::vector<std::string> child_types; // fill always
 Token token;
};

class Tree {
private:
 std::map<index_t, TreeNode> 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<std::string, std::set<std::string>>& reverseBNF);
 std::vector<TreeNode> getParentTreeNode(const BNF& bnf, const std::map<std::string, std::set<std::string>>& reverseBNF);
 index_t GetLast();
 void AddRootNode(const TreeNode& newRootNode);
 void RemoveRootNode();
 std::vector<std::string> GetPath(std::string a, std::string b, const BNF& bnf, const std::map<std::string, std::set<std::string>>& reverseBNF);
 index_t AddNode(const std::string& name, const std::string& child_name, index_t parent_index, const BNF& bnf, const std::map<std::string, std::set<std::string>>& reverseBNF);
 void AddPath(const std::vector<std::string>& path, index_t current_index, const BNF& bnf, const std::map<std::string, std::set<std::string>>& reverseBNF);
 bool Add(const Token& token, const BNF& bnf, const std::map<std::string, std::set<std::string>>& reverseBNF);
 void Resolve(const BNF& bnf, const std::map<std::string, std::set<std::string>>& reverseBNF);

 void Dump();
};

class Compiler
{

private:
 const BNF &bnf;
 const std::string& Top;

 std::map<std::string, std::set<std::string>> ReverseBNF;

public:
 Compiler(const BNF& bnf, const std::string& Top);
 Tree compile(std::vector<Token> Tokens);
};

} // namespace Gram