-
Notifications
You must be signed in to change notification settings - Fork 0
/
LR1-parser.coffee
568 lines (412 loc) · 15 KB
/
LR1-parser.coffee
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
if typeof self == 'undefined'
util = require './util.js'
IR = require './IR.js'
BNFParser = require './BNF-parser.js'
BNFGrammar = BNFParser.BNFGrammar
else
util = self.util
IR = self.IR
BNFGrammar = self.BNFGrammar
SyntaxTable = (grammar_content, start_stmt, end_lex) ->
@start_stmt = start_stmt;
@end_lex = end_lex;
@raw_bnf_grammar = new BNFGrammar(grammar_content)
@raw_bnf_grammar.makePlainBNF()
@grammar_dict = new GrammarDict(@raw_bnf_grammar.bnf_grammar_pairs)
@live_grammars = null
@
SyntaxTable::init = ->
@live_grammars = {}
for closure in @start_stmt
@initGrammar2Live closure, @end_lex, 0
@expand 0, null
SyntaxTable::initGrammar2Live = (closure, end_lex, expand_level) ->
firsts_closure = []
for reprs in (@grammar_dict.get closure)['reprs']
if @grammar_dict.get reprs[0]
firsts_closure.push reprs[0]
SyntaxTable.addGrammar2Set closure, reprs, end_lex, @live_grammars, expand_level
firsts_closure
SyntaxTable.addGrammar2Set = (closure, repr, end_lex, grammar_set, expand_level=0) ->
repr = repr.slice()
end_lex = end_lex.slice()
first_lex = repr.shift()
isOneOrMore = false
if BNFGrammar.isOneOrMore first_lex
first_lex = BNFGrammar.removeSpecialMark first_lex
isOneOrMore = true
if first_lex not of grammar_set
grammar_set[first_lex] = []
if isOneOrMore
grammar_set[first_lex].repeat = true
for each_end_lex, i in end_lex
if BNFGrammar.isOneOrMore each_end_lex
end_lex[i] = BNFGrammar.removeSpecialMark each_end_lex
grammar_set[first_lex].push
closure: closure
repr: repr
end_lex: end_lex
expand_level: expand_level
SyntaxTable.mixGrammars = (a, b) ->
ret = {}
for i of a
if i not of ret
ret[i] = []
ret[i] = ret[i].concat a[i]
for i of b
if i not of ret
ret[i] = []
ret[i] = ret[i].concat b[i]
log a, 'mix from a'
log b, 'mix from b'
log ret, 'mixed'
ret
SyntaxTable.cloneGrammar = (a) ->
ret = {}
for closure of a
ret[closure] = []
if a[closure].repeat
ret[closure].repeat = true
for unit in a[closure]
item =
closure: unit['closure']
repr: unit['repr']
end_lex: unit['end_lex']
expand_level: unit['expand_level']
ret[closure].push item
ret
SyntaxTable::moveDot = (dot_lex, next_lex, expand_level) ->
dot_grammars = @live_grammars[dot_lex]
log dot_lex, 'dot-lex -' + dot_lex
if not dot_grammars
@live_grammars = {}
return false
new_live_grammars = {}
ristrict = null
if dot_grammars.repeat
log 'repeat!'
new_live_grammars = SyntaxTable.cloneGrammar @live_grammars
ristrict = [dot_lex]
dot_grammars.sort (a, b) -> b['expand_level'] - a['expand_level']
for each_grammar in dot_grammars
if not each_grammar['repr'].length
if next_lex in each_grammar['end_lex']
return {
expand_level: each_grammar['expand_level'],
closure: each_grammar['closure']
}
else
ristrict and ristrict.push BNFGrammar.removeSpecialMark each_grammar['repr'][0]
SyntaxTable.addGrammar2Set each_grammar['closure'],
each_grammar['repr'],
each_grammar['end_lex'],
new_live_grammars,
each_grammar['expand_level']
@live_grammars = new_live_grammars
if dot_grammars.repeat
for grammar of @live_grammars
if grammar not in ristrict
delete @live_grammars[grammar]
@expand expand_level, ristrict
return false
SyntaxTable::expand = (expand_level, ristrict) ->
expanded_closures = []
last_ec_len = 0
if ristrict
log ristrict, 'ristrict'
while 1
for closure of @live_grammars
if ristrict and closure not in ristrict
continue
if @grammar_dict.get closure
end_lex = []
for x in @live_grammars[closure]
if x['repr'].length
first_lex = x['repr'][0]
first_lex = if BNFGrammar.isOneOrMore first_lex then BNFGrammar.removeSpecialMark first_lex else first_lex
util.uniqueConcat end_lex, @grammar_dict.findFirst first_lex
else
util.uniqueConcat end_lex, @end_lex
util.uniqueConcat end_lex, x['end_lex']
if @live_grammars[closure].repeat
util.uniqueConcat end_lex, @grammar_dict.findFirst closure
closure_id = closure + end_lex.join ''
if closure_id in expanded_closures
end_lex = []
if not end_lex.length
continue
expanded_closures.push closure_id
firsts_closure = @initGrammar2Live closure, end_lex, expand_level
if ristrict
log firsts_closure, 'firsts_closure'
util.uniqueConcat ristrict, firsts_closure
if last_ec_len == expanded_closures.length
break
last_ec_len = expanded_closures.length
null
GrammarDict = (bnf_grammar_pairs) ->
@bnf_grammar_pairs = bnf_grammar_pairs
@dict_map = {}
for line in bnf_grammar_pairs
closure = line[0]
reprs = if line[1] instanceof Array then line[1] else [line[1]]
if closure not of @dict_map
@dict_map[closure] = GrammarDict.initClosure()
for repr in reprs
@dict_map[closure]['reprs'].push repr.split /\s+/
@makeFirstSets()
@
GrammarDict.initClosure = ->
reprs: []
first: []
follows: []
GrammarDict::get = (closure) ->
if BNFGrammar.isOneOrMore closure
closure = BNFGrammar.removeSpecialMark closure
@dict_map[closure]
GrammarDict::makeFirstSets = ->
getFirst = (closure_key, first_set, pushed_closures) ->
closure = @dict_map[closure_key]
pushed_closures.push closure_key
for repr in closure['reprs']
if repr[0] in pushed_closures
continue
if repr[0] of @dict_map
getFirst.call @, repr[0], first_set, pushed_closures
else
util.uniquePush first_set, repr[0]
for closure_key of @dict_map
closure = @dict_map[closure_key]
first_set = closure['first']
getFirst.call @, closure_key, first_set, []
GrammarDict::findFirst = (closures) ->
if closures not instanceof Array
closures = [closures]
ret = []
for closure in closures
if closure not of @dict_map
util.uniquePush ret, closure
else
util.uniqueConcat ret, @dict_map[closure]['first']
ret
GrammarNode = (lex, parent=null, leaves=[]) ->
@parent = null
@leaves = leaves
@lex = lex
@value = null
if parent
@linkParent parent
@
GrammarNode::isName = (lex) ->
lex == @lex
GrammarNode::getValue = ->
@value
GrammarNode::setValue = (val) ->
@value = val
GrammarNode::appendLeaf = (leaf) ->
if not @hasLeaf leaf
@leaves.push leaf
GrammarNode::prependLeaf = (leaf) ->
if not @hasLeaf leaf
@leaves.unshift leaf
GrammarNode::hasLeaf = (leaf) ->
leaf in @leaves
GrammarNode::findLeaf = (lex_name) ->
for leaf in @leaves
if leaf.lex == lex_name
return leaf
return null
GrammarNode::linkParent = (parent, use_prepend=null) ->
@parent = parent
if use_prepend
parent.prependLeaf @
else
parent.appendLeaf @
AST = (syntax_tree, patterns=[]) ->
@syntax_tree = syntax_tree
@patterns = patterns
AST.cutLeaves @syntax_tree, @patterns
@
AST.cutLeaves = (syntax_tree, patterns) ->
walk = (node) ->
for leaf in node.leaves
walk leaf
if node.lex in patterns or (node.lex.slice(0, 2) == 'E!' and node.value == null)
new_parent_leaves = []
for neighbor in node.parent.leaves
if neighbor == node
new_parent_leaves = new_parent_leaves.concat node.leaves
for leaf in node.leaves
leaf.parent = node.parent
else
new_parent_leaves = new_parent_leaves.concat neighbor
node.parent.leaves = new_parent_leaves
if not node.leaves.length
delete node.leaves
if node.value == null
delete node.value
walk syntax_tree
SyntaxParser = (input_lex) ->
@raw_input_lex = input_lex
@input_lex = input_lex.map (x) -> if x instanceof Array then x[0] else x
@input_val = input_lex.map (x) -> if x instanceof Array then x[1] else null
@stack = []
@sync_lex = []
@_reduce_cache = {}
@_reduce_cache_len = {}
@tree = new GrammarNode 'Syntax'
@
SyntaxParser.flow = (args) ->
console.log 'SyntaxParser'
grammar = args.grammar
start_stmt = args.start_stmt
end_lex = args.end_lex
sync_lex = args.sync_lex or []
lex_list = args.lex_list
mix_map = args.mix_map
ast_cutter = args.ast_cutter
SyntaxParser.Mix.mixer = ->
mix_map.arrange.apply mix_map, arguments
syntax_table = new SyntaxTable grammar, start_stmt, end_lex
syntax_parser = new SyntaxParser lex_list
syntax_parser.sync_lex = sync_lex
syntax_parser.parseTable syntax_table
ast = syntax_parser.getAST ast_cutter
args.ast = ast.syntax_tree
SyntaxParser.Mix.mixer = null
SyntaxParser::getAST = (patterns) ->
new AST @tree, patterns
SyntaxParser::shift = ->
if @input_lex.length
@stack.push @input_lex.shift()
else
return false
SyntaxParser.checkIfReduce = (syntax_table, stack, lookahead) ->
spcr = SyntaxParser.checkIfReduce
cached_index = spcr.getCachedIndex stack
if cached_index > -1
syntax_table.live_grammars = spcr.checkedLiveGrammars[cached_index]
if not syntax_table.live_grammars
syntax_table.init()
len = stack.length
stack.push lookahead
ret = []
for index in [cached_index + 1..len - 1]
log syntax_table.live_grammars, 'before live grammar'
result = syntax_table.moveDot stack[index], stack[index + 1], (index + 1)
log result, 'after move dot'
log syntax_table.live_grammars, 'live grammar'
log index + 1, 'move dot index'
if result
ret = stack.slice 0, result['expand_level']
ret.push result['closure']
stack.pop()
if ret.length
spcr.lastCheckedStack = ret.slice 0, -1
# spcr.lastCheckedStack = []
else
spcr.lastCheckedStack = stack.slice()
spcr.checkedLiveGrammars = spcr.checkedLiveGrammars.slice(0, spcr.lastCheckedStack.length)
while spcr.checkedLiveGrammars.length < spcr.lastCheckedStack.length
spcr.checkedLiveGrammars.push ''
if not ret.length
spcr.checkedLiveGrammars[spcr.checkedLiveGrammars.length - 1] = syntax_table.live_grammars
# console.log stack
# console.log ret
# console.log spcr.lastCheckedStack
# console.log spcr.checkedLiveGrammars
# console.log ''
if ret.length then ret else stack
SyntaxParser.checkIfReduce.lastCheckedStack = []
SyntaxParser.checkIfReduce.checkedLiveGrammars = []
SyntaxParser.checkIfReduce.getCachedIndex = (stack) ->
scif = SyntaxParser.checkIfReduce
for item, index in stack
if scif.lastCheckedStack[index] != item
index--
break
if index == stack.length then index - 1 else index
# Not For Use
SyntaxParser::mutilReverseReduce = (syntax_table) ->
for i in [1..@stack.length]
syntax_table.init()
result = SyntaxParser.checkIfReduce syntax_table, (@stack.slice -i), @input_lex[0]
if result
@stack = @stack.slice 0, (@stack.length - i)
@stack.push result
if i > 1
@reduce syntax_table
break
# Not For Use
SyntaxParser::MutilReduce = (syntax_table) ->
for i in [0..@stack.length - 1]
syntax_table.init()
result = SyntaxParser.checkIfReduce syntax_table, (@stack.slice i, @stack.length), @input_lex[0]
if result
@stack = @stack.slice 0, i
@stack.push result
@reduce syntax_table
break
SyntaxParser::reduce = (syntax_table, value_assign=true) ->
log @stack, 'before reduce'
@generateTree(value_assign)
syntax_table.live_grammars = null
result = SyntaxParser.checkIfReduce syntax_table, @stack, @input_lex[0]
sync_index = @sync_lex.indexOf result[result.length - 1]
sync_len = @sync_lex.length
if sync_index > -1 and sync_index != sync_len - 1 and @stack == result
last_stmt_index = result.lastIndexOf @sync_lex[@sync_lex.length - 1]
result = result.slice(0, last_stmt_index + 1).concat @sync_lex[sync_index + 1]
if result
stack_changed = @stack != result
value_assign = if result.length > @stack.length then true else false
if stack_changed
@stack = result
@reduce syntax_table, value_assign
SyntaxParser::generateTree = (value_assign) ->
curr_stack = @stack.slice()
curr_len = curr_stack.length
curr_node_leaves_len = @tree.leaves.length
if curr_len > curr_node_leaves_len
new_gt = new GrammarNode curr_stack[curr_len - 1], @tree
else
for i in [0..curr_len - 1]
if not @tree.leaves[i].isName curr_stack[i]
break
new_gt = new GrammarNode curr_stack[curr_len - 1]
while i++ != curr_node_leaves_len
@tree.leaves.pop().linkParent new_gt, true
new_gt.linkParent @tree
if value_assign
SyntaxParser.Mix ['SyntaxNode', new_gt], ['Lex', @raw_input_lex[@raw_input_lex.length - @input_val.length]]
new_gt.value = @input_val.shift()
SyntaxParser::parseTable = (syntax_table) ->
len = @.input_lex.length - 1
for i in [1..len]
@.shift syntax_table
log @.stack, 'before reduce'
@.reduce syntax_table
log @.stack, 'after reduce'
SyntaxParser.Mix = ->
if not SyntaxParser.Mix.mixer
return null
SyntaxParser.Mix.mixer.apply @, arguments
SyntaxParser.rebuild = (syntax_node, mix_map, parent) ->
if typeof syntax_node.parent == 'string'
syntax_node.parent = parent
if mix_map and syntax_node['__MixMapID__']
mix_map.refs[syntax_node['__MixMapID__']] = syntax_node
if syntax_node.leaves
for leaf in syntax_node.leaves
SyntaxParser.rebuild leaf, mix_map, syntax_node
if typeof self == 'undefined'
module.exports.SyntaxTable = SyntaxTable
module.exports.SyntaxParser = SyntaxParser
module.exports.AST = AST
else
self.SyntaxTable = SyntaxTable
self.SyntaxParser = SyntaxParser
self.AST = AST
log = ->
return
log = util.log