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