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
@@ -1,749 +0,0 @@
|
|
1
|
-
|
2
|
-
begin; require 'rubygems'; rescue Exception; end
|
3
|
-
require 'depager/parser.rb'
|
4
|
-
|
5
|
-
|
6
|
-
module Depager::DecoratorUtils; end
|
7
|
-
module D4TinyCalc; end
|
8
|
-
|
9
|
-
class TinyCalc < Depager::LALR::Basis
|
10
|
-
|
11
|
-
include Depager
|
12
|
-
|
13
|
-
### Reduce Table
|
14
|
-
reduce_table = [
|
15
|
-
[ -1, 1 ], # ( 0) $start : expr
|
16
|
-
[ 0, 3 ], # ( 1) expr : expr '+' term
|
17
|
-
[ 0, 3 ], # ( 2) expr : expr '-' term
|
18
|
-
[ 0, 1 ], # ( 3) expr : term
|
19
|
-
[ 1, 3 ], # ( 4) term : term '*' fact
|
20
|
-
[ 1, 3 ], # ( 5) term : term '/' fact
|
21
|
-
[ 1, 1 ], # ( 6) term : fact
|
22
|
-
[ 2, 1 ], # ( 7) fact : NUM
|
23
|
-
[ 2, 3 ], # ( 8) fact : '(' expr ')'
|
24
|
-
]
|
25
|
-
### Extension Params
|
26
|
-
nparams = {
|
27
|
-
'Action' => 3,
|
28
|
-
'ASTBuilder' => 2,
|
29
|
-
}
|
30
|
-
### Term to Int
|
31
|
-
t2i = {
|
32
|
-
nil => 0,
|
33
|
-
false => 1,
|
34
|
-
"+" => 2,
|
35
|
-
"-" => 3,
|
36
|
-
"*" => 4,
|
37
|
-
"/" => 5,
|
38
|
-
:NUM => 6,
|
39
|
-
"(" => 7,
|
40
|
-
")" => 8,
|
41
|
-
}
|
42
|
-
### Int to Term
|
43
|
-
i2t = [
|
44
|
-
nil,
|
45
|
-
false,
|
46
|
-
"+",
|
47
|
-
"-",
|
48
|
-
"*",
|
49
|
-
"/",
|
50
|
-
:NUM,
|
51
|
-
"(",
|
52
|
-
")",
|
53
|
-
]
|
54
|
-
### Action Table
|
55
|
-
action_table = [
|
56
|
-
[ nil, nil, nil, nil, nil, nil, 5, 1, nil, ],
|
57
|
-
[ nil, nil, nil, nil, nil, nil, 5, 1, nil, ],
|
58
|
-
[ ACC, nil, 7, 8, nil, nil, nil, nil, nil, ],
|
59
|
-
[ nil, nil, nil, nil, 9, 10, nil, nil, nil, ],
|
60
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
61
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
62
|
-
[ nil, nil, 7, 8, nil, nil, nil, nil, 11, ],
|
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, 5, 1, nil, ],
|
66
|
-
[ nil, nil, nil, nil, nil, nil, 5, 1, nil, ],
|
67
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
68
|
-
[ nil, nil, nil, nil, 9, 10, nil, nil, nil, ],
|
69
|
-
[ nil, nil, nil, nil, 9, 10, nil, nil, nil, ],
|
70
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
71
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
72
|
-
]
|
73
|
-
### Default Reduce Table
|
74
|
-
defred_table = [
|
75
|
-
nil,
|
76
|
-
nil,
|
77
|
-
nil,
|
78
|
-
-3,
|
79
|
-
-6,
|
80
|
-
-7,
|
81
|
-
nil,
|
82
|
-
nil,
|
83
|
-
nil,
|
84
|
-
nil,
|
85
|
-
nil,
|
86
|
-
-8,
|
87
|
-
-1,
|
88
|
-
-2,
|
89
|
-
-4,
|
90
|
-
-5,
|
91
|
-
]
|
92
|
-
defred_after_shift_table = [
|
93
|
-
nil,
|
94
|
-
nil,
|
95
|
-
nil,
|
96
|
-
nil,
|
97
|
-
-6,
|
98
|
-
-7,
|
99
|
-
nil,
|
100
|
-
nil,
|
101
|
-
nil,
|
102
|
-
nil,
|
103
|
-
nil,
|
104
|
-
-8,
|
105
|
-
nil,
|
106
|
-
nil,
|
107
|
-
-4,
|
108
|
-
-5,
|
109
|
-
]
|
110
|
-
### Nonterm to Int
|
111
|
-
nt2i = {
|
112
|
-
:expr => 0,
|
113
|
-
:term => 1,
|
114
|
-
:fact => 2,
|
115
|
-
}
|
116
|
-
### Int to Nonterm
|
117
|
-
i2nt = [
|
118
|
-
:expr,
|
119
|
-
:term,
|
120
|
-
:fact,
|
121
|
-
]
|
122
|
-
### Goto Table
|
123
|
-
goto_table = [
|
124
|
-
[ 2, 3, 4, ],
|
125
|
-
[ 6, 3, 4, ],
|
126
|
-
[ nil, nil, nil, ],
|
127
|
-
[ nil, nil, nil, ],
|
128
|
-
[ nil, nil, nil, ],
|
129
|
-
[ nil, nil, nil, ],
|
130
|
-
[ nil, nil, nil, ],
|
131
|
-
[ nil, 12, 4, ],
|
132
|
-
[ nil, 13, 4, ],
|
133
|
-
[ nil, nil, 14, ],
|
134
|
-
[ nil, nil, 15, ],
|
135
|
-
[ nil, nil, nil, ],
|
136
|
-
[ nil, nil, nil, ],
|
137
|
-
[ nil, nil, nil, ],
|
138
|
-
[ nil, nil, nil, ],
|
139
|
-
[ nil, nil, nil, ],
|
140
|
-
]
|
141
|
-
|
142
|
-
|
143
|
-
alias orig_error error
|
144
|
-
def error
|
145
|
-
orig_error
|
146
|
-
warn "current state: #{StateInfo[@stack.last]}"
|
147
|
-
end
|
148
|
-
|
149
|
-
### States
|
150
|
-
StateInfo = [
|
151
|
-
<<'----------',
|
152
|
-
I000 =
|
153
|
-
( 0) $start : _ expr
|
154
|
-
|
155
|
-
NUM shift, and goto to state 5
|
156
|
-
'(' shift, and goto to state 1
|
157
|
-
|
158
|
-
|
159
|
-
----------
|
160
|
-
|
161
|
-
<<'----------',
|
162
|
-
I001 =
|
163
|
-
( 8) fact : '(' _ expr ')'
|
164
|
-
|
165
|
-
NUM shift, and goto to state 5
|
166
|
-
'(' shift, and goto to state 1
|
167
|
-
|
168
|
-
|
169
|
-
----------
|
170
|
-
|
171
|
-
<<'----------',
|
172
|
-
I002 =
|
173
|
-
( 0) $start : expr _
|
174
|
-
( 1) expr : expr _ '+' term
|
175
|
-
( 2) expr : expr _ '-' term
|
176
|
-
|
177
|
-
'+' shift, and goto to state 7
|
178
|
-
'-' shift, and goto to state 8
|
179
|
-
|
180
|
-
|
181
|
-
$end accept
|
182
|
-
|
183
|
-
----------
|
184
|
-
|
185
|
-
<<'----------',
|
186
|
-
I003 =
|
187
|
-
( 3) expr : term _
|
188
|
-
( 4) term : term _ '*' fact
|
189
|
-
( 5) term : term _ '/' fact
|
190
|
-
|
191
|
-
'*' shift, and goto to state 9
|
192
|
-
'/' shift, and goto to state 10
|
193
|
-
|
194
|
-
$default reduce using rule 3 (expr)
|
195
|
-
|
196
|
-
----------
|
197
|
-
|
198
|
-
<<'----------',
|
199
|
-
I004 =
|
200
|
-
( 6) term : fact _
|
201
|
-
|
202
|
-
|
203
|
-
$default reduce using rule 6 (term) [after shift]
|
204
|
-
|
205
|
-
----------
|
206
|
-
|
207
|
-
<<'----------',
|
208
|
-
I005 =
|
209
|
-
( 7) fact : NUM _
|
210
|
-
|
211
|
-
|
212
|
-
$default reduce using rule 7 (fact) [after shift]
|
213
|
-
|
214
|
-
----------
|
215
|
-
|
216
|
-
<<'----------',
|
217
|
-
I006 =
|
218
|
-
( 8) fact : '(' expr _ ')'
|
219
|
-
( 1) expr : expr _ '+' term
|
220
|
-
( 2) expr : expr _ '-' term
|
221
|
-
|
222
|
-
'+' shift, and goto to state 7
|
223
|
-
'-' shift, and goto to state 8
|
224
|
-
')' shift, and goto to state 11
|
225
|
-
|
226
|
-
|
227
|
-
----------
|
228
|
-
|
229
|
-
<<'----------',
|
230
|
-
I007 =
|
231
|
-
( 1) expr : expr '+' _ term
|
232
|
-
|
233
|
-
NUM shift, and goto to state 5
|
234
|
-
'(' shift, and goto to state 1
|
235
|
-
|
236
|
-
|
237
|
-
----------
|
238
|
-
|
239
|
-
<<'----------',
|
240
|
-
I008 =
|
241
|
-
( 2) expr : expr '-' _ term
|
242
|
-
|
243
|
-
NUM shift, and goto to state 5
|
244
|
-
'(' shift, and goto to state 1
|
245
|
-
|
246
|
-
|
247
|
-
----------
|
248
|
-
|
249
|
-
<<'----------',
|
250
|
-
I009 =
|
251
|
-
( 4) term : term '*' _ fact
|
252
|
-
|
253
|
-
NUM shift, and goto to state 5
|
254
|
-
'(' shift, and goto to state 1
|
255
|
-
|
256
|
-
|
257
|
-
----------
|
258
|
-
|
259
|
-
<<'----------',
|
260
|
-
I010 =
|
261
|
-
( 5) term : term '/' _ fact
|
262
|
-
|
263
|
-
NUM shift, and goto to state 5
|
264
|
-
'(' shift, and goto to state 1
|
265
|
-
|
266
|
-
|
267
|
-
----------
|
268
|
-
|
269
|
-
<<'----------',
|
270
|
-
I011 =
|
271
|
-
( 8) fact : '(' expr ')' _
|
272
|
-
|
273
|
-
|
274
|
-
$default reduce using rule 8 (fact) [after shift]
|
275
|
-
|
276
|
-
----------
|
277
|
-
|
278
|
-
<<'----------',
|
279
|
-
I012 =
|
280
|
-
( 1) expr : expr '+' term _
|
281
|
-
( 4) term : term _ '*' fact
|
282
|
-
( 5) term : term _ '/' fact
|
283
|
-
|
284
|
-
'*' shift, and goto to state 9
|
285
|
-
'/' shift, and goto to state 10
|
286
|
-
|
287
|
-
$default reduce using rule 1 (expr)
|
288
|
-
|
289
|
-
----------
|
290
|
-
|
291
|
-
<<'----------',
|
292
|
-
I013 =
|
293
|
-
( 2) expr : expr '-' term _
|
294
|
-
( 4) term : term _ '*' fact
|
295
|
-
( 5) term : term _ '/' fact
|
296
|
-
|
297
|
-
'*' shift, and goto to state 9
|
298
|
-
'/' shift, and goto to state 10
|
299
|
-
|
300
|
-
$default reduce using rule 2 (expr)
|
301
|
-
|
302
|
-
----------
|
303
|
-
|
304
|
-
<<'----------',
|
305
|
-
I014 =
|
306
|
-
( 4) term : term '*' fact _
|
307
|
-
|
308
|
-
|
309
|
-
$default reduce using rule 4 (term) [after shift]
|
310
|
-
|
311
|
-
----------
|
312
|
-
|
313
|
-
<<'----------',
|
314
|
-
I015 =
|
315
|
-
( 5) term : term '/' fact _
|
316
|
-
|
317
|
-
|
318
|
-
$default reduce using rule 5 (term) [after shift]
|
319
|
-
|
320
|
-
----------
|
321
|
-
]
|
322
|
-
|
323
|
-
|
324
|
-
Tables = [ reduce_table, nparams, action_table,
|
325
|
-
defred_table, defred_after_shift_table, goto_table,
|
326
|
-
t2i, i2t, nt2i, i2nt ]
|
327
|
-
|
328
|
-
def self.createDecoratedParser
|
329
|
-
D4TinyCalc::Action.new(D4TinyCalc::ASTBuilder.new(TinyCalc.new()))
|
330
|
-
end
|
331
|
-
|
332
|
-
### Inner Code
|
333
|
-
|
334
|
-
def lex
|
335
|
-
begin
|
336
|
-
until @line.empty?
|
337
|
-
case @line
|
338
|
-
|
339
|
-
when /\A\s+/, /\A\#.*/, /\A\n/
|
340
|
-
@oldline = @line; @line = $'
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
when /\A[1-9][0-9]*/
|
345
|
-
@oldline = @line; @line = $'
|
346
|
-
yield token(:NUM, $&.to_i)
|
347
|
-
|
348
|
-
|
349
|
-
when /\A./
|
350
|
-
@oldline = @line; @line = $'
|
351
|
-
yield token($&, $&)
|
352
|
-
|
353
|
-
|
354
|
-
else
|
355
|
-
raise RuntimeError, "must not happen #{@line}"
|
356
|
-
end
|
357
|
-
end
|
358
|
-
end while @line0 = @line = getline
|
359
|
-
yield nil, nil
|
360
|
-
end
|
361
|
-
|
362
|
-
end
|
363
|
-
|
364
|
-
### Outer Code
|
365
|
-
class Node
|
366
|
-
attr_accessor :lineno
|
367
|
-
attr_accessor :value
|
368
|
-
|
369
|
-
def initialize
|
370
|
-
@value = nil
|
371
|
-
|
372
|
-
end
|
373
|
-
|
374
|
-
def self.[] lineno, *args
|
375
|
-
self.new lineno, *args
|
376
|
-
end
|
377
|
-
def accept v
|
378
|
-
end
|
379
|
-
end
|
380
|
-
class NodeList < Node
|
381
|
-
attr_accessor :lineno
|
382
|
-
def initialize(lineno, lst=[])
|
383
|
-
@lineno = lineno
|
384
|
-
@lst = lst.to_a.select{|i| ! i.is_a?(NilNode)}
|
385
|
-
end
|
386
|
-
|
387
|
-
def all_value
|
388
|
-
@lst.map{|i| i.value}
|
389
|
-
end
|
390
|
-
|
391
|
-
def size
|
392
|
-
@lst.size
|
393
|
-
end
|
394
|
-
alias length size
|
395
|
-
def push(i)
|
396
|
-
@lst.push i unless i.is_a? NilNode
|
397
|
-
self
|
398
|
-
end
|
399
|
-
def concat(i)
|
400
|
-
@lst.concat i
|
401
|
-
self
|
402
|
-
end
|
403
|
-
def to_ary()
|
404
|
-
@lst
|
405
|
-
end
|
406
|
-
alias to_a to_ary
|
407
|
-
alias list to_ary
|
408
|
-
def accept(v)
|
409
|
-
@lst.each{|i| i.accept(v) }
|
410
|
-
end
|
411
|
-
end
|
412
|
-
class NilNode
|
413
|
-
attr_accessor :lineno
|
414
|
-
attr_accessor :value
|
415
|
-
def initialize(lineno, *args)
|
416
|
-
@lineno = lineno
|
417
|
-
end
|
418
|
-
def accept v
|
419
|
-
end
|
420
|
-
end
|
421
|
-
class Node_add < Node
|
422
|
-
attr_accessor :left, :right
|
423
|
-
attr_accessor
|
424
|
-
def initialize lineno, left, right
|
425
|
-
super()
|
426
|
-
@lineno = lineno
|
427
|
-
@lineno = lineno
|
428
|
-
@left = left
|
429
|
-
@right = right
|
430
|
-
|
431
|
-
end
|
432
|
-
def accept v
|
433
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
434
|
-
|
435
|
-
v.visit_Node_add(self)
|
436
|
-
self
|
437
|
-
end
|
438
|
-
end
|
439
|
-
class Node_sub < Node
|
440
|
-
attr_accessor :left, :right
|
441
|
-
attr_accessor
|
442
|
-
def initialize lineno, left, right
|
443
|
-
super()
|
444
|
-
@lineno = lineno
|
445
|
-
@lineno = lineno
|
446
|
-
@left = left
|
447
|
-
@right = right
|
448
|
-
|
449
|
-
end
|
450
|
-
def accept v
|
451
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
452
|
-
|
453
|
-
v.visit_Node_sub(self)
|
454
|
-
self
|
455
|
-
end
|
456
|
-
end
|
457
|
-
class Node_mul < Node
|
458
|
-
attr_accessor :left, :right
|
459
|
-
attr_accessor
|
460
|
-
def initialize lineno, left, right
|
461
|
-
super()
|
462
|
-
@lineno = lineno
|
463
|
-
@lineno = lineno
|
464
|
-
@left = left
|
465
|
-
@right = right
|
466
|
-
|
467
|
-
end
|
468
|
-
def accept v
|
469
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
470
|
-
|
471
|
-
v.visit_Node_mul(self)
|
472
|
-
self
|
473
|
-
end
|
474
|
-
end
|
475
|
-
class Node_div < Node
|
476
|
-
attr_accessor :left, :right
|
477
|
-
attr_accessor
|
478
|
-
def initialize lineno, left, right
|
479
|
-
super()
|
480
|
-
@lineno = lineno
|
481
|
-
@lineno = lineno
|
482
|
-
@left = left
|
483
|
-
@right = right
|
484
|
-
|
485
|
-
end
|
486
|
-
def accept v
|
487
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
488
|
-
|
489
|
-
v.visit_Node_div(self)
|
490
|
-
self
|
491
|
-
end
|
492
|
-
end
|
493
|
-
class Node_literal < Node
|
494
|
-
attr_accessor :n
|
495
|
-
attr_accessor :n
|
496
|
-
def initialize lineno, n
|
497
|
-
super()
|
498
|
-
@lineno = lineno
|
499
|
-
@lineno = lineno
|
500
|
-
@n = n
|
501
|
-
|
502
|
-
end
|
503
|
-
def accept v
|
504
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
505
|
-
|
506
|
-
v.visit_Node_literal(self)
|
507
|
-
self
|
508
|
-
end
|
509
|
-
end
|
510
|
-
|
511
|
-
class Visitor
|
512
|
-
def visit node
|
513
|
-
node.accept(self)
|
514
|
-
end
|
515
|
-
|
516
|
-
module_eval <<-'.,.,122745803836640.,.,', 'sample_calc/calc.ast.action.dr', 19
|
517
|
-
|
518
|
-
|
519
|
-
.,.,122745803836640.,.,
|
520
|
-
|
521
|
-
module_eval <<-'.,.,122745803860945.,.,', 'sample_calc/calc.ast.action.dr', 19
|
522
|
-
def visit_Node_add node
|
523
|
-
node.value = visit(node.left).value + visit(node.right).value
|
524
|
-
|
525
|
-
rescue
|
526
|
-
warn "raise at src:#{node.lineno}/#{node.class.name}"
|
527
|
-
raise
|
528
|
-
|
529
|
-
end
|
530
|
-
.,.,122745803860945.,.,
|
531
|
-
|
532
|
-
module_eval <<-'.,.,122745803823377.,.,', 'sample_calc/calc.ast.action.dr', 20
|
533
|
-
def visit_Node_sub node
|
534
|
-
node.value = visit(node.left).value - visit(node.right).value
|
535
|
-
|
536
|
-
rescue
|
537
|
-
warn "raise at src:#{node.lineno}/#{node.class.name}"
|
538
|
-
raise
|
539
|
-
|
540
|
-
end
|
541
|
-
.,.,122745803823377.,.,
|
542
|
-
|
543
|
-
module_eval <<-'.,.,12274580385321.,.,', 'sample_calc/calc.ast.action.dr', 21
|
544
|
-
def visit_Node_mul node
|
545
|
-
node.value = visit(node.left).value * visit(node.right).value
|
546
|
-
|
547
|
-
rescue
|
548
|
-
warn "raise at src:#{node.lineno}/#{node.class.name}"
|
549
|
-
raise
|
550
|
-
|
551
|
-
end
|
552
|
-
.,.,12274580385321.,.,
|
553
|
-
|
554
|
-
module_eval <<-'.,.,12274580389451.,.,', 'sample_calc/calc.ast.action.dr', 22
|
555
|
-
def visit_Node_div node
|
556
|
-
node.value = visit(node.left).value / visit(node.right).value
|
557
|
-
|
558
|
-
rescue
|
559
|
-
warn "raise at src:#{node.lineno}/#{node.class.name}"
|
560
|
-
raise
|
561
|
-
|
562
|
-
end
|
563
|
-
.,.,12274580389451.,.,
|
564
|
-
|
565
|
-
module_eval <<-'.,.,122745803835690.,.,', 'sample_calc/calc.ast.action.dr', 23
|
566
|
-
def visit_Node_literal node
|
567
|
-
node.value = node.n.value
|
568
|
-
|
569
|
-
rescue
|
570
|
-
warn "raise at src:#{node.lineno}/#{node.class.name}"
|
571
|
-
raise
|
572
|
-
|
573
|
-
end
|
574
|
-
.,.,122745803835690.,.,
|
575
|
-
|
576
|
-
end
|
577
|
-
|
578
|
-
class D4TinyCalc::ASTBuilder < Depager::LALR::Action #:nodoc:all
|
579
|
-
include Depager::DecoratorUtils
|
580
|
-
|
581
|
-
on_reduce = [
|
582
|
-
nil,
|
583
|
-
:_ast_0,
|
584
|
-
:_ast_1,
|
585
|
-
:_ast_2,
|
586
|
-
:_ast_3,
|
587
|
-
:_ast_4,
|
588
|
-
:_ast_5,
|
589
|
-
:_ast_6,
|
590
|
-
:_ast_7,
|
591
|
-
|
592
|
-
]
|
593
|
-
Tables = [on_reduce]
|
594
|
-
def initialize inside
|
595
|
-
super inside, 'ASTBuilder'
|
596
|
-
@on_reduce, = self.class::Tables
|
597
|
-
init_parser
|
598
|
-
end
|
599
|
-
|
600
|
-
|
601
|
-
module_eval <<-'.,.,122745803840943.,.,', 'sample_calc/calc.ast.action.dr', 29
|
602
|
-
def _ast_0 val
|
603
|
-
Node_add.new(val[0].lineno, val[0], val[2])
|
604
|
-
end
|
605
|
-
.,.,122745803840943.,.,
|
606
|
-
|
607
|
-
module_eval <<-'.,.,122745803849112.,.,', 'sample_calc/calc.ast.action.dr', 32
|
608
|
-
def _ast_1 val
|
609
|
-
Node_sub.new(val[0].lineno, val[0], val[2])
|
610
|
-
end
|
611
|
-
.,.,122745803849112.,.,
|
612
|
-
|
613
|
-
module_eval <<-'.,.,122745803812666.,.,', 'sample_calc/calc.ast.action.dr', 35
|
614
|
-
def _ast_2 val
|
615
|
-
val[0]
|
616
|
-
end
|
617
|
-
.,.,122745803812666.,.,
|
618
|
-
|
619
|
-
module_eval <<-'.,.,122745803828835.,.,', 'sample_calc/calc.ast.action.dr', 40
|
620
|
-
def _ast_3 val
|
621
|
-
Node_mul.new(val[0].lineno, val[0], val[2])
|
622
|
-
end
|
623
|
-
.,.,122745803828835.,.,
|
624
|
-
|
625
|
-
module_eval <<-'.,.,122745803856208.,.,', 'sample_calc/calc.ast.action.dr', 43
|
626
|
-
def _ast_4 val
|
627
|
-
Node_div.new(val[0].lineno, val[0], val[2])
|
628
|
-
end
|
629
|
-
.,.,122745803856208.,.,
|
630
|
-
|
631
|
-
module_eval <<-'.,.,122745803842081.,.,', 'sample_calc/calc.ast.action.dr', 46
|
632
|
-
def _ast_5 val
|
633
|
-
val[0]
|
634
|
-
end
|
635
|
-
.,.,122745803842081.,.,
|
636
|
-
|
637
|
-
module_eval <<-'.,.,122745803820842.,.,', 'sample_calc/calc.ast.action.dr', 51
|
638
|
-
def _ast_6 val
|
639
|
-
Node_literal.new(val[0].lineno, val[0])
|
640
|
-
end
|
641
|
-
.,.,122745803820842.,.,
|
642
|
-
|
643
|
-
module_eval <<-'.,.,122745803861388.,.,', 'sample_calc/calc.ast.action.dr', 54
|
644
|
-
def _ast_7 val
|
645
|
-
val[1]
|
646
|
-
end
|
647
|
-
.,.,122745803861388.,.,
|
648
|
-
|
649
|
-
end
|
650
|
-
|
651
|
-
class D4TinyCalc::Action < Depager::LALR::Action #:nodoc:all
|
652
|
-
include Depager::DecoratorUtils
|
653
|
-
include Depager
|
654
|
-
|
655
|
-
on_reduce = [
|
656
|
-
nil,
|
657
|
-
:_act_0,
|
658
|
-
:_act_1,
|
659
|
-
:_act_2,
|
660
|
-
:_act_3,
|
661
|
-
:_act_4,
|
662
|
-
:_act_5,
|
663
|
-
:_act_6,
|
664
|
-
:_act_7,
|
665
|
-
|
666
|
-
]
|
667
|
-
Tables = [on_reduce]
|
668
|
-
def initialize inside
|
669
|
-
super inside, 'Action'
|
670
|
-
@on_reduce, = self.class::Tables
|
671
|
-
init_parser
|
672
|
-
end
|
673
|
-
|
674
|
-
|
675
|
-
module_eval <<-'.,.,122745803844148.,.,', 'sample_calc/calc.ast.action.dr', 30
|
676
|
-
def _act_0 val
|
677
|
-
val[0] + val[2]
|
678
|
-
|
679
|
-
end
|
680
|
-
.,.,122745803844148.,.,
|
681
|
-
|
682
|
-
module_eval <<-'.,.,122745803845543.,.,', 'sample_calc/calc.ast.action.dr', 33
|
683
|
-
def _act_1 val
|
684
|
-
val[0] - val[2]
|
685
|
-
|
686
|
-
end
|
687
|
-
.,.,122745803845543.,.,
|
688
|
-
|
689
|
-
module_eval <<-'.,.,122745803827764.,.,', 'sample_calc/calc.ast.action.dr', 36
|
690
|
-
def _act_2 val
|
691
|
-
val[0]
|
692
|
-
|
693
|
-
end
|
694
|
-
.,.,122745803827764.,.,
|
695
|
-
|
696
|
-
module_eval <<-'.,.,122745803817369.,.,', 'sample_calc/calc.ast.action.dr', 41
|
697
|
-
def _act_3 val
|
698
|
-
val[0] * val[2]
|
699
|
-
|
700
|
-
end
|
701
|
-
.,.,122745803817369.,.,
|
702
|
-
|
703
|
-
module_eval <<-'.,.,122745803812.,.,', 'sample_calc/calc.ast.action.dr', 44
|
704
|
-
def _act_4 val
|
705
|
-
val[0] / val[2]
|
706
|
-
|
707
|
-
end
|
708
|
-
.,.,122745803812.,.,
|
709
|
-
|
710
|
-
module_eval <<-'.,.,122745803822872.,.,', 'sample_calc/calc.ast.action.dr', 47
|
711
|
-
def _act_5 val
|
712
|
-
val[0]
|
713
|
-
|
714
|
-
end
|
715
|
-
.,.,122745803822872.,.,
|
716
|
-
|
717
|
-
module_eval <<-'.,.,12274580385526.,.,', 'sample_calc/calc.ast.action.dr', 52
|
718
|
-
def _act_6 val
|
719
|
-
val[0].value
|
720
|
-
|
721
|
-
end
|
722
|
-
.,.,12274580385526.,.,
|
723
|
-
|
724
|
-
module_eval <<-'.,.,122745803860083.,.,', 'sample_calc/calc.ast.action.dr', 55
|
725
|
-
def _act_7 val
|
726
|
-
val[1].value
|
727
|
-
|
728
|
-
end
|
729
|
-
.,.,122745803860083.,.,
|
730
|
-
|
731
|
-
end
|
732
|
-
|
733
|
-
|
734
|
-
def createDecoratedTinyCalc
|
735
|
-
D4TinyCalc::Action.new(D4TinyCalc::ASTBuilder.new(TinyCalc.new()))
|
736
|
-
end
|
737
|
-
|
738
|
-
### main
|
739
|
-
if __FILE__ == $0
|
740
|
-
### Main Code
|
741
|
-
require 'pp'
|
742
|
-
parser = createDecoratedTinyCalc()
|
743
|
-
t, r = parser.parse(STDIN)
|
744
|
-
v = Visitor.new
|
745
|
-
pp r
|
746
|
-
pp t.accept(v).value
|
747
|
-
pp t
|
748
|
-
|
749
|
-
end
|