depager 0.2.3 → 0.3.0.b20160729
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.en +4 -19
- data/README.ja +42 -79
- data/bin/depager +42 -45
- data/examples/action_pl0d/pl0d.action.dr +421 -0
- data/examples/action_pl0d/test.pl0ds +49 -0
- data/examples/c89/c89.dr +493 -496
- data/examples/c89/test.c89 +10 -10
- data/examples/extension/astdf.rb +10 -0
- data/examples/extension/atree.dr +55 -0
- data/examples/{sample_calc → extension}/calc.atree.dr +42 -43
- data/examples/extension/calc.simple_action.dr +33 -0
- data/examples/extension/paction.dr +16 -15
- data/examples/extension/pactiontest.dr +14 -14
- data/examples/extension/simple_action.rb +44 -0
- data/examples/pl0d/pl0ds.dr +337 -334
- data/examples/pl0d/test.pl0ds +33 -33
- data/examples/rie_calc/calc.rie.dr +57 -0
- data/examples/rie_calc/test.calc +4 -0
- data/examples/rie_dcuse/dcuse.rie.dr +71 -0
- data/examples/rie_dcuse/test.dcuse +1 -0
- data/examples/rie_pl0/orig_ex/exerrdg.pl0 +44 -0
- data/examples/rie_pl0/orig_ex/exerrm.pl0 +19 -0
- data/examples/rie_pl0/orig_ex/exerrmre.pl0 +20 -0
- data/examples/rie_pl0/orig_ex/exerrtok.pl0 +18 -0
- data/examples/rie_pl0/orig_ex/exmdg.pl0 +40 -0
- data/examples/rie_pl0/orig_ex/exmdgwwl.pl0 +43 -0
- data/examples/rie_pl0/orig_ex/exmrw.pl0 +22 -0
- data/examples/rie_pl0/orig_ex/exmwwl.pl0 +18 -0
- data/examples/rie_pl0/orig_ex/exnorw.pl0 +17 -0
- data/examples/rie_pl0/pl0.rie.dr +450 -0
- data/examples/rie_pl0/test.pl0 +10 -0
- data/examples/sample_calc/calc.action.dr +33 -33
- data/examples/sample_calc/calc.ast.action.dr +65 -66
- data/examples/sample_calc/calc.ast.dr +55 -55
- data/examples/sample_calc/calc.cst.dr +45 -45
- data/examples/sample_calc/calc.dr +43 -43
- data/examples/sample_calc/calc.lex.dr +29 -29
- data/examples/sample_calc/{calc_prec.nvaction.dr → calc_prec.action.dr} +31 -31
- data/examples/slex_test/divreg.slex.dr +29 -29
- data/examples/slex_test/ljoin.slex.dr +36 -36
- data/examples/slex_test/test.divreg +1 -1
- data/examples/slex_test/test.ljoin +3 -3
- data/lib/depager.rb +582 -670
- data/lib/depager/grammar.rb +256 -291
- data/lib/depager/lr.rb +574 -579
- data/lib/depager/parser.rb +282 -277
- data/lib/depager/ruby/plugins/_rie_debug.rb +35 -0
- data/lib/depager/ruby/plugins/action.rb +53 -43
- data/lib/depager/ruby/plugins/ast.dr +364 -269
- data/lib/depager/ruby/plugins/ast.rb +1367 -1308
- data/lib/depager/ruby/plugins/cst.dr +172 -180
- data/lib/depager/ruby/plugins/cst.rb +587 -626
- data/lib/depager/ruby/plugins/lex.dr +85 -89
- data/lib/depager/ruby/plugins/lex.rb +310 -336
- data/lib/depager/ruby/plugins/rie.dr +723 -0
- data/lib/depager/ruby/plugins/rie.rb +1653 -0
- data/lib/depager/ruby/plugins/slex.dr +202 -200
- data/lib/depager/ruby/plugins/slex.rb +780 -817
- data/lib/depager/ruby/plugins/srp.rb +56 -51
- data/lib/depager/ruby/templates/extension_lalr_master.erb +46 -51
- data/lib/depager/ruby/templates/extension_lalr_slave.erb +99 -107
- data/lib/depager/ruby/templates/single_lalr_parser.erb +115 -117
- data/lib/depager/utils.rb +148 -318
- data/lib/depager/version.rb +4 -3
- metadata +52 -60
- data/ChangeLog +0 -16
- data/data/depager/pre-setup.rb +0 -3
- data/examples/c89/c89.tab.rb +0 -7127
- data/examples/pl0d/pl0ds.tab.rb +0 -2698
- data/examples/sample_calc/calc.action.tab.rb +0 -457
- data/examples/sample_calc/calc.ast.action.tab.rb +0 -749
- data/examples/sample_calc/calc.ast.tab.rb +0 -665
- data/examples/sample_calc/calc.astdf.dr +0 -54
- data/examples/sample_calc/calc.astdf.tab.rb +0 -672
- data/examples/sample_calc/calc.atree.tab.rb +0 -451
- data/examples/sample_calc/calc.cst.tab.rb +0 -644
- data/examples/sample_calc/calc.lex.tab.rb +0 -374
- data/examples/sample_calc/calc.nvaction.dr +0 -33
- data/examples/sample_calc/calc.nvaction.tab.rb +0 -465
- data/examples/sample_calc/calc.tab.rb +0 -365
- data/examples/sample_calc/calc_prec.nvaction.tab.rb +0 -431
- data/examples/slex_test/divreg.slex.tab.rb +0 -303
- data/examples/slex_test/ljoin.slex.tab.rb +0 -370
- data/lib/depager/ruby/plugins/_ast_tmpl.rb +0 -73
- data/lib/depager/ruby/plugins/astdf.rb +0 -6
- data/lib/depager/ruby/plugins/atree.dr +0 -55
- data/lib/depager/ruby/plugins/atree.rb +0 -347
- data/lib/depager/ruby/plugins/nvaction.rb +0 -19
- data/lib/depager/ruby/templates/simple.erb +0 -23
- data/setup.rb +0 -1585
data/lib/depager/lr.rb
CHANGED
@@ -1,579 +1,574 @@
|
|
1
|
-
|
2
|
-
require 'depager/grammar.rb'
|
3
|
-
|
4
|
-
module Depager::
|
5
|
-
class
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
def
|
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
|
-
@f0e[
|
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
|
-
def
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
def
|
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
|
-
def
|
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
|
-
end
|
225
|
-
rg
|
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
|
-
states[
|
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
|
-
oldv
|
312
|
-
|
313
|
-
|
314
|
-
newv
|
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
|
-
" reduce: #{rl[-
|
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
|
-
rs =
|
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
|
-
i
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
end
|
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
|
-
output << "
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
}.join("\n")
|
520
|
-
output << "#{str}\n\n"
|
521
|
-
end
|
522
|
-
|
523
|
-
if
|
524
|
-
output << "
|
525
|
-
str = g.
|
526
|
-
output << "#{str}\n\n"
|
527
|
-
end
|
528
|
-
|
529
|
-
if
|
530
|
-
output << "***
|
531
|
-
str =
|
532
|
-
output << "#{str}\n\n"
|
533
|
-
end
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
output <<
|
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
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
File.open("#{File.basename(dp.files.fname, '.dr')}.output", "w"){|f|
|
576
|
-
f.write output.join("\n")
|
577
|
-
}
|
578
|
-
end
|
579
|
-
end
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'depager/grammar.rb'
|
3
|
+
|
4
|
+
module Depager::LALR
|
5
|
+
class ParserGenerator < Depager::ParserGenerator
|
6
|
+
def initialize d_parser
|
7
|
+
super
|
8
|
+
@parsing_method = Depager::LALR
|
9
|
+
end
|
10
|
+
|
11
|
+
def parser_code_template
|
12
|
+
File.read("#{TEMPLATES_DIR}/single_lalr_parser.erb")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class ExtensionGenerator < Depager::ExtensionGenerator
|
17
|
+
def initialize d_parser
|
18
|
+
super
|
19
|
+
@parsing_method = Depager::LALR
|
20
|
+
end
|
21
|
+
|
22
|
+
def master_code_template
|
23
|
+
File.read("#{TEMPLATES_DIR}/extension_lalr_master.erb")
|
24
|
+
end
|
25
|
+
|
26
|
+
def slave_code_template
|
27
|
+
File.read("#{TEMPLATES_DIR}/extension_lalr_slave.erb")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
EPS = Depager::EPS
|
32
|
+
TLA = EPS - 1
|
33
|
+
|
34
|
+
class Grammar < Depager::Grammar
|
35
|
+
attr_accessor :memo_closure
|
36
|
+
def initialize_depend
|
37
|
+
@first1[TLA] = [TLA]
|
38
|
+
@sym_mask[TLA] = (1 << @sym_mask.size)
|
39
|
+
@mask_sym << TLA
|
40
|
+
make_sym_f0e
|
41
|
+
@memo_closure = {}
|
42
|
+
@memo_closure1 = {}
|
43
|
+
end
|
44
|
+
|
45
|
+
def make_sym_f0e
|
46
|
+
@f0e = {}
|
47
|
+
@empty_rules.each do |lhs, rule_no|
|
48
|
+
@f0e[lhs] = { rule_no => symset([EPS]) }
|
49
|
+
end
|
50
|
+
begin
|
51
|
+
changed = false
|
52
|
+
@rulelist.each do |rule|
|
53
|
+
next unless @f0e[rule.rhs[0]]
|
54
|
+
|
55
|
+
lhs, rhs = rule.lhs, rule.rhs
|
56
|
+
@f0e[lhs] ||= {}
|
57
|
+
@f0e[rhs[0]].each do |n, fst|
|
58
|
+
@f0e[lhs][n] ||= symset()
|
59
|
+
if fst.include? EPS
|
60
|
+
fst = fst.dup
|
61
|
+
fst.delete(EPS)
|
62
|
+
fst.merge!(first(rhs[1 .. -1]))
|
63
|
+
end
|
64
|
+
unless fst.subset_of? @f0e[lhs][n]
|
65
|
+
@f0e[lhs][n].merge! fst
|
66
|
+
changed = true
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end while changed
|
71
|
+
end
|
72
|
+
|
73
|
+
def closure1 rule, n, la
|
74
|
+
key = [rule.n, n, la]
|
75
|
+
return @memo_closure1[key] if @memo_closure1[key]
|
76
|
+
|
77
|
+
result = [ LRItem[rule, n, symset(la)] ]
|
78
|
+
memo = n == 0 ? { result[0].rule.n => result[0] } : {}
|
79
|
+
memo2 = {}
|
80
|
+
i = 0
|
81
|
+
while i < result.size
|
82
|
+
ri = result[i] ; i += 1
|
83
|
+
dotsym = ri.dotsym
|
84
|
+
next unless nonterm? dotsym
|
85
|
+
|
86
|
+
b = first(ri.dotrest)
|
87
|
+
if b.delete(EPS) then b.merge! ri.la
|
88
|
+
elsif memo2[ ri ] then next
|
89
|
+
end
|
90
|
+
memo2[ ri ] = true
|
91
|
+
|
92
|
+
lhs_to_rule[dotsym].each do |rule|
|
93
|
+
if li = memo[rule.n]
|
94
|
+
unless b.subset_of? li.la
|
95
|
+
li.la.merge! b
|
96
|
+
result << li
|
97
|
+
end
|
98
|
+
else
|
99
|
+
li = memo[rule.n] = LRItem[rule, 0, b]
|
100
|
+
result << li
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
result.uniq!
|
106
|
+
@memo_closure1[key] = result
|
107
|
+
end
|
108
|
+
|
109
|
+
def closure rule, n, base=nil
|
110
|
+
i = 0; appended = {}
|
111
|
+
result = [base ? base : LRItem[rule, n]]
|
112
|
+
memo = memo_closure
|
113
|
+
|
114
|
+
while i < result.size
|
115
|
+
ds = result[i].dotsym
|
116
|
+
if nonterm?(ds) && !appended[ds]
|
117
|
+
unless memo[ds]
|
118
|
+
memo[ds] = []
|
119
|
+
lhs_to_rule[ds].each do |rule|
|
120
|
+
memo[ds] << LRItem[rule, 0]
|
121
|
+
end
|
122
|
+
end
|
123
|
+
result.concat memo[ds]
|
124
|
+
appended[ds] = true
|
125
|
+
end
|
126
|
+
i += 1
|
127
|
+
end
|
128
|
+
result
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
class Rule < Depager::Rule
|
133
|
+
end
|
134
|
+
|
135
|
+
class LRItem
|
136
|
+
attr_accessor :rule, :n, :la
|
137
|
+
def initialize rule, n, la = nil
|
138
|
+
@rule = rule
|
139
|
+
@n = n
|
140
|
+
@la = la || rule.grammar.symset()
|
141
|
+
@_hash = nil
|
142
|
+
end
|
143
|
+
def self.[] rule, n, la = nil
|
144
|
+
LRItem.new rule, n, la
|
145
|
+
end
|
146
|
+
|
147
|
+
def hash
|
148
|
+
return @_hash if @_hash
|
149
|
+
@_hash = (@rule.hash * 217 + @n.hash) % 76511
|
150
|
+
end
|
151
|
+
def eql? i
|
152
|
+
@rule == i.rule && @n == i.n
|
153
|
+
end
|
154
|
+
alias == eql?
|
155
|
+
|
156
|
+
def to_s
|
157
|
+
la = @la.map{|i| @rule.grammar.symname i}.sort.join(' ')
|
158
|
+
lhs = @rule.grammar.symname @rule.lhs
|
159
|
+
rhs = @rule.rhs.map{|i| @rule.grammar.symname i}.insert(@n, '_').join(' ')
|
160
|
+
|
161
|
+
str = "(#{'%03s' % @rule.n}) #{lhs} : #{rhs}"
|
162
|
+
str << "\n # #{la}" if Depager.debug_mode?(:l)
|
163
|
+
return str
|
164
|
+
end
|
165
|
+
def closure1 la
|
166
|
+
@rule.grammar.closure1 @rule, @n, la
|
167
|
+
end
|
168
|
+
def closure
|
169
|
+
@rule.grammar.closure @rule, @n, self
|
170
|
+
end
|
171
|
+
def dotsym
|
172
|
+
@rule.rhs[@n]
|
173
|
+
end
|
174
|
+
def dotrest
|
175
|
+
@rule.rhs[@n+1 .. -1]
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
class State
|
180
|
+
attr_accessor :items, :n
|
181
|
+
def initialize items, n = nil
|
182
|
+
@items = items
|
183
|
+
@goto = {}
|
184
|
+
@n = n
|
185
|
+
@closure = nil
|
186
|
+
@_hash = nil
|
187
|
+
end
|
188
|
+
|
189
|
+
def hash
|
190
|
+
return @_hash if @_hash
|
191
|
+
@_hash = @items.hash
|
192
|
+
end
|
193
|
+
def eql? i
|
194
|
+
@items == i.items
|
195
|
+
end
|
196
|
+
alias == eql?
|
197
|
+
|
198
|
+
def to_s
|
199
|
+
("I%03i = \n" % n) << @items.map{|i| i.to_s }.join("\n").lines.map{|i| " #{i}"}.join
|
200
|
+
end
|
201
|
+
def empty?
|
202
|
+
@items.empty?
|
203
|
+
end
|
204
|
+
def self.[] items = []
|
205
|
+
State.new items
|
206
|
+
end
|
207
|
+
def closure
|
208
|
+
return @closure if @closure
|
209
|
+
r = []
|
210
|
+
@items.each do |i|
|
211
|
+
r |= i.closure
|
212
|
+
end
|
213
|
+
@closure = r
|
214
|
+
end
|
215
|
+
def gg
|
216
|
+
@goto
|
217
|
+
end
|
218
|
+
def mkgoto
|
219
|
+
r = Hash.new{|h,k| h[k]=[]}
|
220
|
+
closure.each do |c|
|
221
|
+
if c.n < c.rule.rhs.size
|
222
|
+
r[c.dotsym].push LRItem[c.rule, c.n+1]
|
223
|
+
end
|
224
|
+
end
|
225
|
+
rg = []
|
226
|
+
r.each do |k, v|
|
227
|
+
rg.push [k, (@goto[k] = State.new(v))]
|
228
|
+
end
|
229
|
+
rg
|
230
|
+
end
|
231
|
+
def goto x
|
232
|
+
return @goto[x] || State.new([])
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
class Table
|
237
|
+
attr_accessor :grammar, :action_table, :goto_table, :states
|
238
|
+
attr_accessor :defred_table, :defred_after_shift_table
|
239
|
+
def initialize grammar
|
240
|
+
@grammar = grammar
|
241
|
+
@states = nil
|
242
|
+
@warning_list = []
|
243
|
+
|
244
|
+
items
|
245
|
+
mkset
|
246
|
+
mktable
|
247
|
+
end
|
248
|
+
|
249
|
+
def items
|
250
|
+
warn '** LR(0) **' if Depager.debug_mode?
|
251
|
+
n = 0
|
252
|
+
m = 0
|
253
|
+
states = [ State.new([ LRItem[grammar[0], 0] ], 0) ]
|
254
|
+
memo = {states[0] => m}
|
255
|
+
|
256
|
+
while n < states.size
|
257
|
+
states[n].mkgoto.each do |x, a|
|
258
|
+
unless a.empty?
|
259
|
+
if memo_a = memo[a]
|
260
|
+
states[n].gg[x] = states[memo_a]
|
261
|
+
else
|
262
|
+
m += 1
|
263
|
+
a.n = m
|
264
|
+
states[m] = a
|
265
|
+
memo[a] = m
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
269
|
+
n += 1
|
270
|
+
end
|
271
|
+
@states = states
|
272
|
+
end
|
273
|
+
|
274
|
+
# -1:reduce sh; 1:shift rd(=key); 0:can't resolve
|
275
|
+
def resolveconf(sh, rd)
|
276
|
+
precs = grammar.precs
|
277
|
+
psh = precs[sh]
|
278
|
+
mrt = grammar[rd].rhs.reverse.find{|i| grammar.term?(i) }
|
279
|
+
prd = grammar[rd].prec || precs[mrt]
|
280
|
+
|
281
|
+
if psh && prd
|
282
|
+
if psh[1] == prd[1]
|
283
|
+
# warn :LEFT ? 'reduce' : 'shift'
|
284
|
+
psh[0] == :LEFT ? -1 : 1
|
285
|
+
elsif psh[1] > prd[1]
|
286
|
+
# warn 'reduce'
|
287
|
+
-1
|
288
|
+
else
|
289
|
+
# warn 'shift'
|
290
|
+
1
|
291
|
+
end
|
292
|
+
else
|
293
|
+
0
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
def checkconf newv, oldv, key, g = nil
|
298
|
+
return newv unless oldv
|
299
|
+
|
300
|
+
rl = grammar.rulelist
|
301
|
+
syms = grammar.syms
|
302
|
+
if oldv == 'ACC'
|
303
|
+
@warning_list << "'ACC' conflict #{g}."
|
304
|
+
return oldv
|
305
|
+
end
|
306
|
+
# warn "\n-- N:#{newv} O:#{oldv} K:#{grammar.syms[key]} "
|
307
|
+
if newv < 0 and oldv > 0
|
308
|
+
# warn "-:shift #{grammar.syms[key]} go to #{oldv}"
|
309
|
+
r = resolveconf(key, -newv)
|
310
|
+
if r > 0
|
311
|
+
# warn "+:shift #{grammar.syms[key]} go to #{oldv}"
|
312
|
+
oldv
|
313
|
+
elsif r < 0
|
314
|
+
# warn "+:reduce #{grammar[-newv]}"
|
315
|
+
newv
|
316
|
+
else
|
317
|
+
@warning_list <<
|
318
|
+
("shift/reduce conflict #{syms[key]}.\n #{g}\n" <<
|
319
|
+
" shift : #{syms[key]}\n" <<
|
320
|
+
" reduce: #{rl[-newv]}")
|
321
|
+
oldv
|
322
|
+
end
|
323
|
+
elsif newv > 0 and oldv < 0
|
324
|
+
r = resolveconf(key, -oldv)
|
325
|
+
if r > 0
|
326
|
+
# warn "shift"
|
327
|
+
newv
|
328
|
+
elsif r <0
|
329
|
+
# warn "reduce"
|
330
|
+
oldv
|
331
|
+
else
|
332
|
+
@warning_list <<
|
333
|
+
("shift/reduce conflict #{syms[key]}.\n #{g}\n" <<
|
334
|
+
" shift : #{syms[key]}\n" <<
|
335
|
+
" reduce: #{rl[-oldv]}")
|
336
|
+
newv
|
337
|
+
end
|
338
|
+
elsif newv < 0 and oldv < 0
|
339
|
+
unless newv == oldv
|
340
|
+
@warning_list <<
|
341
|
+
("reduce/reduce conflict #{syms[key]}.\n #{g}\n" <<
|
342
|
+
" reduce: #{rl[-newv]}\n" <<
|
343
|
+
" reduce: #{rl[-oldv]}" )
|
344
|
+
newv
|
345
|
+
end
|
346
|
+
else
|
347
|
+
warn "must not happen"
|
348
|
+
newv
|
349
|
+
end
|
350
|
+
end
|
351
|
+
|
352
|
+
def mktable
|
353
|
+
warn '** TABLE **' if Depager.debug_mode?
|
354
|
+
taction = (0...@states.size).map{ Array.new(grammar.syms.size - grammar.nonterms.size) }
|
355
|
+
tgoto = (0...@states.size).map{ Array.new(grammar.nonterms.size - 1) }
|
356
|
+
|
357
|
+
@states.each_with_index do |c, i|
|
358
|
+
c.gg.each do |k, j|
|
359
|
+
next unless k
|
360
|
+
if grammar.term? k
|
361
|
+
key = k - grammar.nonterms.size
|
362
|
+
taction[i][key] = checkconf(j.n, taction[i][key], k, c )
|
363
|
+
else
|
364
|
+
tgoto[i][k - 1] = j.n
|
365
|
+
end
|
366
|
+
end
|
367
|
+
c.items.each do |j|
|
368
|
+
if j.n == j.rule.rhs.size and j.rule.lhs != 0 # $start
|
369
|
+
j.la.each do |u|
|
370
|
+
key = u - grammar.nonterms.size
|
371
|
+
taction[i][key] = checkconf(-j.rule.n, taction[i][key], u, c)
|
372
|
+
end
|
373
|
+
elsif efs = grammar.f0e[j.dotsym]
|
374
|
+
efs.each do |rn, ef|
|
375
|
+
ef.each do |es|
|
376
|
+
fst = grammar.symset
|
377
|
+
j.la.each do |la|
|
378
|
+
fst |= grammar.first [es, *j.dotrest].push(la)
|
379
|
+
end
|
380
|
+
fst.each do |u|
|
381
|
+
key = u - grammar.nonterms.size
|
382
|
+
taction[i][key] = checkconf(-rn, taction[i][key], u, c)
|
383
|
+
end
|
384
|
+
end
|
385
|
+
end
|
386
|
+
end
|
387
|
+
if j.rule == grammar[0] && j.n == 1 # $start : ...
|
388
|
+
taction[i][0] = 'ACC'
|
389
|
+
end
|
390
|
+
end
|
391
|
+
end
|
392
|
+
|
393
|
+
@defred_table = []
|
394
|
+
taction.each_with_index do |l, x|
|
395
|
+
rs = l.select{|i| i.is_a? Integer}.uniq.compact
|
396
|
+
rs = rs.select{|i| i < 0 }
|
397
|
+
if rs.size == 1
|
398
|
+
@defred_table[x] = rs[0]
|
399
|
+
else
|
400
|
+
@defred_table[x] = nil
|
401
|
+
end
|
402
|
+
end
|
403
|
+
|
404
|
+
@defred_after_shift_table = []
|
405
|
+
taction.each_with_index do |l, x|
|
406
|
+
rs = l.select{|i| i.is_a? Integer}.uniq.compact
|
407
|
+
if rs.size == 1 and rs[0] < 0
|
408
|
+
@defred_after_shift_table[x] = rs[0]
|
409
|
+
else
|
410
|
+
@defred_after_shift_table[x] = nil
|
411
|
+
end
|
412
|
+
end
|
413
|
+
|
414
|
+
taction.each_with_index do |l, x|
|
415
|
+
if @defred_table[x]
|
416
|
+
l.size.times do |x|
|
417
|
+
l[x] = nil if l[x] && l[x] < 0
|
418
|
+
end
|
419
|
+
end
|
420
|
+
end
|
421
|
+
|
422
|
+
@action_table=taction
|
423
|
+
@goto_table=tgoto
|
424
|
+
end
|
425
|
+
|
426
|
+
def check_table dp
|
427
|
+
@warning_list.each{|i| dp.warning i }
|
428
|
+
|
429
|
+
if Depager.debug_mode?
|
430
|
+
gen_state_info
|
431
|
+
verbose dp if Depager.verbose_mode?
|
432
|
+
end
|
433
|
+
end
|
434
|
+
|
435
|
+
def mkset
|
436
|
+
warn '** LA **' if Depager.debug_mode?
|
437
|
+
trn = []
|
438
|
+
@states.each do |state|
|
439
|
+
state.items.each do |sitem|
|
440
|
+
sitem.closure1([TLA]).each do |citem|
|
441
|
+
next if citem.n == citem.rule.rhs.size
|
442
|
+
gi = state.goto(citem.dotsym).items
|
443
|
+
i = gi.find{|j| j.rule == citem.rule && j.n == citem.n+1 }
|
444
|
+
trn << [sitem, i] if citem.la.include? TLA
|
445
|
+
i.la.merge!(citem.la).delete(TLA)
|
446
|
+
end
|
447
|
+
end
|
448
|
+
end
|
449
|
+
|
450
|
+
warn '** LALR(1) **' if Depager.debug_mode?
|
451
|
+
@states[0].items[0].la = @grammar.symset([grammar.nonterms.size]) # '$'
|
452
|
+
begin
|
453
|
+
changed = false
|
454
|
+
trn.each do |k, v|
|
455
|
+
unless k.la.subset_of?(v.la)
|
456
|
+
v.la.merge!(k.la)
|
457
|
+
changed
|
458
|
+
end
|
459
|
+
end
|
460
|
+
end while changed
|
461
|
+
end
|
462
|
+
end
|
463
|
+
end
|
464
|
+
|
465
|
+
class Depager::LALR::Table
|
466
|
+
def state_info
|
467
|
+
@state_info
|
468
|
+
end
|
469
|
+
|
470
|
+
def gen_state_info
|
471
|
+
g = grammar
|
472
|
+
@state_info = []
|
473
|
+
@action_table.each_with_index {|s,x|
|
474
|
+
shi, red, acc = "", "", ""
|
475
|
+
s.each_with_index {|t, y|
|
476
|
+
next unless t
|
477
|
+
hd = (" %-15s" % g.symname(y + g.nonterms.size))
|
478
|
+
if t == 'ACC'
|
479
|
+
acc = "#{hd} accept"
|
480
|
+
elsif t < 0
|
481
|
+
red << "#{hd} reduce using rule #{-t} (#{g.symname g[-t].lhs})\n"
|
482
|
+
else
|
483
|
+
shi << "#{hd} shift, and goto to state #{t}\n"
|
484
|
+
end
|
485
|
+
}
|
486
|
+
if t = @defred_table[x]
|
487
|
+
as = @defred_after_shift_table[x] ? " [after shift]" : ""
|
488
|
+
red << (" %-15s" % '$default') <<
|
489
|
+
"reduce using rule #{-t} (#{g.symname g[-t].lhs}) #{as}"
|
490
|
+
end
|
491
|
+
|
492
|
+
@state_info << (@states[x].to_s << "\n\n#{shi}\n#{red}\n#{acc}").strip
|
493
|
+
}
|
494
|
+
end
|
495
|
+
|
496
|
+
def verbose dp
|
497
|
+
g, output = grammar, []
|
498
|
+
|
499
|
+
if Depager.debug_mode?(:f)
|
500
|
+
output << "** FIRST1 **"
|
501
|
+
str = g.first1.map{|k, v|
|
502
|
+
"#{g.symname k} => #{v.map{|i| g.symname i}.join(' ')}"
|
503
|
+
}.join("\n")
|
504
|
+
output << "#{str}\n\n"
|
505
|
+
end
|
506
|
+
|
507
|
+
if Depager.debug_mode?(:e)
|
508
|
+
output << "** Empty Reduction **"
|
509
|
+
str = g.f0e.map {|k, v|
|
510
|
+
"#{g.symname k} =>\n" << v.map{|n, f|
|
511
|
+
" #{rulelist[n]} ? #{f.map{|i| g.symname i}.join(' ')}"
|
512
|
+
}.join("\n")
|
513
|
+
}.join("\n")
|
514
|
+
output << "#{str}\n\n"
|
515
|
+
end
|
516
|
+
|
517
|
+
if Depager.debug_mode?(:s)
|
518
|
+
output << "** SYMBOLS **"
|
519
|
+
str = g.syms.map{|k,_| "#{"%03i" % k} #{ g.symname k}"}.sort.join("\n")
|
520
|
+
output << "#{str}\n\n"
|
521
|
+
end
|
522
|
+
|
523
|
+
if Depager.debug_mode?(:g)
|
524
|
+
output << "*** Grammar ***"
|
525
|
+
str = g.rulelist.join("\n")
|
526
|
+
output << "#{str}\n\n"
|
527
|
+
end
|
528
|
+
|
529
|
+
if Depager.debug_mode?(:c)
|
530
|
+
output << "*** States ***"
|
531
|
+
str = @state_info.join("\n\n").strip
|
532
|
+
output << "#{str}\n\n"
|
533
|
+
end
|
534
|
+
|
535
|
+
nssize = g.nonterms.size
|
536
|
+
if Depager.debug_mode?(:t)
|
537
|
+
output << "*** Action Table ***"
|
538
|
+
ws = (nssize...g.syms.size).map{|i| j = g.symname(i).size; j < 6 ? 6 : j}
|
539
|
+
str = " |"
|
540
|
+
(nssize...g.syms.size).each_with_index{|i, x|
|
541
|
+
str << ("%0#{ws[x]}s|" % g.symname(i))
|
542
|
+
}; str << " $default|\n"
|
543
|
+
|
544
|
+
@action_table.each_with_index{|i,x|
|
545
|
+
str << ("%03i|" % x)
|
546
|
+
i.each_with_index{|j, y|
|
547
|
+
str << ("%0#{ws[y]}s|" % j)
|
548
|
+
}
|
549
|
+
str << ("%04s,%04s|\n" % [@defred_table[x], @defred_after_shift_table[x]])
|
550
|
+
}
|
551
|
+
output << "#{str}\n\n"
|
552
|
+
|
553
|
+
output << "*** Goto Table ***"
|
554
|
+
ws = (1...nssize).map{|i| j = g.symname(i).size; j < 6 ? 6 : j}
|
555
|
+
str = " |"
|
556
|
+
(1...nssize).each_with_index{|i, x|
|
557
|
+
str << ("%0#{ws[x]}s|" % g.symname(i))
|
558
|
+
}; str << "\n"
|
559
|
+
|
560
|
+
@goto_table.each_with_index{|i,x|
|
561
|
+
str << ("%03i|" % x)
|
562
|
+
i.each_with_index{|j, y|
|
563
|
+
str << ("%0#{ws[y]}s|" % j)
|
564
|
+
}
|
565
|
+
str << "\n"
|
566
|
+
}
|
567
|
+
output << "#{str}\n\n"
|
568
|
+
end
|
569
|
+
|
570
|
+
File.open("#{File.basename(dp.file.path, '.dr')}.output", "w"){|f|
|
571
|
+
f.write output.join("\n")
|
572
|
+
}
|
573
|
+
end
|
574
|
+
end
|