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,374 +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
|
-
}
|
27
|
-
### Term to Int
|
28
|
-
t2i = {
|
29
|
-
nil => 0,
|
30
|
-
false => 1,
|
31
|
-
"+" => 2,
|
32
|
-
"-" => 3,
|
33
|
-
"*" => 4,
|
34
|
-
"/" => 5,
|
35
|
-
:NUM => 6,
|
36
|
-
"(" => 7,
|
37
|
-
")" => 8,
|
38
|
-
}
|
39
|
-
### Int to Term
|
40
|
-
i2t = [
|
41
|
-
nil,
|
42
|
-
false,
|
43
|
-
"+",
|
44
|
-
"-",
|
45
|
-
"*",
|
46
|
-
"/",
|
47
|
-
:NUM,
|
48
|
-
"(",
|
49
|
-
")",
|
50
|
-
]
|
51
|
-
### Action Table
|
52
|
-
action_table = [
|
53
|
-
[ nil, nil, nil, nil, nil, nil, 5, 1, nil, ],
|
54
|
-
[ nil, nil, nil, nil, nil, nil, 5, 1, nil, ],
|
55
|
-
[ ACC, nil, 7, 8, nil, nil, nil, nil, nil, ],
|
56
|
-
[ nil, nil, nil, nil, 9, 10, nil, nil, nil, ],
|
57
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
58
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
59
|
-
[ nil, nil, 7, 8, nil, nil, nil, nil, 11, ],
|
60
|
-
[ nil, nil, nil, nil, nil, nil, 5, 1, nil, ],
|
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, nil, nil, nil, ],
|
65
|
-
[ nil, nil, nil, nil, 9, 10, nil, nil, nil, ],
|
66
|
-
[ nil, nil, nil, nil, 9, 10, nil, nil, nil, ],
|
67
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
68
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
69
|
-
]
|
70
|
-
### Default Reduce Table
|
71
|
-
defred_table = [
|
72
|
-
nil,
|
73
|
-
nil,
|
74
|
-
nil,
|
75
|
-
-3,
|
76
|
-
-6,
|
77
|
-
-7,
|
78
|
-
nil,
|
79
|
-
nil,
|
80
|
-
nil,
|
81
|
-
nil,
|
82
|
-
nil,
|
83
|
-
-8,
|
84
|
-
-1,
|
85
|
-
-2,
|
86
|
-
-4,
|
87
|
-
-5,
|
88
|
-
]
|
89
|
-
defred_after_shift_table = [
|
90
|
-
nil,
|
91
|
-
nil,
|
92
|
-
nil,
|
93
|
-
nil,
|
94
|
-
-6,
|
95
|
-
-7,
|
96
|
-
nil,
|
97
|
-
nil,
|
98
|
-
nil,
|
99
|
-
nil,
|
100
|
-
nil,
|
101
|
-
-8,
|
102
|
-
nil,
|
103
|
-
nil,
|
104
|
-
-4,
|
105
|
-
-5,
|
106
|
-
]
|
107
|
-
### Nonterm to Int
|
108
|
-
nt2i = {
|
109
|
-
:expr => 0,
|
110
|
-
:term => 1,
|
111
|
-
:fact => 2,
|
112
|
-
}
|
113
|
-
### Int to Nonterm
|
114
|
-
i2nt = [
|
115
|
-
:expr,
|
116
|
-
:term,
|
117
|
-
:fact,
|
118
|
-
]
|
119
|
-
### Goto Table
|
120
|
-
goto_table = [
|
121
|
-
[ 2, 3, 4, ],
|
122
|
-
[ 6, 3, 4, ],
|
123
|
-
[ nil, nil, nil, ],
|
124
|
-
[ nil, nil, nil, ],
|
125
|
-
[ nil, nil, nil, ],
|
126
|
-
[ nil, nil, nil, ],
|
127
|
-
[ nil, nil, nil, ],
|
128
|
-
[ nil, 12, 4, ],
|
129
|
-
[ nil, 13, 4, ],
|
130
|
-
[ nil, nil, 14, ],
|
131
|
-
[ nil, nil, 15, ],
|
132
|
-
[ nil, nil, nil, ],
|
133
|
-
[ nil, nil, nil, ],
|
134
|
-
[ nil, nil, nil, ],
|
135
|
-
[ nil, nil, nil, ],
|
136
|
-
[ nil, nil, nil, ],
|
137
|
-
]
|
138
|
-
|
139
|
-
|
140
|
-
alias orig_error error
|
141
|
-
def error
|
142
|
-
orig_error
|
143
|
-
warn "current state: #{StateInfo[@stack.last]}"
|
144
|
-
end
|
145
|
-
|
146
|
-
### States
|
147
|
-
StateInfo = [
|
148
|
-
<<'----------',
|
149
|
-
I000 =
|
150
|
-
( 0) $start : _ expr
|
151
|
-
|
152
|
-
NUM shift, and goto to state 5
|
153
|
-
'(' shift, and goto to state 1
|
154
|
-
|
155
|
-
|
156
|
-
----------
|
157
|
-
|
158
|
-
<<'----------',
|
159
|
-
I001 =
|
160
|
-
( 8) fact : '(' _ expr ')'
|
161
|
-
|
162
|
-
NUM shift, and goto to state 5
|
163
|
-
'(' shift, and goto to state 1
|
164
|
-
|
165
|
-
|
166
|
-
----------
|
167
|
-
|
168
|
-
<<'----------',
|
169
|
-
I002 =
|
170
|
-
( 0) $start : expr _
|
171
|
-
( 1) expr : expr _ '+' term
|
172
|
-
( 2) expr : expr _ '-' term
|
173
|
-
|
174
|
-
'+' shift, and goto to state 7
|
175
|
-
'-' shift, and goto to state 8
|
176
|
-
|
177
|
-
|
178
|
-
$end accept
|
179
|
-
|
180
|
-
----------
|
181
|
-
|
182
|
-
<<'----------',
|
183
|
-
I003 =
|
184
|
-
( 3) expr : term _
|
185
|
-
( 4) term : term _ '*' fact
|
186
|
-
( 5) term : term _ '/' fact
|
187
|
-
|
188
|
-
'*' shift, and goto to state 9
|
189
|
-
'/' shift, and goto to state 10
|
190
|
-
|
191
|
-
$default reduce using rule 3 (expr)
|
192
|
-
|
193
|
-
----------
|
194
|
-
|
195
|
-
<<'----------',
|
196
|
-
I004 =
|
197
|
-
( 6) term : fact _
|
198
|
-
|
199
|
-
|
200
|
-
$default reduce using rule 6 (term) [after shift]
|
201
|
-
|
202
|
-
----------
|
203
|
-
|
204
|
-
<<'----------',
|
205
|
-
I005 =
|
206
|
-
( 7) fact : NUM _
|
207
|
-
|
208
|
-
|
209
|
-
$default reduce using rule 7 (fact) [after shift]
|
210
|
-
|
211
|
-
----------
|
212
|
-
|
213
|
-
<<'----------',
|
214
|
-
I006 =
|
215
|
-
( 8) fact : '(' expr _ ')'
|
216
|
-
( 1) expr : expr _ '+' term
|
217
|
-
( 2) expr : expr _ '-' term
|
218
|
-
|
219
|
-
'+' shift, and goto to state 7
|
220
|
-
'-' shift, and goto to state 8
|
221
|
-
')' shift, and goto to state 11
|
222
|
-
|
223
|
-
|
224
|
-
----------
|
225
|
-
|
226
|
-
<<'----------',
|
227
|
-
I007 =
|
228
|
-
( 1) expr : expr '+' _ term
|
229
|
-
|
230
|
-
NUM shift, and goto to state 5
|
231
|
-
'(' shift, and goto to state 1
|
232
|
-
|
233
|
-
|
234
|
-
----------
|
235
|
-
|
236
|
-
<<'----------',
|
237
|
-
I008 =
|
238
|
-
( 2) expr : expr '-' _ term
|
239
|
-
|
240
|
-
NUM shift, and goto to state 5
|
241
|
-
'(' shift, and goto to state 1
|
242
|
-
|
243
|
-
|
244
|
-
----------
|
245
|
-
|
246
|
-
<<'----------',
|
247
|
-
I009 =
|
248
|
-
( 4) term : term '*' _ fact
|
249
|
-
|
250
|
-
NUM shift, and goto to state 5
|
251
|
-
'(' shift, and goto to state 1
|
252
|
-
|
253
|
-
|
254
|
-
----------
|
255
|
-
|
256
|
-
<<'----------',
|
257
|
-
I010 =
|
258
|
-
( 5) term : term '/' _ fact
|
259
|
-
|
260
|
-
NUM shift, and goto to state 5
|
261
|
-
'(' shift, and goto to state 1
|
262
|
-
|
263
|
-
|
264
|
-
----------
|
265
|
-
|
266
|
-
<<'----------',
|
267
|
-
I011 =
|
268
|
-
( 8) fact : '(' expr ')' _
|
269
|
-
|
270
|
-
|
271
|
-
$default reduce using rule 8 (fact) [after shift]
|
272
|
-
|
273
|
-
----------
|
274
|
-
|
275
|
-
<<'----------',
|
276
|
-
I012 =
|
277
|
-
( 1) expr : expr '+' term _
|
278
|
-
( 4) term : term _ '*' fact
|
279
|
-
( 5) term : term _ '/' fact
|
280
|
-
|
281
|
-
'*' shift, and goto to state 9
|
282
|
-
'/' shift, and goto to state 10
|
283
|
-
|
284
|
-
$default reduce using rule 1 (expr)
|
285
|
-
|
286
|
-
----------
|
287
|
-
|
288
|
-
<<'----------',
|
289
|
-
I013 =
|
290
|
-
( 2) expr : expr '-' term _
|
291
|
-
( 4) term : term _ '*' fact
|
292
|
-
( 5) term : term _ '/' fact
|
293
|
-
|
294
|
-
'*' shift, and goto to state 9
|
295
|
-
'/' shift, and goto to state 10
|
296
|
-
|
297
|
-
$default reduce using rule 2 (expr)
|
298
|
-
|
299
|
-
----------
|
300
|
-
|
301
|
-
<<'----------',
|
302
|
-
I014 =
|
303
|
-
( 4) term : term '*' fact _
|
304
|
-
|
305
|
-
|
306
|
-
$default reduce using rule 4 (term) [after shift]
|
307
|
-
|
308
|
-
----------
|
309
|
-
|
310
|
-
<<'----------',
|
311
|
-
I015 =
|
312
|
-
( 5) term : term '/' fact _
|
313
|
-
|
314
|
-
|
315
|
-
$default reduce using rule 5 (term) [after shift]
|
316
|
-
|
317
|
-
----------
|
318
|
-
]
|
319
|
-
|
320
|
-
|
321
|
-
Tables = [ reduce_table, nparams, action_table,
|
322
|
-
defred_table, defred_after_shift_table, goto_table,
|
323
|
-
t2i, i2t, nt2i, i2nt ]
|
324
|
-
|
325
|
-
def self.createDecoratedParser
|
326
|
-
TinyCalc.new()
|
327
|
-
end
|
328
|
-
|
329
|
-
### Inner Code
|
330
|
-
|
331
|
-
def lex
|
332
|
-
begin
|
333
|
-
until @line.empty?
|
334
|
-
case @line
|
335
|
-
|
336
|
-
when /\A\s+/, /\A\#.*/, /\A\n/
|
337
|
-
@oldline = @line; @line = $'
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
when /\A[1-9][0-9]*/
|
342
|
-
@oldline = @line; @line = $'
|
343
|
-
yield token(:NUM, $&.to_i)
|
344
|
-
|
345
|
-
|
346
|
-
when /\A./
|
347
|
-
@oldline = @line; @line = $'
|
348
|
-
yield token($&, $&)
|
349
|
-
|
350
|
-
|
351
|
-
else
|
352
|
-
raise RuntimeError, "must not happen #{@line}"
|
353
|
-
end
|
354
|
-
end
|
355
|
-
end while @line0 = @line = getline
|
356
|
-
yield nil, nil
|
357
|
-
end
|
358
|
-
|
359
|
-
end
|
360
|
-
|
361
|
-
### Outer Code
|
362
|
-
|
363
|
-
|
364
|
-
def createDecoratedTinyCalc
|
365
|
-
TinyCalc.new()
|
366
|
-
end
|
367
|
-
|
368
|
-
### main
|
369
|
-
if __FILE__ == $0
|
370
|
-
### Main Code
|
371
|
-
parser = TinyCalc.new()
|
372
|
-
parser.parse(STDIN)
|
373
|
-
|
374
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
%class TinyCalc
|
2
|
-
%extend Lexer ('plugins/lex.rb')
|
3
|
-
%extend NVAction ('plugins/nvaction.rb')
|
4
|
-
%decorate @NVAction
|
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
|
-
#begin-rule
|
15
|
-
expr :
|
16
|
-
expr '+' term { _expr + _term }
|
17
|
-
| expr '-' term { _expr - _term }
|
18
|
-
| term { _term }
|
19
|
-
;
|
20
|
-
term :
|
21
|
-
term '*' fact { _term * _fact }
|
22
|
-
| term '/' fact { _term / _fact }
|
23
|
-
| fact { _fact }
|
24
|
-
;
|
25
|
-
fact :
|
26
|
-
NUM { _NUM.value }
|
27
|
-
| '(' expr ')' { _expr }
|
28
|
-
;
|
29
|
-
#end-rule
|
30
|
-
%%
|
31
|
-
parser = createDecoratedTinyCalc
|
32
|
-
r, = parser.parse(STDIN)
|
33
|
-
puts r
|
@@ -1,465 +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
|
-
'NVAction' => 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::NVAction.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
|
-
|
364
|
-
class D4TinyCalc::NVAction < Depager::LALR::Action #:nodoc:all
|
365
|
-
include Depager::DecoratorUtils
|
366
|
-
|
367
|
-
on_reduce = [
|
368
|
-
nil,
|
369
|
-
:_act_0,
|
370
|
-
:_act_1,
|
371
|
-
:_act_2,
|
372
|
-
:_act_3,
|
373
|
-
:_act_4,
|
374
|
-
:_act_5,
|
375
|
-
:_act_6,
|
376
|
-
:_act_7,
|
377
|
-
|
378
|
-
]
|
379
|
-
Tables = [on_reduce]
|
380
|
-
def initialize inside
|
381
|
-
super inside, 'NVAction'
|
382
|
-
@on_reduce, = self.class::Tables
|
383
|
-
init_parser
|
384
|
-
end
|
385
|
-
|
386
|
-
|
387
|
-
module_eval <<-'.,.,122745803961572.,.,', 'sample_calc/calc.nvaction.dr', 14
|
388
|
-
def _act_0 val
|
389
|
-
_expr, _, _term, = *val
|
390
|
-
_expr + _term
|
391
|
-
|
392
|
-
end
|
393
|
-
.,.,122745803961572.,.,
|
394
|
-
|
395
|
-
module_eval <<-'.,.,122745803917671.,.,', 'sample_calc/calc.nvaction.dr', 15
|
396
|
-
def _act_1 val
|
397
|
-
_expr, _, _term, = *val
|
398
|
-
_expr - _term
|
399
|
-
|
400
|
-
end
|
401
|
-
.,.,122745803917671.,.,
|
402
|
-
|
403
|
-
module_eval <<-'.,.,12274580397247.,.,', 'sample_calc/calc.nvaction.dr', 16
|
404
|
-
def _act_2 val
|
405
|
-
_term, = *val
|
406
|
-
_term
|
407
|
-
|
408
|
-
end
|
409
|
-
.,.,12274580397247.,.,
|
410
|
-
|
411
|
-
module_eval <<-'.,.,122745803926497.,.,', 'sample_calc/calc.nvaction.dr', 19
|
412
|
-
def _act_3 val
|
413
|
-
_term, _, _fact, = *val
|
414
|
-
_term * _fact
|
415
|
-
|
416
|
-
end
|
417
|
-
.,.,122745803926497.,.,
|
418
|
-
|
419
|
-
module_eval <<-'.,.,122745803919971.,.,', 'sample_calc/calc.nvaction.dr', 20
|
420
|
-
def _act_4 val
|
421
|
-
_term, _, _fact, = *val
|
422
|
-
_term / _fact
|
423
|
-
|
424
|
-
end
|
425
|
-
.,.,122745803919971.,.,
|
426
|
-
|
427
|
-
module_eval <<-'.,.,122745803920937.,.,', 'sample_calc/calc.nvaction.dr', 21
|
428
|
-
def _act_5 val
|
429
|
-
_fact, = *val
|
430
|
-
_fact
|
431
|
-
|
432
|
-
end
|
433
|
-
.,.,122745803920937.,.,
|
434
|
-
|
435
|
-
module_eval <<-'.,.,122745803940791.,.,', 'sample_calc/calc.nvaction.dr', 24
|
436
|
-
def _act_6 val
|
437
|
-
_NUM, = *val
|
438
|
-
_NUM.value
|
439
|
-
|
440
|
-
end
|
441
|
-
.,.,122745803940791.,.,
|
442
|
-
|
443
|
-
module_eval <<-'.,.,122745803911472.,.,', 'sample_calc/calc.nvaction.dr', 25
|
444
|
-
def _act_7 val
|
445
|
-
_, _expr, _, = *val
|
446
|
-
_expr
|
447
|
-
|
448
|
-
end
|
449
|
-
.,.,122745803911472.,.,
|
450
|
-
|
451
|
-
end
|
452
|
-
|
453
|
-
|
454
|
-
def createDecoratedTinyCalc
|
455
|
-
D4TinyCalc::NVAction.new(TinyCalc.new())
|
456
|
-
end
|
457
|
-
|
458
|
-
### main
|
459
|
-
if __FILE__ == $0
|
460
|
-
### Main Code
|
461
|
-
parser = createDecoratedTinyCalc
|
462
|
-
r, = parser.parse(STDIN)
|
463
|
-
puts r
|
464
|
-
|
465
|
-
end
|