summaryrefslogtreecommitdiffhomepage
path: root/grammer.h
blob: e5c2f11e55ed396af7e4a0aebc82871f583a4e38 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#pragma once

#include "bnf.h"
#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
// token_id: index into token list
// node_id: index into tree node list
struct TreeNode {
 index_t parent_node_id{}; // root if parent_node_id == node_id
 index_t node_id{};

 std::string type;
 index_t variant; // bnf[type][variant]
 std::deque<std::pair<std::string, index_t>> alternatives; // [type][value] alternatives that we can consider on fail. Discard after parsing!
 std::vector<int32_t> child_ids; // < 0: terminal: token_id; >= 0: non-terminal: node_id; fill token by token
};

class Compiler
{
private:
 // The result
 index_t root_node_id{};
 std::vector<TreeNode> nodes;
 
 // Input
 std::vector<Token> tokens;

 // Helper data
 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 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

 // 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(index_t 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 removeTokensUpTo(index_t token_id);
 void removeTokensUpTo(index_t token_id, index_t node_id);
 void RemoveLastNode();
 void ChangeNodeType();
 void TrackBack();
 void Validate() const;
 std::unordered_map<std::string, std::string> traverse(const std::string& lower, const std::string& upper);
 std::vector<std::string> GetPath(std::string upper, std::string lower);
 index_t AddNode(const std::string& child_type, index_t parent_index);
 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> p_tokens);
 void DumpTree();

 friend class ::GrammerTest;
};

bool ChildIdIsToken(int32_t child_id);
index_t TokenIdFromChildId(int32_t child_id);
int32_t ChildIdFromTokenId(index_t token_id);

} // namespace Gram