depager 0.2.0 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +4 -0
- data/README.en +5 -10
- data/bin/depager +17 -20
- data/examples/c89/c89.tab.rb +5632 -702
- data/examples/pl0d/pl0ds.dr +41 -41
- data/examples/pl0d/pl0ds.tab.rb +1887 -874
- data/examples/sample_calc/calc.action.tab.rb +243 -69
- data/examples/sample_calc/{calc.astl.action.dr → calc.ast.action.dr} +7 -7
- data/examples/sample_calc/calc.ast.action.tab.rb +755 -0
- data/examples/sample_calc/{calc.astl.dr → calc.ast.dr} +7 -7
- data/examples/sample_calc/calc.ast.tab.rb +672 -0
- data/examples/sample_calc/calc.astdf.dr +5 -5
- data/examples/sample_calc/calc.astdf.tab.rb +405 -202
- data/examples/sample_calc/calc.atree.tab.rb +243 -69
- data/examples/sample_calc/calc.cst.tab.rb +275 -109
- data/examples/sample_calc/calc.lex.tab.rb +210 -28
- data/examples/sample_calc/calc.nvaction.tab.rb +251 -77
- data/examples/sample_calc/calc.tab.rb +210 -28
- data/examples/sample_calc/calc_prec.nvaction.tab.rb +224 -50
- data/examples/slex_test/divreg.slex.tab.rb +97 -21
- data/examples/slex_test/ljoin.slex.tab.rb +128 -35
- data/lib/depager.rb +77 -44
- data/lib/depager/{ast_base.dr → ast.dr} +56 -18
- data/lib/depager/{ast_base.rb → ast.rb} +432 -424
- data/lib/depager/astdf.rb +3 -6
- data/lib/depager/atree.rb +54 -62
- data/lib/depager/cst.dr +2 -2
- data/lib/depager/cst.rb +64 -77
- data/lib/depager/grammar.rb +225 -66
- data/lib/depager/lex.dr +1 -1
- data/lib/depager/lex.rb +45 -54
- data/lib/depager/lr.rb +181 -262
- data/lib/depager/lr_put_table.rb +116 -0
- data/lib/depager/nvaction.rb +1 -1
- data/lib/depager/parser.rb +23 -2
- data/lib/depager/slex.dr +3 -3
- data/lib/depager/slex.rb +148 -169
- data/lib/depager/srp.rb +1 -1
- data/lib/depager/template/ast.erbs +69 -0
- data/lib/depager/template/extension_lalr_master.erb +3 -3
- data/lib/depager/template/extension_lalr_slave.erb +7 -7
- data/lib/depager/template/simple.erb +4 -2
- data/lib/depager/template/single_lalr_parser.erb +30 -10
- data/lib/depager/utils.rb +10 -9
- data/lib/depager/version.rb +2 -8
- metadata +10 -11
- data/examples/sample_calc/calc.astl.action.tab.rb +0 -593
- data/examples/sample_calc/calc.astl.tab.rb +0 -501
- data/lib/depager/astl.rb +0 -14
- data/lib/depager/template/astdf.erbs +0 -57
- data/lib/depager/template/astl.erbs +0 -57
@@ -1,501 +0,0 @@
|
|
1
|
-
|
2
|
-
begin; require 'rubygems'; rescue Exception; end
|
3
|
-
require 'depager/parser.rb'
|
4
|
-
|
5
|
-
|
6
|
-
module D4TinyCalc
|
7
|
-
end
|
8
|
-
|
9
|
-
class TinyCalc < Depager::LALR::Basis
|
10
|
-
|
11
|
-
|
12
|
-
### Reduce Table
|
13
|
-
reduce_table = [
|
14
|
-
[ -1, 1 ], # ( 0) $start : expr
|
15
|
-
[ 0, 3 ], # ( 1) expr : expr + term
|
16
|
-
[ 0, 3 ], # ( 2) expr : expr - term
|
17
|
-
[ 0, 1 ], # ( 3) expr : term
|
18
|
-
[ 1, 3 ], # ( 4) term : term * fact
|
19
|
-
[ 1, 3 ], # ( 5) term : term / fact
|
20
|
-
[ 1, 1 ], # ( 6) term : fact
|
21
|
-
[ 2, 1 ], # ( 7) fact : NUM
|
22
|
-
[ 2, 3 ], # ( 8) fact : ( expr )
|
23
|
-
]
|
24
|
-
### Extension Params
|
25
|
-
nparams = {
|
26
|
-
'ASTBuilderLazy' => 2,
|
27
|
-
}
|
28
|
-
### Term to Int
|
29
|
-
t2i = {
|
30
|
-
nil => 0,
|
31
|
-
false => 1,
|
32
|
-
"+" => 2,
|
33
|
-
"-" => 3,
|
34
|
-
"*" => 4,
|
35
|
-
"/" => 5,
|
36
|
-
:NUM => 6,
|
37
|
-
"(" => 7,
|
38
|
-
")" => 8,
|
39
|
-
}
|
40
|
-
### Int to Term
|
41
|
-
i2t = [
|
42
|
-
nil,
|
43
|
-
false,
|
44
|
-
"+",
|
45
|
-
"-",
|
46
|
-
"*",
|
47
|
-
"/",
|
48
|
-
:NUM,
|
49
|
-
"(",
|
50
|
-
")",
|
51
|
-
]
|
52
|
-
### Action Table
|
53
|
-
action_table = [
|
54
|
-
[ nil, nil, nil, nil, nil, nil, 5, 1, nil, ],
|
55
|
-
[ nil, nil, nil, nil, nil, nil, 5, 1, nil, ],
|
56
|
-
[ ACC, nil, 7, 8, nil, nil, nil, nil, nil, ],
|
57
|
-
[ nil, nil, nil, nil, 9, 10, nil, nil, nil, ],
|
58
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
59
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
60
|
-
[ nil, nil, 7, 8, nil, nil, nil, nil, 11, ],
|
61
|
-
[ nil, nil, nil, nil, nil, nil, 5, 1, nil, ],
|
62
|
-
[ nil, nil, nil, nil, nil, nil, 5, 1, nil, ],
|
63
|
-
[ nil, nil, nil, nil, nil, nil, 5, 1, nil, ],
|
64
|
-
[ nil, nil, nil, nil, nil, nil, 5, 1, nil, ],
|
65
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
66
|
-
[ nil, nil, nil, nil, 9, 10, nil, nil, nil, ],
|
67
|
-
[ nil, nil, nil, nil, 9, 10, nil, nil, nil, ],
|
68
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
69
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
70
|
-
]
|
71
|
-
### Default Reduce Table
|
72
|
-
defred_table = [
|
73
|
-
nil,
|
74
|
-
nil,
|
75
|
-
nil,
|
76
|
-
-3,
|
77
|
-
-6,
|
78
|
-
-7,
|
79
|
-
nil,
|
80
|
-
nil,
|
81
|
-
nil,
|
82
|
-
nil,
|
83
|
-
nil,
|
84
|
-
-8,
|
85
|
-
-1,
|
86
|
-
-2,
|
87
|
-
-4,
|
88
|
-
-5,
|
89
|
-
]
|
90
|
-
defred_after_shift_table = [
|
91
|
-
nil,
|
92
|
-
nil,
|
93
|
-
nil,
|
94
|
-
nil,
|
95
|
-
-6,
|
96
|
-
-7,
|
97
|
-
nil,
|
98
|
-
nil,
|
99
|
-
nil,
|
100
|
-
nil,
|
101
|
-
nil,
|
102
|
-
-8,
|
103
|
-
nil,
|
104
|
-
nil,
|
105
|
-
-4,
|
106
|
-
-5,
|
107
|
-
]
|
108
|
-
### Nonterm to Int
|
109
|
-
nt2i = {
|
110
|
-
:expr => 0,
|
111
|
-
:term => 1,
|
112
|
-
:fact => 2,
|
113
|
-
}
|
114
|
-
### Int to Nonterm
|
115
|
-
i2nt = [
|
116
|
-
:expr,
|
117
|
-
:term,
|
118
|
-
:fact,
|
119
|
-
]
|
120
|
-
### Goto Table
|
121
|
-
goto_table = [
|
122
|
-
[ 2, 3, 4, ],
|
123
|
-
[ 6, 3, 4, ],
|
124
|
-
[ nil, nil, nil, ],
|
125
|
-
[ nil, nil, nil, ],
|
126
|
-
[ nil, nil, nil, ],
|
127
|
-
[ nil, nil, nil, ],
|
128
|
-
[ nil, nil, nil, ],
|
129
|
-
[ nil, 12, 4, ],
|
130
|
-
[ nil, 13, 4, ],
|
131
|
-
[ nil, nil, 14, ],
|
132
|
-
[ nil, nil, 15, ],
|
133
|
-
[ nil, nil, nil, ],
|
134
|
-
[ nil, nil, nil, ],
|
135
|
-
[ nil, nil, nil, ],
|
136
|
-
[ nil, nil, nil, ],
|
137
|
-
[ nil, nil, nil, ],
|
138
|
-
]
|
139
|
-
|
140
|
-
Tables = [ reduce_table, nparams, action_table,
|
141
|
-
defred_table, defred_after_shift_table, goto_table,
|
142
|
-
t2i, i2t, nt2i, i2nt ]
|
143
|
-
|
144
|
-
def self.createDecoratedParser
|
145
|
-
D4TinyCalc::ASTBuilderLazy.new(TinyCalc.new())
|
146
|
-
end
|
147
|
-
|
148
|
-
### Inner Code
|
149
|
-
|
150
|
-
def lex
|
151
|
-
begin
|
152
|
-
until @line.empty?
|
153
|
-
case @line
|
154
|
-
|
155
|
-
when /\A\s+/, /\A\#.*/
|
156
|
-
@oldline = @line; @line = $'
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
when /\A[1-9][0-9]*/
|
161
|
-
@oldline = @line; @line = $'
|
162
|
-
yield _Token(:NUM, $&.to_i)
|
163
|
-
|
164
|
-
|
165
|
-
when /\A./
|
166
|
-
@oldline = @line; @line = $'
|
167
|
-
yield _Token($&, $&)
|
168
|
-
|
169
|
-
|
170
|
-
else
|
171
|
-
raise RuntimeError, "must not happen #{@line}"
|
172
|
-
end
|
173
|
-
end
|
174
|
-
end while @line0 = @line = getline
|
175
|
-
yield nil, nil
|
176
|
-
end
|
177
|
-
|
178
|
-
end
|
179
|
-
|
180
|
-
### Outer Code
|
181
|
-
|
182
|
-
class Node
|
183
|
-
attr_accessor :lineno
|
184
|
-
attr_accessor :value
|
185
|
-
|
186
|
-
def initialize
|
187
|
-
@value = nil
|
188
|
-
|
189
|
-
end
|
190
|
-
|
191
|
-
def to_ary
|
192
|
-
[self]
|
193
|
-
end
|
194
|
-
def self.[] lineno, *args
|
195
|
-
self.new lineno, *args
|
196
|
-
end
|
197
|
-
def accept v
|
198
|
-
end
|
199
|
-
end
|
200
|
-
require 'forwardable'
|
201
|
-
class NodeList < Node
|
202
|
-
attr_accessor :lineno
|
203
|
-
extend Forwardable
|
204
|
-
_methods = Array.instance_methods -
|
205
|
-
Object.instance_methods -
|
206
|
-
['push', 'concat', 'to_ary', 'map']
|
207
|
-
def_delegators(:@lst, *_methods)
|
208
|
-
def initialize(lineno, lst=[])
|
209
|
-
@lineno = lineno
|
210
|
-
@lst = lst.to_a
|
211
|
-
end
|
212
|
-
|
213
|
-
def value
|
214
|
-
@lst.map{|i| "i.value"}
|
215
|
-
end
|
216
|
-
|
217
|
-
def push(i) @lst.push i; self; end
|
218
|
-
def concat(i) @lst.concat i; self; end
|
219
|
-
def map(&b) @lst.map(&b); end
|
220
|
-
def to_ary() @lst; end
|
221
|
-
def accept(v)
|
222
|
-
@lst.each{|i| i.accept(v) }
|
223
|
-
end
|
224
|
-
end
|
225
|
-
|
226
|
-
class Node_add < Node
|
227
|
-
attr_accessor :left, :right
|
228
|
-
attr_accessor
|
229
|
-
def initialize lineno, left, right
|
230
|
-
super()
|
231
|
-
@lineno = lineno
|
232
|
-
@lineno = lineno
|
233
|
-
@left = left
|
234
|
-
@right = right
|
235
|
-
|
236
|
-
end
|
237
|
-
|
238
|
-
def _left_ v
|
239
|
-
@left.accept(v); @left
|
240
|
-
end
|
241
|
-
|
242
|
-
def _right_ v
|
243
|
-
@right.accept(v); @right
|
244
|
-
end
|
245
|
-
|
246
|
-
def accept v
|
247
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
248
|
-
v.visit_Node_add(self)
|
249
|
-
self
|
250
|
-
end
|
251
|
-
end
|
252
|
-
|
253
|
-
class Node_sub < Node
|
254
|
-
attr_accessor :left, :right
|
255
|
-
attr_accessor
|
256
|
-
def initialize lineno, left, right
|
257
|
-
super()
|
258
|
-
@lineno = lineno
|
259
|
-
@lineno = lineno
|
260
|
-
@left = left
|
261
|
-
@right = right
|
262
|
-
|
263
|
-
end
|
264
|
-
|
265
|
-
def _left_ v
|
266
|
-
@left.accept(v); @left
|
267
|
-
end
|
268
|
-
|
269
|
-
def _right_ v
|
270
|
-
@right.accept(v); @right
|
271
|
-
end
|
272
|
-
|
273
|
-
def accept v
|
274
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
275
|
-
v.visit_Node_sub(self)
|
276
|
-
self
|
277
|
-
end
|
278
|
-
end
|
279
|
-
|
280
|
-
class Node_mul < Node
|
281
|
-
attr_accessor :left, :right
|
282
|
-
attr_accessor
|
283
|
-
def initialize lineno, left, right
|
284
|
-
super()
|
285
|
-
@lineno = lineno
|
286
|
-
@lineno = lineno
|
287
|
-
@left = left
|
288
|
-
@right = right
|
289
|
-
|
290
|
-
end
|
291
|
-
|
292
|
-
def _left_ v
|
293
|
-
@left.accept(v); @left
|
294
|
-
end
|
295
|
-
|
296
|
-
def _right_ v
|
297
|
-
@right.accept(v); @right
|
298
|
-
end
|
299
|
-
|
300
|
-
def accept v
|
301
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
302
|
-
v.visit_Node_mul(self)
|
303
|
-
self
|
304
|
-
end
|
305
|
-
end
|
306
|
-
|
307
|
-
class Node_div < Node
|
308
|
-
attr_accessor :left, :right
|
309
|
-
attr_accessor
|
310
|
-
def initialize lineno, left, right
|
311
|
-
super()
|
312
|
-
@lineno = lineno
|
313
|
-
@lineno = lineno
|
314
|
-
@left = left
|
315
|
-
@right = right
|
316
|
-
|
317
|
-
end
|
318
|
-
|
319
|
-
def _left_ v
|
320
|
-
@left.accept(v); @left
|
321
|
-
end
|
322
|
-
|
323
|
-
def _right_ v
|
324
|
-
@right.accept(v); @right
|
325
|
-
end
|
326
|
-
|
327
|
-
def accept v
|
328
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
329
|
-
v.visit_Node_div(self)
|
330
|
-
self
|
331
|
-
end
|
332
|
-
end
|
333
|
-
|
334
|
-
class Node_literal < Node
|
335
|
-
attr_accessor :n
|
336
|
-
attr_accessor :n
|
337
|
-
def initialize lineno, n
|
338
|
-
super()
|
339
|
-
@lineno = lineno
|
340
|
-
@lineno = lineno
|
341
|
-
@n = n
|
342
|
-
|
343
|
-
end
|
344
|
-
|
345
|
-
def accept v
|
346
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
347
|
-
v.visit_Node_literal(self)
|
348
|
-
self
|
349
|
-
end
|
350
|
-
end
|
351
|
-
|
352
|
-
class Visitor
|
353
|
-
def visit node
|
354
|
-
node.accept(self)
|
355
|
-
end
|
356
|
-
|
357
|
-
module_eval <<-'.,.,118754108932728.,.,', 'sample_calc/calc.astl.dr', 16
|
358
|
-
|
359
|
-
|
360
|
-
.,.,118754108932728.,.,
|
361
|
-
|
362
|
-
module_eval <<-'.,.,118754108939722.,.,', 'sample_calc/calc.astl.dr', 16
|
363
|
-
def visit_Node_add node
|
364
|
-
node.value = visit(node.left).value + visit(node.right).value
|
365
|
-
|
366
|
-
end
|
367
|
-
|
368
|
-
.,.,118754108939722.,.,
|
369
|
-
|
370
|
-
module_eval <<-'.,.,118754108923127.,.,', 'sample_calc/calc.astl.dr', 17
|
371
|
-
def visit_Node_sub node
|
372
|
-
node.value = visit(node.left).value - visit(node.right).value
|
373
|
-
|
374
|
-
end
|
375
|
-
|
376
|
-
.,.,118754108923127.,.,
|
377
|
-
|
378
|
-
module_eval <<-'.,.,118754108917684.,.,', 'sample_calc/calc.astl.dr', 18
|
379
|
-
def visit_Node_mul node
|
380
|
-
node.value = visit(node.left).value * visit(node.right).value
|
381
|
-
|
382
|
-
end
|
383
|
-
|
384
|
-
.,.,118754108917684.,.,
|
385
|
-
|
386
|
-
module_eval <<-'.,.,11875410891729.,.,', 'sample_calc/calc.astl.dr', 19
|
387
|
-
def visit_Node_div node
|
388
|
-
node.value = visit(node.left).value / visit(node.right).value
|
389
|
-
|
390
|
-
end
|
391
|
-
|
392
|
-
.,.,11875410891729.,.,
|
393
|
-
|
394
|
-
module_eval <<-'.,.,118754108945236.,.,', 'sample_calc/calc.astl.dr', 20
|
395
|
-
def visit_Node_literal node
|
396
|
-
node.value = node.n.value
|
397
|
-
|
398
|
-
end
|
399
|
-
|
400
|
-
.,.,118754108945236.,.,
|
401
|
-
|
402
|
-
end
|
403
|
-
|
404
|
-
class D4TinyCalc::ASTBuilderLazy < Depager::LALR::Action #:nodoc:all
|
405
|
-
include Depager::DecoratorUtils
|
406
|
-
|
407
|
-
on_reduce = [
|
408
|
-
nil,
|
409
|
-
:_ast_0,
|
410
|
-
:_ast_1,
|
411
|
-
:_ast_2,
|
412
|
-
:_ast_3,
|
413
|
-
:_ast_4,
|
414
|
-
:_ast_5,
|
415
|
-
:_ast_6,
|
416
|
-
:_ast_7,
|
417
|
-
|
418
|
-
]
|
419
|
-
Tables = [on_reduce]
|
420
|
-
def initialize inside
|
421
|
-
super inside, 'ASTBuilderLazy'
|
422
|
-
@on_reduce, = self.class::Tables
|
423
|
-
init_parser
|
424
|
-
end
|
425
|
-
|
426
|
-
|
427
|
-
module_eval <<-'.,.,118754108915892.,.,', 'sample_calc/calc.astl.dr', 26
|
428
|
-
def _ast_0 val
|
429
|
-
Node_add.new(val[0].lineno, val[0], val[2])
|
430
|
-
end
|
431
|
-
|
432
|
-
.,.,118754108915892.,.,
|
433
|
-
|
434
|
-
module_eval <<-'.,.,1187541089554.,.,', 'sample_calc/calc.astl.dr', 28
|
435
|
-
def _ast_1 val
|
436
|
-
Node_sub.new(val[0].lineno, val[0], val[2])
|
437
|
-
end
|
438
|
-
|
439
|
-
.,.,1187541089554.,.,
|
440
|
-
|
441
|
-
module_eval <<-'.,.,118754108932030.,.,', 'sample_calc/calc.astl.dr', 30
|
442
|
-
def _ast_2 val
|
443
|
-
val[0]
|
444
|
-
end
|
445
|
-
|
446
|
-
.,.,118754108932030.,.,
|
447
|
-
|
448
|
-
module_eval <<-'.,.,118754108931351.,.,', 'sample_calc/calc.astl.dr', 34
|
449
|
-
def _ast_3 val
|
450
|
-
Node_mul.new(val[0].lineno, val[0], val[2])
|
451
|
-
end
|
452
|
-
|
453
|
-
.,.,118754108931351.,.,
|
454
|
-
|
455
|
-
module_eval <<-'.,.,118754108946910.,.,', 'sample_calc/calc.astl.dr', 36
|
456
|
-
def _ast_4 val
|
457
|
-
Node_div.new(val[0].lineno, val[0], val[2])
|
458
|
-
end
|
459
|
-
|
460
|
-
.,.,118754108946910.,.,
|
461
|
-
|
462
|
-
module_eval <<-'.,.,118754108946476.,.,', 'sample_calc/calc.astl.dr', 38
|
463
|
-
def _ast_5 val
|
464
|
-
val[0]
|
465
|
-
end
|
466
|
-
|
467
|
-
.,.,118754108946476.,.,
|
468
|
-
|
469
|
-
module_eval <<-'.,.,118754108958355.,.,', 'sample_calc/calc.astl.dr', 42
|
470
|
-
def _ast_6 val
|
471
|
-
Node_literal.new(val[0].lineno, val[0])
|
472
|
-
end
|
473
|
-
|
474
|
-
.,.,118754108958355.,.,
|
475
|
-
|
476
|
-
module_eval <<-'.,.,118754108955792.,.,', 'sample_calc/calc.astl.dr', 44
|
477
|
-
def _ast_7 val
|
478
|
-
val[1]
|
479
|
-
end
|
480
|
-
|
481
|
-
.,.,118754108955792.,.,
|
482
|
-
|
483
|
-
end
|
484
|
-
|
485
|
-
|
486
|
-
def createDecoratedTinyCalc
|
487
|
-
D4TinyCalc::ASTBuilderLazy.new(TinyCalc.new())
|
488
|
-
end
|
489
|
-
|
490
|
-
### main
|
491
|
-
if __FILE__ == $0
|
492
|
-
### Main Code
|
493
|
-
require 'pp'
|
494
|
-
parser = createDecoratedTinyCalc()
|
495
|
-
t, = parser.yyparse(STDIN)
|
496
|
-
v = Visitor.new
|
497
|
-
pp t.accept(v).value
|
498
|
-
pp t
|
499
|
-
|
500
|
-
|
501
|
-
end
|