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,54 +0,0 @@
|
|
1
|
-
%class TinyCalc
|
2
|
-
%extend Lexer ('plugins/lex.rb')
|
3
|
-
%extend ASTBuilderDepthFirst ('plugins/astdf.rb')
|
4
|
-
%decorate @ASTBuilderDepthFirst
|
5
|
-
#%decorate ShiftReducePrinter ('plugins/srp.rb')
|
6
|
-
%%
|
7
|
-
|
8
|
-
%LEX{
|
9
|
-
/\s+/, /\#.*/ { }
|
10
|
-
/[1-9][0-9]*/ { yield token(:NUM, $&.to_i) }
|
11
|
-
/./ { yield token($&, $&) }
|
12
|
-
%}
|
13
|
-
|
14
|
-
%AST{
|
15
|
-
Node [value] { @value = nil }
|
16
|
-
Visitor { }
|
17
|
-
add(left, right) { $.value = $.left.value + $.right.value }
|
18
|
-
sub(left, right) { $.value = $.left.value - $.right.value }
|
19
|
-
mul(left, right) { $.value = $.left.value * $.right.value }
|
20
|
-
div(left, right) { $.value = $.left.value / $.right.value }
|
21
|
-
literal(-n) { $.value = $.n.value }
|
22
|
-
%}
|
23
|
-
|
24
|
-
#begin-rule
|
25
|
-
expr :
|
26
|
-
expr '+' term
|
27
|
-
=> add(expr, term)
|
28
|
-
| expr '-' term
|
29
|
-
=> sub(expr, term)
|
30
|
-
| term
|
31
|
-
=> term
|
32
|
-
;
|
33
|
-
term :
|
34
|
-
term '*' fact
|
35
|
-
=> mul(term, fact)
|
36
|
-
| term '/' fact
|
37
|
-
=> div(term, fact)
|
38
|
-
| fact
|
39
|
-
=> fact
|
40
|
-
;
|
41
|
-
fact :
|
42
|
-
NUM
|
43
|
-
=> literal(NUM)
|
44
|
-
| '(' expr ')'
|
45
|
-
=> expr
|
46
|
-
;
|
47
|
-
#end-rule
|
48
|
-
%%
|
49
|
-
require 'pp'
|
50
|
-
parser = createDecoratedTinyCalc()
|
51
|
-
t, = parser.parse(STDIN)
|
52
|
-
v = Visitor.new
|
53
|
-
pp t.accept(v).value
|
54
|
-
pp t
|
@@ -1,672 +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
|
-
|
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
|
-
'ASTBuilderDepthFirst' => 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
|
-
|
141
|
-
alias orig_error error
|
142
|
-
def error
|
143
|
-
orig_error
|
144
|
-
warn "current state: #{StateInfo[@stack.last]}"
|
145
|
-
end
|
146
|
-
|
147
|
-
### States
|
148
|
-
StateInfo = [
|
149
|
-
<<'----------',
|
150
|
-
I000 =
|
151
|
-
( 0) $start : _ expr
|
152
|
-
|
153
|
-
NUM shift, and goto to state 5
|
154
|
-
'(' shift, and goto to state 1
|
155
|
-
|
156
|
-
|
157
|
-
----------
|
158
|
-
|
159
|
-
<<'----------',
|
160
|
-
I001 =
|
161
|
-
( 8) fact : '(' _ expr ')'
|
162
|
-
|
163
|
-
NUM shift, and goto to state 5
|
164
|
-
'(' shift, and goto to state 1
|
165
|
-
|
166
|
-
|
167
|
-
----------
|
168
|
-
|
169
|
-
<<'----------',
|
170
|
-
I002 =
|
171
|
-
( 0) $start : expr _
|
172
|
-
( 1) expr : expr _ '+' term
|
173
|
-
( 2) expr : expr _ '-' term
|
174
|
-
|
175
|
-
'+' shift, and goto to state 7
|
176
|
-
'-' shift, and goto to state 8
|
177
|
-
|
178
|
-
|
179
|
-
$end accept
|
180
|
-
|
181
|
-
----------
|
182
|
-
|
183
|
-
<<'----------',
|
184
|
-
I003 =
|
185
|
-
( 3) expr : term _
|
186
|
-
( 4) term : term _ '*' fact
|
187
|
-
( 5) term : term _ '/' fact
|
188
|
-
|
189
|
-
'*' shift, and goto to state 9
|
190
|
-
'/' shift, and goto to state 10
|
191
|
-
|
192
|
-
$default reduce using rule 3 (expr)
|
193
|
-
|
194
|
-
----------
|
195
|
-
|
196
|
-
<<'----------',
|
197
|
-
I004 =
|
198
|
-
( 6) term : fact _
|
199
|
-
|
200
|
-
|
201
|
-
$default reduce using rule 6 (term) [after shift]
|
202
|
-
|
203
|
-
----------
|
204
|
-
|
205
|
-
<<'----------',
|
206
|
-
I005 =
|
207
|
-
( 7) fact : NUM _
|
208
|
-
|
209
|
-
|
210
|
-
$default reduce using rule 7 (fact) [after shift]
|
211
|
-
|
212
|
-
----------
|
213
|
-
|
214
|
-
<<'----------',
|
215
|
-
I006 =
|
216
|
-
( 8) fact : '(' expr _ ')'
|
217
|
-
( 1) expr : expr _ '+' term
|
218
|
-
( 2) expr : expr _ '-' term
|
219
|
-
|
220
|
-
'+' shift, and goto to state 7
|
221
|
-
'-' shift, and goto to state 8
|
222
|
-
')' shift, and goto to state 11
|
223
|
-
|
224
|
-
|
225
|
-
----------
|
226
|
-
|
227
|
-
<<'----------',
|
228
|
-
I007 =
|
229
|
-
( 1) expr : expr '+' _ term
|
230
|
-
|
231
|
-
NUM shift, and goto to state 5
|
232
|
-
'(' shift, and goto to state 1
|
233
|
-
|
234
|
-
|
235
|
-
----------
|
236
|
-
|
237
|
-
<<'----------',
|
238
|
-
I008 =
|
239
|
-
( 2) expr : expr '-' _ term
|
240
|
-
|
241
|
-
NUM shift, and goto to state 5
|
242
|
-
'(' shift, and goto to state 1
|
243
|
-
|
244
|
-
|
245
|
-
----------
|
246
|
-
|
247
|
-
<<'----------',
|
248
|
-
I009 =
|
249
|
-
( 4) term : term '*' _ fact
|
250
|
-
|
251
|
-
NUM shift, and goto to state 5
|
252
|
-
'(' shift, and goto to state 1
|
253
|
-
|
254
|
-
|
255
|
-
----------
|
256
|
-
|
257
|
-
<<'----------',
|
258
|
-
I010 =
|
259
|
-
( 5) term : term '/' _ fact
|
260
|
-
|
261
|
-
NUM shift, and goto to state 5
|
262
|
-
'(' shift, and goto to state 1
|
263
|
-
|
264
|
-
|
265
|
-
----------
|
266
|
-
|
267
|
-
<<'----------',
|
268
|
-
I011 =
|
269
|
-
( 8) fact : '(' expr ')' _
|
270
|
-
|
271
|
-
|
272
|
-
$default reduce using rule 8 (fact) [after shift]
|
273
|
-
|
274
|
-
----------
|
275
|
-
|
276
|
-
<<'----------',
|
277
|
-
I012 =
|
278
|
-
( 1) expr : expr '+' term _
|
279
|
-
( 4) term : term _ '*' fact
|
280
|
-
( 5) term : term _ '/' fact
|
281
|
-
|
282
|
-
'*' shift, and goto to state 9
|
283
|
-
'/' shift, and goto to state 10
|
284
|
-
|
285
|
-
$default reduce using rule 1 (expr)
|
286
|
-
|
287
|
-
----------
|
288
|
-
|
289
|
-
<<'----------',
|
290
|
-
I013 =
|
291
|
-
( 2) expr : expr '-' term _
|
292
|
-
( 4) term : term _ '*' fact
|
293
|
-
( 5) term : term _ '/' fact
|
294
|
-
|
295
|
-
'*' shift, and goto to state 9
|
296
|
-
'/' shift, and goto to state 10
|
297
|
-
|
298
|
-
$default reduce using rule 2 (expr)
|
299
|
-
|
300
|
-
----------
|
301
|
-
|
302
|
-
<<'----------',
|
303
|
-
I014 =
|
304
|
-
( 4) term : term '*' fact _
|
305
|
-
|
306
|
-
|
307
|
-
$default reduce using rule 4 (term) [after shift]
|
308
|
-
|
309
|
-
----------
|
310
|
-
|
311
|
-
<<'----------',
|
312
|
-
I015 =
|
313
|
-
( 5) term : term '/' fact _
|
314
|
-
|
315
|
-
|
316
|
-
$default reduce using rule 5 (term) [after shift]
|
317
|
-
|
318
|
-
----------
|
319
|
-
]
|
320
|
-
|
321
|
-
|
322
|
-
Tables = [ reduce_table, nparams, action_table,
|
323
|
-
defred_table, defred_after_shift_table, goto_table,
|
324
|
-
t2i, i2t, nt2i, i2nt ]
|
325
|
-
|
326
|
-
def self.createDecoratedParser
|
327
|
-
D4TinyCalc::ASTBuilderDepthFirst.new(TinyCalc.new())
|
328
|
-
end
|
329
|
-
|
330
|
-
### Inner Code
|
331
|
-
|
332
|
-
def lex
|
333
|
-
begin
|
334
|
-
until @line.empty?
|
335
|
-
case @line
|
336
|
-
|
337
|
-
when /\A\s+/, /\A\#.*/
|
338
|
-
@oldline = @line; @line = $'
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
when /\A[1-9][0-9]*/
|
343
|
-
@oldline = @line; @line = $'
|
344
|
-
yield token(:NUM, $&.to_i)
|
345
|
-
|
346
|
-
|
347
|
-
when /\A./
|
348
|
-
@oldline = @line; @line = $'
|
349
|
-
yield token($&, $&)
|
350
|
-
|
351
|
-
|
352
|
-
else
|
353
|
-
raise RuntimeError, "must not happen #{@line}"
|
354
|
-
end
|
355
|
-
end
|
356
|
-
end while @line0 = @line = getline
|
357
|
-
yield nil, nil
|
358
|
-
end
|
359
|
-
|
360
|
-
end
|
361
|
-
|
362
|
-
### Outer Code
|
363
|
-
class Node
|
364
|
-
attr_accessor :lineno
|
365
|
-
attr_accessor :value
|
366
|
-
|
367
|
-
def initialize
|
368
|
-
@value = nil
|
369
|
-
|
370
|
-
end
|
371
|
-
|
372
|
-
def self.[] lineno, *args
|
373
|
-
self.new lineno, *args
|
374
|
-
end
|
375
|
-
def accept v
|
376
|
-
end
|
377
|
-
end
|
378
|
-
class NodeList < Node
|
379
|
-
attr_accessor :lineno
|
380
|
-
def initialize(lineno, lst=[])
|
381
|
-
@lineno = lineno
|
382
|
-
@lst = lst.to_a.select{|i| ! i.is_a?(NilNode)}
|
383
|
-
end
|
384
|
-
|
385
|
-
def all_value
|
386
|
-
@lst.map{|i| i.value}
|
387
|
-
end
|
388
|
-
|
389
|
-
def size
|
390
|
-
@lst.size
|
391
|
-
end
|
392
|
-
alias length size
|
393
|
-
def push(i)
|
394
|
-
@lst.push i unless i.is_a? NilNode
|
395
|
-
self
|
396
|
-
end
|
397
|
-
def concat(i)
|
398
|
-
@lst.concat i
|
399
|
-
self
|
400
|
-
end
|
401
|
-
def to_ary()
|
402
|
-
@lst
|
403
|
-
end
|
404
|
-
alias to_a to_ary
|
405
|
-
alias list to_ary
|
406
|
-
def accept(v)
|
407
|
-
@lst.each{|i| i.accept(v) }
|
408
|
-
end
|
409
|
-
end
|
410
|
-
class NilNode
|
411
|
-
attr_accessor :lineno
|
412
|
-
attr_accessor :value
|
413
|
-
def initialize(lineno, *args)
|
414
|
-
@lineno = lineno
|
415
|
-
end
|
416
|
-
def accept v
|
417
|
-
end
|
418
|
-
end
|
419
|
-
class Node_add < Node
|
420
|
-
attr_accessor :left, :right
|
421
|
-
attr_accessor
|
422
|
-
def initialize lineno, left, right
|
423
|
-
super()
|
424
|
-
@lineno = lineno
|
425
|
-
@lineno = lineno
|
426
|
-
@left = left
|
427
|
-
@right = right
|
428
|
-
|
429
|
-
end
|
430
|
-
def accept v
|
431
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
432
|
-
@left.accept(v)
|
433
|
-
@right.accept(v)
|
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
|
-
@left.accept(v)
|
453
|
-
@right.accept(v)
|
454
|
-
|
455
|
-
v.visit_Node_sub(self)
|
456
|
-
self
|
457
|
-
end
|
458
|
-
end
|
459
|
-
class Node_mul < Node
|
460
|
-
attr_accessor :left, :right
|
461
|
-
attr_accessor
|
462
|
-
def initialize lineno, left, right
|
463
|
-
super()
|
464
|
-
@lineno = lineno
|
465
|
-
@lineno = lineno
|
466
|
-
@left = left
|
467
|
-
@right = right
|
468
|
-
|
469
|
-
end
|
470
|
-
def accept v
|
471
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
472
|
-
@left.accept(v)
|
473
|
-
@right.accept(v)
|
474
|
-
|
475
|
-
v.visit_Node_mul(self)
|
476
|
-
self
|
477
|
-
end
|
478
|
-
end
|
479
|
-
class Node_div < Node
|
480
|
-
attr_accessor :left, :right
|
481
|
-
attr_accessor
|
482
|
-
def initialize lineno, left, right
|
483
|
-
super()
|
484
|
-
@lineno = lineno
|
485
|
-
@lineno = lineno
|
486
|
-
@left = left
|
487
|
-
@right = right
|
488
|
-
|
489
|
-
end
|
490
|
-
def accept v
|
491
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
492
|
-
@left.accept(v)
|
493
|
-
@right.accept(v)
|
494
|
-
|
495
|
-
v.visit_Node_div(self)
|
496
|
-
self
|
497
|
-
end
|
498
|
-
end
|
499
|
-
class Node_literal < Node
|
500
|
-
attr_accessor :n
|
501
|
-
attr_accessor :n
|
502
|
-
def initialize lineno, n
|
503
|
-
super()
|
504
|
-
@lineno = lineno
|
505
|
-
@lineno = lineno
|
506
|
-
@n = n
|
507
|
-
|
508
|
-
end
|
509
|
-
def accept v
|
510
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
511
|
-
|
512
|
-
v.visit_Node_literal(self)
|
513
|
-
self
|
514
|
-
end
|
515
|
-
end
|
516
|
-
|
517
|
-
class Visitor
|
518
|
-
def visit node
|
519
|
-
node.accept(self)
|
520
|
-
end
|
521
|
-
|
522
|
-
module_eval <<-'.,.,122745803821011.,.,', 'sample_calc/calc.astdf.dr', 16
|
523
|
-
|
524
|
-
|
525
|
-
.,.,122745803821011.,.,
|
526
|
-
|
527
|
-
module_eval <<-'.,.,12274580381382.,.,', 'sample_calc/calc.astdf.dr', 16
|
528
|
-
def visit_Node_add node
|
529
|
-
node.value = node.left.value + node.right.value
|
530
|
-
|
531
|
-
rescue
|
532
|
-
warn "raise at src:#{node.lineno}/#{node.class.name}"
|
533
|
-
raise
|
534
|
-
|
535
|
-
end
|
536
|
-
.,.,12274580381382.,.,
|
537
|
-
|
538
|
-
module_eval <<-'.,.,122745803862484.,.,', 'sample_calc/calc.astdf.dr', 17
|
539
|
-
def visit_Node_sub node
|
540
|
-
node.value = node.left.value - node.right.value
|
541
|
-
|
542
|
-
rescue
|
543
|
-
warn "raise at src:#{node.lineno}/#{node.class.name}"
|
544
|
-
raise
|
545
|
-
|
546
|
-
end
|
547
|
-
.,.,122745803862484.,.,
|
548
|
-
|
549
|
-
module_eval <<-'.,.,122745803834024.,.,', 'sample_calc/calc.astdf.dr', 18
|
550
|
-
def visit_Node_mul node
|
551
|
-
node.value = node.left.value * node.right.value
|
552
|
-
|
553
|
-
rescue
|
554
|
-
warn "raise at src:#{node.lineno}/#{node.class.name}"
|
555
|
-
raise
|
556
|
-
|
557
|
-
end
|
558
|
-
.,.,122745803834024.,.,
|
559
|
-
|
560
|
-
module_eval <<-'.,.,122745803829849.,.,', 'sample_calc/calc.astdf.dr', 19
|
561
|
-
def visit_Node_div node
|
562
|
-
node.value = node.left.value / node.right.value
|
563
|
-
|
564
|
-
rescue
|
565
|
-
warn "raise at src:#{node.lineno}/#{node.class.name}"
|
566
|
-
raise
|
567
|
-
|
568
|
-
end
|
569
|
-
.,.,122745803829849.,.,
|
570
|
-
|
571
|
-
module_eval <<-'.,.,122745803814243.,.,', 'sample_calc/calc.astdf.dr', 20
|
572
|
-
def visit_Node_literal node
|
573
|
-
node.value = node.n.value
|
574
|
-
|
575
|
-
rescue
|
576
|
-
warn "raise at src:#{node.lineno}/#{node.class.name}"
|
577
|
-
raise
|
578
|
-
|
579
|
-
end
|
580
|
-
.,.,122745803814243.,.,
|
581
|
-
|
582
|
-
end
|
583
|
-
|
584
|
-
class D4TinyCalc::ASTBuilderDepthFirst < Depager::LALR::Action #:nodoc:all
|
585
|
-
include Depager::DecoratorUtils
|
586
|
-
|
587
|
-
on_reduce = [
|
588
|
-
nil,
|
589
|
-
:_ast_0,
|
590
|
-
:_ast_1,
|
591
|
-
:_ast_2,
|
592
|
-
:_ast_3,
|
593
|
-
:_ast_4,
|
594
|
-
:_ast_5,
|
595
|
-
:_ast_6,
|
596
|
-
:_ast_7,
|
597
|
-
|
598
|
-
]
|
599
|
-
Tables = [on_reduce]
|
600
|
-
def initialize inside
|
601
|
-
super inside, 'ASTBuilderDepthFirst'
|
602
|
-
@on_reduce, = self.class::Tables
|
603
|
-
init_parser
|
604
|
-
end
|
605
|
-
|
606
|
-
|
607
|
-
module_eval <<-'.,.,122745803811862.,.,', 'sample_calc/calc.astdf.dr', 26
|
608
|
-
def _ast_0 val
|
609
|
-
Node_add.new(val[0].lineno, val[0], val[2])
|
610
|
-
end
|
611
|
-
.,.,122745803811862.,.,
|
612
|
-
|
613
|
-
module_eval <<-'.,.,122745803856027.,.,', 'sample_calc/calc.astdf.dr', 28
|
614
|
-
def _ast_1 val
|
615
|
-
Node_sub.new(val[0].lineno, val[0], val[2])
|
616
|
-
end
|
617
|
-
.,.,122745803856027.,.,
|
618
|
-
|
619
|
-
module_eval <<-'.,.,122745803849322.,.,', 'sample_calc/calc.astdf.dr', 30
|
620
|
-
def _ast_2 val
|
621
|
-
val[0]
|
622
|
-
end
|
623
|
-
.,.,122745803849322.,.,
|
624
|
-
|
625
|
-
module_eval <<-'.,.,12274580384051.,.,', 'sample_calc/calc.astdf.dr', 34
|
626
|
-
def _ast_3 val
|
627
|
-
Node_mul.new(val[0].lineno, val[0], val[2])
|
628
|
-
end
|
629
|
-
.,.,12274580384051.,.,
|
630
|
-
|
631
|
-
module_eval <<-'.,.,122745803856888.,.,', 'sample_calc/calc.astdf.dr', 36
|
632
|
-
def _ast_4 val
|
633
|
-
Node_div.new(val[0].lineno, val[0], val[2])
|
634
|
-
end
|
635
|
-
.,.,122745803856888.,.,
|
636
|
-
|
637
|
-
module_eval <<-'.,.,122745803846734.,.,', 'sample_calc/calc.astdf.dr', 38
|
638
|
-
def _ast_5 val
|
639
|
-
val[0]
|
640
|
-
end
|
641
|
-
.,.,122745803846734.,.,
|
642
|
-
|
643
|
-
module_eval <<-'.,.,12274580385064.,.,', 'sample_calc/calc.astdf.dr', 42
|
644
|
-
def _ast_6 val
|
645
|
-
Node_literal.new(val[0].lineno, val[0])
|
646
|
-
end
|
647
|
-
.,.,12274580385064.,.,
|
648
|
-
|
649
|
-
module_eval <<-'.,.,122745803818388.,.,', 'sample_calc/calc.astdf.dr', 44
|
650
|
-
def _ast_7 val
|
651
|
-
val[1]
|
652
|
-
end
|
653
|
-
.,.,122745803818388.,.,
|
654
|
-
|
655
|
-
end
|
656
|
-
|
657
|
-
|
658
|
-
def createDecoratedTinyCalc
|
659
|
-
D4TinyCalc::ASTBuilderDepthFirst.new(TinyCalc.new())
|
660
|
-
end
|
661
|
-
|
662
|
-
### main
|
663
|
-
if __FILE__ == $0
|
664
|
-
### Main Code
|
665
|
-
require 'pp'
|
666
|
-
parser = createDecoratedTinyCalc()
|
667
|
-
t, = parser.parse(STDIN)
|
668
|
-
v = Visitor.new
|
669
|
-
pp t.accept(v).value
|
670
|
-
pp t
|
671
|
-
|
672
|
-
end
|