-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.js
355 lines (347 loc) · 9.82 KB
/
parser.js
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//基于LL(k)文法的语法分析器,生成一课Ast树
function AstNode(type, params)
{
var node = {};
node.type = type;
node.child = {};
for (item in params) {
node.child[item] = params[item];
}
return node;
}
function Empty(){
return AstNode('Empty');
}
//1.为了避免文法定义等出现问题而出现无限递归等情况,可以考虑规定递归层最多200层
//2.没有使用文法分析表,时间问题,不足以我完成这项工作
//3.为了避免一直出现this.method这样的调用,而且为了和终结符保持一致,程序的非终结符函数调用也用了tryMatch,但参数是字符串
var ERR = 'ERR';
var parser = {
tokens:[],
index:0,
try:false,
Error:function(){
if (this.try && this.try < 200) return false;
//可能会嵌套探测,探测过程中如果出现错误是不会当作Error的
console.error("Unexpect token: " + this.tokens[this.index-1].text + ' ' + (this.index-1));
process.exit();
},
match:function(tag){
if (this.index < this.tokens.length && this.tokens[this.index].tag.match(tag)) {
var node = AstNode(this.tokens[this.index].tag,{value:this.tokens[this.index].text});
this.index = this.index + 1;
return node;
}
else return false;
},
tryMatch:function(method){
// console.log(method)
if (this[method]==undefined) {
return this.match(method);
}
//如果方法未定义,说明是正则表达式,则说明是终结符
this.try++;
//这个变量保证了不会在探测时直接被err
var node = this[method]();
this.try--;
return node;
},
//match 是用来匹配终结符的
//tryMatch是用来匹配非终结符的
TryAst:function(methods){
var sub = [];
for (var i = 0; i < methods.length; i++) {
var node = this.tryMatch(methods[i]);
if (node == false) {
// console.log(methods[i])
return ERR;
}
else if (node != null) {
sub.push(node);
}
else {
sub.push(Empty())
}
}
return sub;
//尝试匹配后续结点,如果有一个是ERR的话,就
},
parse:function(tokens){
this.tokens = tokens;
var astRoot = this.start();
if (this.index < this.tokens.length) {
console.log(this.index);
console.log(this.tokens[this.index])
console.log("Err: 非法的输入.");
return null;
}
return astRoot;
},
start:function(){
return this.stmt();
},
stmt:function(){
var left;
if ( left = this.tryMatch('line') ) {
if ( this.token >= this.tokens || this.tokens[this.index].tag == 'DEDENT') {
//当到达缩进末尾的时候,应该终止stmt
return AstNode('stmt',{left:left,right:Empty()});
}
var sub = this.TryAst([/CR/,'stmt']);
if (sub == ERR) {
// console.log(this.index)
// console.log(this.tokens[this.index])
return this.Error();
}
//如果只是试探呢?
return AstNode('stmt',{left:left,right:sub[1]});
}
else if (left = this.tryMatch(/CR/) ){
// console.log(this.index +" " + this.tokens.length)
if (this.index < this.tokens.length) {
var sub = this.TryAst(['stmt']);
if (sub == ERR) {
return this.Error();
}
//如果只是试探呢?
return AstNode('stmt',{left:left,right:sub[0]});
}
}
return null;
},
line:function(){
var left;
if (left = this.tryMatch('IDENT')) {
//变量/函数/数组调用
return this.Identifier(left);
}
else if (left = this.tryMatch(/IF/)) {
//if 判定做的比较简单,一定要if开头,加上判断语句、一定要缩进
//其实IF可以做二义性的,就是单行if,不要缩进,比较麻烦就不写了
var sub = this.TryAst(['expr',/colon/,/INDENT/,'stmt','DEDENT',/CR/,'Else']);
if (sub == ERR) {
return this.Error();
}
return AstNode('IF',{left:sub[0],mid:sub[3],right:sub[6]});
}
else if (left = this.tryMatch(/WHILE/)) {
//循环中的while和if很像,比较方便
var sub = this.TryAst(['expr',/colon/,/INDENT/,'stmt','DEDENT']);
if (sub == ERR) {
return this.Error();
}
return AstNode('WHILE',{left:sub[0],right:sub[3]});
}
else if (left = this.tryMatch(/FOR/)) {
//For循环的expr一定是个数组
var sub = this.TryAst([/IDENT/,/IN/,'expr',/colon/,/INDENT/,'stmt','DEDENT']);
if (sub == ERR) {
return this.Error();
}
return AstNode('FOR',{left:sub[0],mid:sub[2],right:sub[5]});
}
else if (left = this.tryMatch(/DEF/)) {
//函数定义
var sub = this.TryAst([/IDENT/,/lbracket/,'para_list',/rbracket/,/colon/,/INDENT/,'stmt','DEDENT']);
if (sub == ERR) {
return this.Error();
}
return AstNode('FUNCDEF',{left:sub[0],para_list:sub[2],funcRoot:sub[6]});
}
else if (left = this.tryMatch(/return/)) {
//函数定义
var sub = this.TryAst(['expr']);
if (sub == ERR) {
return this.Error();
}
return AstNode('return',{left:sub[0]});
}
return null;
},
/* PxFunc和OpFunc用来避免重复的函数体 */
OpFunc:function(pattern,tryList){
var left;
if (left = this.tryMatch(pattern)) {
var sub = this.TryAst(tryList);
if (sub == ERR) {
return this.Error();
}
return AstNode('OpNode',{left:left,mid:sub[0],right:sub[1]});
}
return null;
},
PxFunc:function(pattern,tryList){
var left;
if (left = this.tryMatch(pattern)) {
var sub = this.TryAst([tryList]);
if (sub == ERR) {
return this.Error();
}
return AstNode('PxNode',{left:left,right:sub[0]});
}
return false;
},
/* done */
expr:function(){
return this.PxFunc('PVExpr',['ORExpr']);
},
ORExpr:function(){ return this.OpFunc(/or/,['PVExpr','ORExpr']); },
//与运算
PVExpr:function(){ return this.PxFunc('PWExpr',['ANDExpr']); },
ANDExpr:function(){ return this.OpFunc(/and/,['PWExpr','ANDExpr']); },
//或运算
PWExpr:function(){ return this.PxFunc('PXExpr',['RELExpr']); },
RELExpr:function(){ return this.OpFunc(/EQ|NEQ|LE|GE|LT|GT/,['PXExpr','RELExpr']); },
//逻辑运算
PXExpr:function(){ return this.PxFunc('PYExpr',['LOExpr']); },
LOExpr:function(){ return this.OpFunc(/plus|minus/,['PYExpr','LOExpr']); },
//低级运算(Low Operation)
PYExpr:function(){ return this.PxFunc('Meta',['HOExpr']); },
HOExpr:function(){ return this.OpFunc(/mul|div|exp|mod/,['Meta','HOExpr']); },
//或运算
Meta:function(){
var left;
if (left = this.tryMatch('term'))
return left;
else if (left = this.tryMatch(/IDENT/)) {
return this.Identifier(left);
}
else if (left = this.tryMatch(/LArray/)) {
//数组
var sub = this.TryAst(['expr_list',/RArray/]);
if (sub == ERR) {
return this.Error();
}
return AstNode('Array',{left:sub[0]});
}
// else if (left = tryMatch(/{/)) {
// //集合
// var sub = this.TryAst(['expr',/,/,'expr_list']);
// if (sub == ERR) {
// return this.Error();
// }
// return AstNode('SET',{left:sub[0],right:sub[2]});
// }
else if (left = this.tryMatch(/lbracket/)) {
//括号
var sub = this.TryAst(['expr',/rbracket/]);
if (sub == ERR) {
return this.Error();
}
// return AstNode('Bracket',{value:sub[0]});
return sub[0];//这里应该直接返回expr的值就好了
}
else if (left = this.tryMatch('-')) {
//负数
var sub = this.TryAst(['FExpr']);
if (sub == ERR) {
return this.Error();
}
return AstNode('UMINUS',{right:sub[0]});
}
return false;
},
term:function(){
//可能可以并到expr里 FEXP
return this.match(/NUMBER|STRING|NONE|BOOLEAN/);
},
Identifier:function(left){
if (this.tryMatch(/Assaign/)) {
var sub = this.TryAst(['expr']);
if (sub == ERR) {
return this.Error();
}
return AstNode('=',{left:left,right:sub[0]});
}
else if (this.tryMatch(/lbracket/)) {
var sub = this.TryAst(['expr_list',/rbracket/]);
if (sub == ERR) {
return this.Error();
}
return AstNode('FUNCCALL',{left:left,right:sub[0]});
}
else if (this.tryMatch(/LArray/)) {
//数组调用
var sub = this.TryAst(['expr',/RArray/]);
if (sub == ERR) {
return this.Error();
}
return AstNode('ArrayCall',{left:left,right:sub[0]});
}
return left;
},
DEDENT:function(){
if (this.index < this.tokens.length) {
return this.match(/DEDENT/);
}
else if (this.index >= this.tokens.length) {
return null;
}
return false;
},
Else:function(){
//单独提取else是因为if可以作为line的开头,而else是不可以的
var left;
if (left = this.tryMatch(/ELIF/)) {
var sub = this.TryAst(['expr',/colon/,/INDENT/,'stmt','DEDENT',/CR/,'Else']);
if (sub == ERR) {
return this.Error();
}
console.log(this.tokens[this.index])
//这个直接用IF做结点名字就可以了
return AstNode('IF',{left:sub[0],mid:sub[3],right:sub[6]});
}
else if (left = this.tryMatch(/ELSE/)) {
var sub = this.TryAst([/colon/,/INDENT/,'stmt','DEDENT']);
if (sub == ERR) {
return this.Error();
}
return AstNode('ELSE',{left:sub[2]});
}
return null;
},
para_list:function(){
var left;
if (left = this.tryMatch(/IDENT/)) {
if (this.tryMatch(/comma/)) {
var sub = this.TryAst(['para_list']);
if (sub == ERR) {
return this.Error();
}
return AstNode('para_list',{left:left,right:sub[0]});
}
return AstNode('para_list',{left:left,right:Empty()});
}
return null;
},
expr_list:function(){
var left;
if (left = this.tryMatch('expr')) {
if (this.tryMatch(/comma/)) {
var sub = this.TryAst(['expr_list']);
if (sub == ERR) {
return this.Error();
}
return AstNode('expr_list',{left:left,right:sub[0]});
}
return AstNode('expr_list',{left:left,right:Empty()});
}
return null;
}
}
//中序遍历
var traversal = function(astRoot){
if (!astRoot || astRoot.type == 'Empty') {
// console.log(astRoot);
return;
}
var output = 'Type: ' + astRoot.type + ' | Son: ';
for (item in astRoot.child) {
var type = astRoot.child[item] == undefined ? undefined : astRoot.child[item].type;
if(item != 'value')output += '{' + item + ': ' + type + '} ';
}
console.log(output);
for (item in astRoot.child) {
if(item != 'value')traversal(astRoot.child[item]);
}
}