-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.hpp
203 lines (190 loc) · 5.38 KB
/
parser.hpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#ifndef PARSER_HPP
enum class NodeType {
Nonterminal, Terminal
};
class Node {
public:
NodeType type;
string val; // available when NodeType is `Terminal`
TokenType tt; // available when NodeType is `Terminal`
typedef function<void(Node*)> F_ACT; // parent node, self node
vector<pair<vector<Node*>, F_ACT>> child; // use for CFG
vector<Node*> astchild; // use for AST
string nonterminal_name; // use for debug(print `Nonterminal` name)
bool emptynode; // emptynode for string `""`
class F_ACTs { // basic function for user use
public:
static void remPriTail(Node *nd) { // remove CFG tail
if(nd -> astchild.empty()) return ;
Node *tail = nd -> astchild.back();
nd -> astchild.pop_back();
walka(x, tail -> astchild) {
nd -> astchild.push_back(x);
}
delete tail;
};
static void remPriTail2rd(Node *nd) { // remove CFG last second
if((int) nd -> astchild.size() < 2) return ;
Node *tail = nd -> astchild.back();
nd -> astchild.pop_back();
remPriTail(nd);
nd -> astchild.push_back(tail);
};
};
typedef function<void(Node*, int)> FF_ACT;
FF_ACT act; // maintain self after matched
public:
static string printAST(Node *nd) {
function<string(Node*, int)> dfs = [&] (Node *nd, int dep) {
string res;
for(int i = 1 ; i <= dep ; ++ i) {
res += " ";
}
if(nd -> type == NodeType :: Nonterminal) {
res += "<" + nd -> nonterminal_name + ">\n";
} else {
res += "[" + nd -> val + "]\n";
}
walka(x, nd -> astchild) {
res += dfs(x, dep + 1);
}
return res;
};
string res;
res += "============================= AST ==============================\n";
res += dfs(nd, 0);
res += "================================================================\n";
return res;
}
static void freeASTNode(Node *nd) { // free `astchild`
walka(c, nd -> astchild) {
freeASTNode(c);
}
delete nd;
};
static Node* match(Node *S) {
int ptr = 0;
function<Node*(Node*)> dfs = [&] (Node *sta) {
int ptrmem = ptr;
if(sta -> type == NodeType :: Terminal) {
if(sta -> tt == TokenType :: __reserved) { // reserved for raw `string`
if(sta -> val == "") {
return (new Node(NodeType :: Terminal, "")) -> setEmptyNode(true);
} else if(sta -> val == tokens[ptr].val) {
Node *res = new Node(NodeType :: Terminal, tokens[ptr].val, tokens[ptr].type); // remain lexer's tokentype
++ ptr;
return res;
} else {
return (Node *)NULL;
}
} else {
if(sta -> tt == tokens[ptr].type) {
Node *res = new Node(NodeType :: Terminal, tokens[ptr].val, tokens[ptr].type);
++ ptr;
return res;
} else {
return (Node *)NULL;
}
}
} else if(sta -> type == NodeType :: Nonterminal) {
int ruleidx = 0;
for(auto &c: sta -> child) {
Node *res = new Node(NodeType :: Nonterminal);
res -> nonterminal_name = sta -> nonterminal_name;
for(auto ele: c.first) {
Node *tmp = dfs(ele);
if(tmp == NULL) {
goto nxt;
} else if(tmp -> emptynode == false) {
res -> astchild.push_back(tmp);
} else {
Node :: freeASTNode(tmp);
}
}
if(c.second) { // if has declared action function, then use it
c.second(res);
} else if(res -> astchild.empty()) {
res -> setEmptyNode(true);
}
if(sta -> act) {
sta -> act(res, ruleidx);
}
return res;
nxt: ptr = ptrmem;
++ ruleidx;
Node :: freeASTNode(res);
}
}
ptr = ptrmem;
return (Node *)NULL;
};
return dfs(S);
}
public:
Node() {
type = NodeType :: Nonterminal;
val = "";
tt = TokenType :: __reserved;
child.clear(), astchild.clear();
emptynode = false;
act = NULL;
}
Node(NodeType type, string val, TokenType tt) : type(type), val(val), tt(tt) { emptynode = false; }
Node(NodeType type, string val) : type(type), val(val) { tt = TokenType :: __reserved; emptynode = false; }
Node(NodeType type) : type(type) { tt = TokenType :: __reserved; emptynode = false; }
Node* setTokenType(TokenType tt) {
this -> tt = tt;
return this;
}
Node* setNodeType(NodeType type) {
this -> type = type;
return this;
}
Node* setEmptyNode(bool flag) {
this -> emptynode = flag;
return this;
}
Node* setAct(FF_ACT act) {
this -> act = act;
return this;
}
Node* add(vector<Node*> args, F_ACT act = NULL) {
child.push_back(make_pair(args, act));
return this;
}
};
class Parser {
private:
Node *nonterminal_nodes_pool;
vector<Node*> terminal_nodes_pool;
Node *astroot;
public:
Node* match(Node *S) {
astroot = Node :: match(S);
return astroot;
}
Node* getASTroot() {
return astroot;
}
Node* getTerminalNode(string val) {
terminal_nodes_pool.push_back(new Node(NodeType :: Terminal, val));
return terminal_nodes_pool.back();
}
Node* getNonterminalNode(int idx) {
return &nonterminal_nodes_pool[idx];
}
~Parser() {
// free memory
Node :: freeASTNode(astroot);
delete [] nonterminal_nodes_pool;
walka(x, terminal_nodes_pool) delete x;
}
Parser() {
nonterminal_nodes_pool = new Node[__Nonterminal_Count];
for(int i = 0 ; i < __Nonterminal_Count ; ++ i) {
nonterminal_nodes_pool[i].nonterminal_name = string(symbols[i]);
}
}
};
#endif
#define PARSER_HPP