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
data/lib/depager/parser.rb
CHANGED
@@ -1,277 +1,282 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
22
|
-
end
|
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
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
"unexpected #{la}, expecting #{exp}."
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
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
|
-
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
lib = File.expand_path("../ruby", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
module Depager
|
6
|
+
class Token
|
7
|
+
attr_accessor :value, :lineno, :filename
|
8
|
+
|
9
|
+
def initialize value, lineno=nil, filename=nil
|
10
|
+
@value = value
|
11
|
+
@lineno = lineno
|
12
|
+
@filename = filename
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s
|
16
|
+
"#{@value.inspect}:#{@lineno}@#{@filename}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def inspect
|
20
|
+
"<#{@value.inspect}:#{@lineno}>"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module LALR
|
25
|
+
# = Parser runtime
|
26
|
+
# == Stack
|
27
|
+
# s:: state
|
28
|
+
# la:: look ahead
|
29
|
+
# v:: value
|
30
|
+
# stack top->
|
31
|
+
# [0, [la0, v0], s0, [la1, v1], s1, .....]
|
32
|
+
#
|
33
|
+
class Parser
|
34
|
+
ACC = 0x03ffffff
|
35
|
+
|
36
|
+
attr_reader :action_table, :defred_table, :defred_after_shift_table
|
37
|
+
attr_reader :goto_table, :reduce_table
|
38
|
+
attr_reader :int_to_term, :term_to_int, :nonterm_to_int, :int_to_nonterm
|
39
|
+
|
40
|
+
def initialize
|
41
|
+
@reduce_table,
|
42
|
+
@action_table,
|
43
|
+
@defred_table,
|
44
|
+
@defred_after_shift_table,
|
45
|
+
@goto_table,
|
46
|
+
@term_to_int,
|
47
|
+
@int_to_term,
|
48
|
+
@nonterm_to_int,
|
49
|
+
@int_to_nonterm = basis.class::Tables
|
50
|
+
end
|
51
|
+
|
52
|
+
def parse f, line = ''
|
53
|
+
basis.__send__ :internal_initialize, f, line, [0, nil]
|
54
|
+
|
55
|
+
do_hook :before_parse
|
56
|
+
lex do |sym, val|
|
57
|
+
basis.lookahead = [term_to_int[sym] || term_to_int[false], val]
|
58
|
+
break if driver
|
59
|
+
end
|
60
|
+
do_hook :after_parse
|
61
|
+
|
62
|
+
stack[1][2..basis.parser_size]
|
63
|
+
end
|
64
|
+
|
65
|
+
def accept
|
66
|
+
end
|
67
|
+
|
68
|
+
def error
|
69
|
+
end
|
70
|
+
|
71
|
+
def shift
|
72
|
+
end
|
73
|
+
|
74
|
+
def reduce
|
75
|
+
end
|
76
|
+
|
77
|
+
def stack
|
78
|
+
basis.stack
|
79
|
+
end
|
80
|
+
|
81
|
+
def lookahead
|
82
|
+
basis.lookahead
|
83
|
+
end
|
84
|
+
|
85
|
+
def action_value
|
86
|
+
basis.action_value
|
87
|
+
end
|
88
|
+
|
89
|
+
protected
|
90
|
+
def lex &block
|
91
|
+
basis.lex &block
|
92
|
+
end
|
93
|
+
|
94
|
+
def driver
|
95
|
+
while true
|
96
|
+
basis.action_value = action_table[stack.last][lookahead[0]] || defred_table[stack.last]
|
97
|
+
|
98
|
+
if action_value == nil
|
99
|
+
error
|
100
|
+
exit 1
|
101
|
+
elsif action_value == ACC
|
102
|
+
if int_to_term[lookahead[0]] == nil
|
103
|
+
accept
|
104
|
+
else
|
105
|
+
error
|
106
|
+
end
|
107
|
+
return true
|
108
|
+
elsif action_value > 0
|
109
|
+
shift
|
110
|
+
while v = defred_after_shift_table[stack.last]
|
111
|
+
basis.action_value = v
|
112
|
+
reduce
|
113
|
+
end
|
114
|
+
return false
|
115
|
+
else
|
116
|
+
reduce
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def do_hook hook
|
122
|
+
if @basis == self
|
123
|
+
send hook
|
124
|
+
elsif hook.to_s.match(/^before_/)
|
125
|
+
send hook
|
126
|
+
@inside.do_hook hook
|
127
|
+
else
|
128
|
+
@inside.do_hook hook
|
129
|
+
send hook
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
private
|
134
|
+
def before_parse
|
135
|
+
end
|
136
|
+
|
137
|
+
def after_parse
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
class Basis < Parser
|
142
|
+
attr_reader :basis, :file, :line, :parser_size
|
143
|
+
attr_accessor :lookahead, :action_value, :stack
|
144
|
+
|
145
|
+
def initialize
|
146
|
+
@basis = self
|
147
|
+
@parser_size = 1
|
148
|
+
super()
|
149
|
+
end
|
150
|
+
|
151
|
+
def banner
|
152
|
+
self.class.name
|
153
|
+
end
|
154
|
+
|
155
|
+
def get_decorator_index
|
156
|
+
@parser_size += 1
|
157
|
+
end
|
158
|
+
|
159
|
+
def accept
|
160
|
+
end
|
161
|
+
|
162
|
+
def error
|
163
|
+
path = file.respond_to?(:path) ? file.path : '-'
|
164
|
+
exp = []
|
165
|
+
action_table[stack.last].each_with_index do |i, x|
|
166
|
+
exp << int_to_term[x] if i
|
167
|
+
end
|
168
|
+
la = lookahead[1].value rescue lookahead[1]
|
169
|
+
la = (int_to_term[lookahead[0]] || la).inspect
|
170
|
+
exp = exp.map{|i| i ? i.inspect : '$' }.join(', ')
|
171
|
+
warn "#{path}:#{file.lineno}: syntax error(#{banner}), unexpected #{la}, expecting #{exp}."
|
172
|
+
warn @stack.pretty_inspect if Depager.debug_mode?
|
173
|
+
end
|
174
|
+
|
175
|
+
def shift
|
176
|
+
stack << lookahead << action_value
|
177
|
+
end
|
178
|
+
|
179
|
+
def reduce
|
180
|
+
r, x = reduce_table[-action_value]
|
181
|
+
(x * 2).times{ stack.pop }
|
182
|
+
v = goto_table[stack.last][r]
|
183
|
+
stack << [r, :NT] << v
|
184
|
+
end
|
185
|
+
|
186
|
+
private
|
187
|
+
def internal_initialize file, line, lookahead
|
188
|
+
@stack, @action_value, @file, @line, @lookahead =
|
189
|
+
[0], nil, file, line, lookahead
|
190
|
+
end
|
191
|
+
|
192
|
+
def token sym, value = nil, lineno = nil
|
193
|
+
lineno ||= file.lineno
|
194
|
+
path = file.respond_to?(:path) ? file.path : '-'
|
195
|
+
return sym, Depager::Token.new(value, lineno, path)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
class AdvancedParser < Parser
|
200
|
+
attr_reader :basis, :decorator_index
|
201
|
+
|
202
|
+
def initialize inside
|
203
|
+
@inside = inside
|
204
|
+
@basis = inside.basis
|
205
|
+
@decorator_index = @basis.get_decorator_index
|
206
|
+
super()
|
207
|
+
end
|
208
|
+
|
209
|
+
def accept
|
210
|
+
before_accept
|
211
|
+
@inside.accept
|
212
|
+
after_accept
|
213
|
+
end
|
214
|
+
|
215
|
+
def error
|
216
|
+
before_error
|
217
|
+
@inside.error
|
218
|
+
after_error
|
219
|
+
end
|
220
|
+
|
221
|
+
def shift
|
222
|
+
before_shift
|
223
|
+
@inside.shift
|
224
|
+
after_shift
|
225
|
+
end
|
226
|
+
|
227
|
+
def reduce
|
228
|
+
before_reduce
|
229
|
+
@inside.reduce
|
230
|
+
after_reduce
|
231
|
+
end
|
232
|
+
|
233
|
+
private
|
234
|
+
def before_accept
|
235
|
+
end
|
236
|
+
|
237
|
+
def after_accept
|
238
|
+
end
|
239
|
+
|
240
|
+
def before_error
|
241
|
+
end
|
242
|
+
|
243
|
+
def after_error
|
244
|
+
end
|
245
|
+
|
246
|
+
def before_shift
|
247
|
+
end
|
248
|
+
|
249
|
+
def after_shift
|
250
|
+
end
|
251
|
+
|
252
|
+
def before_reduce
|
253
|
+
end
|
254
|
+
|
255
|
+
def after_reduce
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
class Action < AdvancedParser
|
260
|
+
def initialize inside
|
261
|
+
super inside
|
262
|
+
@_arguments = []
|
263
|
+
@on_reduce, = self.class::Tables
|
264
|
+
end
|
265
|
+
|
266
|
+
def before_reduce
|
267
|
+
@_arguments = []
|
268
|
+
n = reduce_table[-action_value][1]
|
269
|
+
n.times do |i|
|
270
|
+
v = stack[stack.size - 2 * (i + 1)]
|
271
|
+
@_arguments[n - i - 1] = v[1] == :NT ? v[decorator_index] : v[1]
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
def after_reduce
|
276
|
+
method = @on_reduce[-action_value]
|
277
|
+
result = method ? send(method, @_arguments) : nil
|
278
|
+
stack[stack.size - 2][decorator_index] = result
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|