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/grammar.rb
CHANGED
@@ -1,291 +1,256 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
@
|
11
|
-
@
|
12
|
-
|
13
|
-
@
|
14
|
-
|
15
|
-
@
|
16
|
-
@
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
@
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
def
|
51
|
-
@
|
52
|
-
end
|
53
|
-
|
54
|
-
def
|
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
|
-
@first1[lhs]
|
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
|
-
|
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
|
-
end
|
256
|
-
|
257
|
-
class Rule
|
258
|
-
def self.[] l, r, prec = nil
|
259
|
-
self.new l, r, prec
|
260
|
-
end
|
261
|
-
|
262
|
-
attr_accessor :lhs, :rhs, :grammar, :n, :prec
|
263
|
-
def initialize lhs, rhs, prec = nil
|
264
|
-
@lhs = lhs
|
265
|
-
@rhs = rhs
|
266
|
-
@prec = prec
|
267
|
-
|
268
|
-
@grammar = nil
|
269
|
-
@n = 0
|
270
|
-
|
271
|
-
initialize_depend
|
272
|
-
end
|
273
|
-
def initialize_depend
|
274
|
-
end
|
275
|
-
|
276
|
-
def to_s
|
277
|
-
lhs = grammar.symname @lhs
|
278
|
-
rhs = @rhs.map{|i| grammar.symname i}.join(' ')
|
279
|
-
|
280
|
-
"(#{'%03s' % @n}) #{lhs} : #{rhs}"
|
281
|
-
end
|
282
|
-
|
283
|
-
def hash
|
284
|
-
1117 * @rhs.hash + @lhs.hash % 3037
|
285
|
-
end
|
286
|
-
def eql? i
|
287
|
-
@lhs == i.lhs && @rhs == i.rhs
|
288
|
-
end
|
289
|
-
alias == eql?
|
290
|
-
end
|
291
|
-
end
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
module Depager
|
3
|
+
EPS = 0x3fffffff #epsilon
|
4
|
+
|
5
|
+
class Grammar
|
6
|
+
attr_accessor :rulelist, :lhs_to_rule, :syms, :nonterms, :terms
|
7
|
+
attr_accessor :precs, :f0e, :first1
|
8
|
+
|
9
|
+
def initialize rulelist, terms, nonterms, precs = nil
|
10
|
+
@precs = precs
|
11
|
+
@nonterms = nonterms.invert
|
12
|
+
@terms = terms.invert
|
13
|
+
@syms = nonterms.invert.merge(terms.invert)
|
14
|
+
|
15
|
+
@rulelist = rulelist
|
16
|
+
@lhs_to_rule = []; @nonterms.size.times{|i| @lhs_to_rule[i] = []}
|
17
|
+
@empty_rules = {}
|
18
|
+
@rulelist.each_with_index do |rule, rx|
|
19
|
+
rule.grammar = self
|
20
|
+
rule.n = rx
|
21
|
+
@lhs_to_rule[rule.lhs] << rule
|
22
|
+
if rule.rhs.empty?
|
23
|
+
@empty_rules[rule.lhs] = rule.n
|
24
|
+
end
|
25
|
+
end
|
26
|
+
make_sym_mask
|
27
|
+
make_sym_first
|
28
|
+
initialize_depend
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize_depend
|
32
|
+
end
|
33
|
+
|
34
|
+
def make_sym_mask
|
35
|
+
@sym_mask, @mask_sym = {}, []
|
36
|
+
@terms.sort_by{|a, b| a}.each_with_index do |i,x|
|
37
|
+
@sym_mask[i[0]] = 1 << x
|
38
|
+
@mask_sym[x] = i[0]
|
39
|
+
end
|
40
|
+
|
41
|
+
@sym_mask[EPS] = (1 << @sym_mask.size)
|
42
|
+
@mask_sym << EPS
|
43
|
+
end
|
44
|
+
|
45
|
+
def symset syms=nil
|
46
|
+
ss = SymbolSet.new(self)
|
47
|
+
syms ? ss.concat_array(syms) : ss
|
48
|
+
end
|
49
|
+
|
50
|
+
def sym_mask sym
|
51
|
+
@sym_mask[sym]
|
52
|
+
end
|
53
|
+
|
54
|
+
def mask_size
|
55
|
+
@mask_sym.size
|
56
|
+
end
|
57
|
+
|
58
|
+
def mask_sym
|
59
|
+
@mask_sym
|
60
|
+
end
|
61
|
+
|
62
|
+
def make_sym_first
|
63
|
+
@first1 = { }
|
64
|
+
@syms.each_key do |s|
|
65
|
+
@first1[s] = symset()
|
66
|
+
|
67
|
+
@first1[s] << s if term? s
|
68
|
+
@first1[s] << EPS if @empty_rules[s]
|
69
|
+
end
|
70
|
+
|
71
|
+
begin
|
72
|
+
changed = false
|
73
|
+
@nonterms.size.times do |lhs|
|
74
|
+
@lhs_to_rule[lhs].each do |rule|
|
75
|
+
rule.rhs.each do |s|
|
76
|
+
unless @first1[s].subset_of?(@first1[lhs])
|
77
|
+
# warn "%b\n%b" % [@first1[s].set, @first1[lhs].set]
|
78
|
+
#warn "update{"
|
79
|
+
#warn " R:#{symname(s)}:#{@first1[s].inspect}"
|
80
|
+
#warn " L:#{symname(lhs)}:#{@first1[lhs].inspect}"
|
81
|
+
@first1[lhs].merge! @first1[s]
|
82
|
+
#warn " L:#{symname(lhs)}:#{@first1[lhs].inspect}"
|
83
|
+
#warn "}"
|
84
|
+
changed = true
|
85
|
+
end
|
86
|
+
break unless @first1[s].include? EPS
|
87
|
+
end
|
88
|
+
unless @first1[lhs].include? EPS
|
89
|
+
if rule.rhs.all?{|i| @first1[i].include? EPS }
|
90
|
+
@first1[lhs] << EPS
|
91
|
+
changed = true
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end while changed
|
97
|
+
end
|
98
|
+
|
99
|
+
def first p
|
100
|
+
return symset([EPS]) if p.empty?
|
101
|
+
r, x = symset(), 0
|
102
|
+
p.each do |i|
|
103
|
+
unless i == EPS
|
104
|
+
r.merge! @first1[i]
|
105
|
+
break unless @first1[i].include? EPS
|
106
|
+
end
|
107
|
+
x += 1
|
108
|
+
end
|
109
|
+
r.delete EPS
|
110
|
+
r << EPS if x == p.size
|
111
|
+
|
112
|
+
return r
|
113
|
+
end
|
114
|
+
|
115
|
+
def term? i
|
116
|
+
i && i >= @nonterms.size
|
117
|
+
end
|
118
|
+
|
119
|
+
def nonterm? i
|
120
|
+
i && i < @nonterms.size
|
121
|
+
end
|
122
|
+
|
123
|
+
def [](n)
|
124
|
+
@rulelist[n]
|
125
|
+
end
|
126
|
+
|
127
|
+
def symname sym
|
128
|
+
name = @syms[sym]
|
129
|
+
return nil unless sym.is_a? Integer
|
130
|
+
return '$start' if sym == 0
|
131
|
+
return '$empty' if sym == EPS
|
132
|
+
return '$end' if name == nil
|
133
|
+
return '$error' if name == false
|
134
|
+
return "#{name}" if name.is_a? Symbol
|
135
|
+
return "'#{name}'"
|
136
|
+
end
|
137
|
+
|
138
|
+
class SymbolSet
|
139
|
+
include Enumerable
|
140
|
+
attr_accessor :set
|
141
|
+
|
142
|
+
def initialize g, set=0
|
143
|
+
raise g.class.name unless g.is_a? Depager::Grammar
|
144
|
+
@grammar = g
|
145
|
+
@set = set
|
146
|
+
end
|
147
|
+
|
148
|
+
def concat_array set
|
149
|
+
set.each do |i|
|
150
|
+
self << i
|
151
|
+
end
|
152
|
+
self
|
153
|
+
end
|
154
|
+
|
155
|
+
def << sym
|
156
|
+
@set |= @grammar.sym_mask(sym)
|
157
|
+
self
|
158
|
+
end
|
159
|
+
|
160
|
+
def & symset
|
161
|
+
SymbolSet.new(@grammar, @set & symset.set)
|
162
|
+
end
|
163
|
+
|
164
|
+
def | symset
|
165
|
+
SymbolSet.new(@grammar, @set | symset.set)
|
166
|
+
end
|
167
|
+
|
168
|
+
def merge! symset
|
169
|
+
@set |= symset.set
|
170
|
+
self
|
171
|
+
end
|
172
|
+
|
173
|
+
def - symset
|
174
|
+
SymbolSet.new(@grammar, @set ^ symset.set & @set)
|
175
|
+
end
|
176
|
+
|
177
|
+
def subset_of? symset
|
178
|
+
# "a is a subset of b" is a-b={}
|
179
|
+
d = (@set ^ symset.set & @set)
|
180
|
+
d == 0 ? true : false
|
181
|
+
end
|
182
|
+
|
183
|
+
def empty?
|
184
|
+
@set == 0
|
185
|
+
end
|
186
|
+
|
187
|
+
def include? sym
|
188
|
+
(@set & @grammar.sym_mask(sym) != 0) ? true : false
|
189
|
+
end
|
190
|
+
|
191
|
+
def delete sym
|
192
|
+
old = @set
|
193
|
+
@set = @set & ~@grammar.sym_mask(sym)
|
194
|
+
old != @set ? sym : nil
|
195
|
+
end
|
196
|
+
|
197
|
+
def each &block
|
198
|
+
mask_sym = @grammar.mask_sym
|
199
|
+
@grammar.mask_size.times do |x|
|
200
|
+
@set[x] == 1 and yield mask_sym[x]
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
def inspect
|
205
|
+
self.map{|i| @grammar.symname(i)}.inspect
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
class Rule
|
211
|
+
def self.[] *args
|
212
|
+
self.new *args
|
213
|
+
end
|
214
|
+
|
215
|
+
attr_accessor :lhs, :rhs, :grammar, :n, :prec, :rhs_names
|
216
|
+
|
217
|
+
def initialize lhs, rhs, prec = nil, rhs_names = nil
|
218
|
+
@lhs = lhs
|
219
|
+
@rhs = rhs
|
220
|
+
@prec = prec
|
221
|
+
@rhs_names = rhs_names
|
222
|
+
|
223
|
+
@grammar = nil
|
224
|
+
@n = 0
|
225
|
+
|
226
|
+
initialize_depend
|
227
|
+
end
|
228
|
+
|
229
|
+
def initialize_depend
|
230
|
+
end
|
231
|
+
|
232
|
+
def lhs_name
|
233
|
+
grammar.symname @lhs
|
234
|
+
end
|
235
|
+
|
236
|
+
def rhs_name_to_sym name
|
237
|
+
@rhs and @rhs[@rhs_names.index(name)] rescue nil
|
238
|
+
end
|
239
|
+
|
240
|
+
def to_s
|
241
|
+
lhs = grammar.symname @lhs
|
242
|
+
rhs = @rhs.map{|i| grammar.symname i}.join(' ')
|
243
|
+
|
244
|
+
"(#{'%03s' % @n}) #{lhs_name} : #{rhs}"
|
245
|
+
end
|
246
|
+
|
247
|
+
def hash
|
248
|
+
1117 * @rhs.hash + @lhs.hash % 3037
|
249
|
+
end
|
250
|
+
|
251
|
+
def eql? i
|
252
|
+
@lhs == i.lhs && @rhs == i.rhs
|
253
|
+
end
|
254
|
+
alias == eql?
|
255
|
+
end
|
256
|
+
end
|