depager 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +4 -0
- data/README.ja +27 -28
- data/examples/c89/c89.dr +34 -34
- data/examples/c89/c89.tab.rb +3074 -3074
- data/examples/extension/paction.dr +4 -4
- data/examples/extension/pactiontest.dr +1 -1
- data/examples/pl0d/pl0ds.dr +27 -27
- data/examples/pl0d/pl0ds.tab.rb +626 -643
- data/examples/sample_calc/calc.action.dr +6 -6
- data/examples/sample_calc/calc.action.tab.rb +90 -90
- data/examples/sample_calc/calc.ast.action.dr +7 -7
- data/examples/sample_calc/calc.ast.action.tab.rb +121 -127
- data/examples/sample_calc/calc.ast.dr +6 -6
- data/examples/sample_calc/calc.ast.tab.rb +102 -109
- data/examples/sample_calc/calc.astdf.dr +6 -6
- data/examples/sample_calc/calc.astdf.tab.rb +102 -109
- data/examples/sample_calc/calc.atree.dr +6 -6
- data/examples/sample_calc/calc.atree.tab.rb +90 -90
- data/examples/sample_calc/calc.cst.dr +5 -5
- data/examples/sample_calc/calc.cst.tab.rb +106 -106
- data/examples/sample_calc/calc.dr +1 -1
- data/examples/sample_calc/calc.lex.dr +4 -4
- data/examples/sample_calc/calc.lex.tab.rb +73 -73
- data/examples/sample_calc/calc.nvaction.dr +6 -6
- data/examples/sample_calc/calc.nvaction.tab.rb +90 -90
- data/examples/sample_calc/calc.tab.rb +71 -71
- data/examples/sample_calc/calc_prec.nvaction.dr +6 -6
- data/examples/sample_calc/calc_prec.nvaction.tab.rb +46 -46
- data/examples/slex_test/divreg.slex.dr +7 -7
- data/examples/slex_test/divreg.slex.tab.rb +20 -20
- data/examples/slex_test/ljoin.slex.dr +7 -7
- data/examples/slex_test/ljoin.slex.tab.rb +15 -15
- data/lib/depager.rb +45 -83
- data/lib/depager/grammar.rb +3 -7
- data/lib/depager/lr.rb +123 -1
- data/lib/depager/parser.rb +29 -48
- data/lib/depager/{template/ast.erbs → ruby/plugins/_ast_tmpl.rb} +11 -7
- data/lib/depager/{action.rb → ruby/plugins/action.rb} +7 -11
- data/lib/depager/{ast.dr → ruby/plugins/ast.dr} +24 -25
- data/lib/depager/{ast.rb → ruby/plugins/ast.rb} +241 -243
- data/lib/depager/{astdf.rb → ruby/plugins/astdf.rb} +1 -2
- data/lib/depager/{atree.dr → ruby/plugins/atree.dr} +5 -5
- data/lib/depager/{atree.rb → ruby/plugins/atree.rb} +39 -39
- data/lib/depager/{cst.dr → ruby/plugins/cst.dr} +17 -21
- data/lib/depager/{cst.rb → ruby/plugins/cst.rb} +62 -68
- data/lib/depager/{lex.dr → ruby/plugins/lex.dr} +3 -4
- data/lib/depager/{lex.rb → ruby/plugins/lex.rb} +29 -31
- data/lib/depager/{nvaction.rb → ruby/plugins/nvaction.rb} +1 -3
- data/lib/depager/{slex.dr → ruby/plugins/slex.dr} +16 -17
- data/lib/depager/{slex.rb → ruby/plugins/slex.rb} +115 -117
- data/lib/depager/{srp.rb → ruby/plugins/srp.rb} +4 -4
- data/lib/depager/{template → ruby/templates}/extension_lalr_master.erb +6 -6
- data/lib/depager/{template → ruby/templates}/extension_lalr_slave.erb +0 -0
- data/lib/depager/{template → ruby/templates}/simple.erb +0 -0
- data/lib/depager/{template → ruby/templates}/single_lalr_parser.erb +0 -0
- data/lib/depager/utils.rb +30 -69
- data/lib/depager/version.rb +1 -1
- metadata +59 -56
- data/examples/Rakefile +0 -36
- data/lib/depager/Rakefile +0 -34
- data/lib/depager/lr_put_table.rb +0 -116
- data/lib/depager/parse_action.rb +0 -24
@@ -20,7 +20,7 @@ class ShiftReducePrinter < Depager::LALR::AdvancedParser
|
|
20
20
|
end
|
21
21
|
r
|
22
22
|
end
|
23
|
-
def
|
23
|
+
def after_shift
|
24
24
|
st0 = _stack[_stack.size-3]
|
25
25
|
st = _stack.last
|
26
26
|
sh = @i2t[_stack[_stack.size-2][0]]
|
@@ -29,7 +29,7 @@ class ShiftReducePrinter < Depager::LALR::AdvancedParser
|
|
29
29
|
warn "LINE:<#{@basis.line}>"
|
30
30
|
warn dumpstack(_stack) + "\n"
|
31
31
|
end
|
32
|
-
def
|
32
|
+
def after_reduce
|
33
33
|
st = _stack.last
|
34
34
|
re = @i2nt[_stack[_stack.size-2][0]]
|
35
35
|
la = @i2t[_lookahead[0]]
|
@@ -37,10 +37,10 @@ class ShiftReducePrinter < Depager::LALR::AdvancedParser
|
|
37
37
|
warn "LINE:<#{@basis.line}>"
|
38
38
|
warn dumpstack(_stack) + "\n"
|
39
39
|
end
|
40
|
-
def
|
40
|
+
def after_accept
|
41
41
|
warn "acc."
|
42
42
|
end
|
43
|
-
def
|
43
|
+
def after_error
|
44
44
|
st = _stack.last
|
45
45
|
re = @i2nt[_stack[_stack.size-2][0]]
|
46
46
|
la = @i2t[_lookahead[0]]
|
@@ -34,12 +34,12 @@ class <%= target_name %> #:nodoc:all
|
|
34
34
|
|
35
35
|
def __regext__ p
|
36
36
|
super p, <%= paramkey %>, nil
|
37
|
-
<% x = 0; h = {}; regs.each do |i, m
|
38
|
-
<%unless h[m
|
39
|
-
@h2p['<%=m
|
40
|
-
%># use @hook[<%=h[m
|
41
|
-
p
|
42
|
-
<% x+=1; end
|
37
|
+
<% x = 0; h = {}; regs.each do |i, m| %>
|
38
|
+
<% unless h[m] %>@hook[<%=x%>] = <%= m %>.new(p, @nparam, self)<%h[m]=x%>
|
39
|
+
@h2p['<%= m.sub(/#{target_name}_/,'')%>'] = @hook[<%=h[m]%>] <%else
|
40
|
+
%># use @hook[<%=h[m]%>](<%=m%>)<%end%>
|
41
|
+
p.hooks[:<%=i%>].push [@hook[<%=h[m]%>], :do_parse]
|
42
|
+
<% x+=1; end %>
|
43
43
|
regext p
|
44
44
|
end
|
45
45
|
|
File without changes
|
File without changes
|
File without changes
|
data/lib/depager/utils.rb
CHANGED
@@ -129,39 +129,35 @@ module Depager
|
|
129
129
|
unindent(code).sub!('$$!!@ BODY @!!$$', body)
|
130
130
|
end
|
131
131
|
|
132
|
-
#
|
133
|
-
# generate code from template
|
134
|
-
#
|
135
|
-
# tmpl:: template symbol
|
136
|
-
# b:: binding
|
137
|
-
# lineno:: line no
|
138
|
-
# d:: input line no - output line no
|
139
|
-
#
|
140
|
-
def gen_tmpl_code tmpl, b, lineno=nil, d=nil
|
141
|
-
erb = ERB.new(self.class::Template[tmpl])
|
142
|
-
erb.result(b)
|
143
|
-
end
|
144
|
-
|
145
|
-
#
|
146
|
-
# make template from file
|
147
|
-
#
|
148
|
-
# fname:: template file name
|
149
|
-
#
|
150
|
-
def make_tmpl fname
|
151
|
-
tmpl = File.read(fname).split(/===/)
|
152
|
-
tmpl = tmpl.map{|i| i.match(/^:(\S+)/) and [$1.intern, $'] }
|
153
|
-
Hash[*tmpl.compact.flatten]
|
154
|
-
end
|
155
|
-
module_function :make_tmpl
|
156
|
-
|
157
132
|
# unindent
|
158
133
|
def unindent s
|
159
134
|
s = s.join rescue s.to_s
|
160
135
|
ind = s.match(/\n(\s+)/)
|
161
136
|
s.gsub(/^#{ind[1]}/m, '')
|
162
137
|
end
|
163
|
-
end
|
164
138
|
|
139
|
+
def parse_action
|
140
|
+
val = ''
|
141
|
+
start_lineno = lineno
|
142
|
+
if @line =~ /\A\s*\{(.*)\}\s*(#.*)?\Z/
|
143
|
+
val = " #{$1}\n"
|
144
|
+
else
|
145
|
+
val.concat @line.sub(/\A\s*\{/, '')
|
146
|
+
ind = @line0.match(/\A([ \t]*)/)[1]
|
147
|
+
until file.eof?
|
148
|
+
getline
|
149
|
+
break if line =~ /\A#{ind}\}\s*(#.*)?\Z/
|
150
|
+
val.concat line
|
151
|
+
end
|
152
|
+
if file.eof?
|
153
|
+
error_exit 'syntax error(parse_action).', start_lineno
|
154
|
+
end
|
155
|
+
end
|
156
|
+
@line = $'
|
157
|
+
# warn "--action--\n#{val}"
|
158
|
+
return val
|
159
|
+
end
|
160
|
+
end
|
165
161
|
|
166
162
|
#
|
167
163
|
# Utilities for Extension
|
@@ -172,7 +168,6 @@ module Depager
|
|
172
168
|
def g_parser
|
173
169
|
@g_parser
|
174
170
|
end
|
175
|
-
alias gp g_parser # for compat
|
176
171
|
|
177
172
|
# master parser
|
178
173
|
def master
|
@@ -184,27 +179,11 @@ module Depager
|
|
184
179
|
@g_parser.target_name
|
185
180
|
end
|
186
181
|
|
187
|
-
# get current token
|
188
|
-
def token
|
189
|
-
@g_parser.token
|
190
|
-
end
|
191
|
-
|
192
182
|
# get current line
|
193
183
|
def line
|
194
184
|
@g_parser.line
|
195
185
|
end
|
196
186
|
|
197
|
-
# fix current line using s
|
198
|
-
# s:: fix string
|
199
|
-
def fixline s
|
200
|
-
@g_parser.line = s
|
201
|
-
end
|
202
|
-
|
203
|
-
# get next token
|
204
|
-
def gettoken
|
205
|
-
@g_parser.gettoken
|
206
|
-
end
|
207
|
-
|
208
187
|
# end of file?
|
209
188
|
def eof?
|
210
189
|
@g_parser.eof?
|
@@ -260,13 +239,11 @@ module Depager
|
|
260
239
|
|
261
240
|
protected
|
262
241
|
# initialize method
|
263
|
-
def init_parser
|
264
|
-
end
|
242
|
+
def init_parser ; end
|
265
243
|
|
266
244
|
# do default action
|
267
245
|
# for generated extensions
|
268
|
-
def do_default
|
269
|
-
end
|
246
|
+
def do_default ; end
|
270
247
|
|
271
248
|
# do default action?
|
272
249
|
# for generated extensions
|
@@ -281,30 +258,17 @@ module Depager
|
|
281
258
|
init_parser
|
282
259
|
@line = @g_parser.oldline
|
283
260
|
if do_parse?
|
284
|
-
ret = @dect.
|
285
|
-
@g_parser.
|
286
|
-
@g_parser.gettoken
|
261
|
+
ret = @dect.parse @g_parser.files
|
262
|
+
@g_parser.update_context @line
|
287
263
|
ret
|
288
264
|
else
|
289
265
|
do_default
|
290
266
|
end
|
291
267
|
end
|
292
268
|
|
293
|
-
Hooks = {
|
294
|
-
"prerulelist" => true,
|
295
|
-
"postrulelist" => true,
|
296
|
-
"prerule" => true,
|
297
|
-
"postrule" => true,
|
298
|
-
"postlhs" => true,
|
299
|
-
"postrhslist" => true,
|
300
|
-
"prerhslist" => true,
|
301
|
-
"prerhs" => true,
|
302
|
-
"postrhs" => true,
|
303
|
-
} #:nodoc:
|
304
|
-
|
305
269
|
# register extension (for users)
|
306
|
-
def regext p
|
307
|
-
|
270
|
+
def regext p ; end
|
271
|
+
|
308
272
|
# register extension (default)
|
309
273
|
def __regext__ p, key=:default, reg_p=true
|
310
274
|
@g_parser = @p = p
|
@@ -313,11 +277,8 @@ module Depager
|
|
313
277
|
when :default ; @g_parser.getnparam(paramkey)
|
314
278
|
else ; @g_parser.getnparam(key)
|
315
279
|
end
|
316
|
-
self.methods.sort.each do |m|
|
317
|
-
|
318
|
-
if Hooks[h]
|
319
|
-
p.send(h).push [self, m.intern]
|
320
|
-
end
|
280
|
+
self.methods.sort.each do |m| m = m.to_sym
|
281
|
+
p.hooks[m].push [self, m] if GrammarParser.hook_name? m
|
321
282
|
end
|
322
283
|
regext p if reg_p
|
323
284
|
end
|
data/lib/depager/version.rb
CHANGED
metadata
CHANGED
@@ -1,33 +1,26 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.0
|
3
|
-
specification_version: 1
|
4
2
|
name: depager
|
5
3
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.2.
|
7
|
-
date: 2008-05-05 00:00:00 +09:00
|
8
|
-
summary: A self extensible parser generator.
|
9
|
-
require_paths:
|
10
|
-
- lib
|
11
|
-
email: maita@fujiyama.slis.tsukuba.ac.jp
|
12
|
-
homepage: http://depager.rubyforge.org
|
13
|
-
rubyforge_project: depager
|
14
|
-
description: A self extensible parser generator.
|
15
|
-
autorequire:
|
16
|
-
default_executable:
|
17
|
-
bindir: bin
|
18
|
-
has_rdoc: true
|
19
|
-
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.0.0
|
24
|
-
version:
|
4
|
+
version: 0.2.3
|
25
5
|
platform: ruby
|
26
|
-
signing_key:
|
27
|
-
cert_chain:
|
28
|
-
post_install_message:
|
29
6
|
authors:
|
30
7
|
- maita
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-11-24 00:00:00 +09:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A self extensible parser generator.
|
17
|
+
email: maita@fujiyama.slis.tsukuba.ac.jp
|
18
|
+
executables:
|
19
|
+
- depager
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
31
24
|
files:
|
32
25
|
- ChangeLog
|
33
26
|
- README.en
|
@@ -35,7 +28,6 @@ files:
|
|
35
28
|
- bin/depager
|
36
29
|
- data/depager/misc/depager-mode.el
|
37
30
|
- data/depager/pre-setup.rb
|
38
|
-
- examples/Rakefile
|
39
31
|
- examples/c89/c89.dr
|
40
32
|
- examples/c89/c89.tab.rb
|
41
33
|
- examples/c89/test.c89
|
@@ -72,45 +64,56 @@ files:
|
|
72
64
|
- examples/slex_test/test.divreg
|
73
65
|
- examples/slex_test/test.ljoin
|
74
66
|
- lib/depager.rb
|
75
|
-
- lib/depager/Rakefile
|
76
|
-
- lib/depager/action.rb
|
77
|
-
- lib/depager/ast.dr
|
78
|
-
- lib/depager/ast.rb
|
79
|
-
- lib/depager/astdf.rb
|
80
|
-
- lib/depager/atree.dr
|
81
|
-
- lib/depager/atree.rb
|
82
|
-
- lib/depager/cst.dr
|
83
|
-
- lib/depager/cst.rb
|
84
67
|
- lib/depager/grammar.rb
|
85
|
-
- lib/depager/lex.dr
|
86
|
-
- lib/depager/lex.rb
|
87
68
|
- lib/depager/lr.rb
|
88
|
-
- lib/depager/lr_put_table.rb
|
89
|
-
- lib/depager/nvaction.rb
|
90
|
-
- lib/depager/parse_action.rb
|
91
69
|
- lib/depager/parser.rb
|
92
|
-
- lib/depager/
|
93
|
-
- lib/depager/
|
94
|
-
- lib/depager/
|
95
|
-
- lib/depager/
|
96
|
-
- lib/depager/
|
97
|
-
- lib/depager/
|
98
|
-
- lib/depager/
|
99
|
-
- lib/depager/
|
70
|
+
- lib/depager/ruby/plugins/_ast_tmpl.rb
|
71
|
+
- lib/depager/ruby/plugins/action.rb
|
72
|
+
- lib/depager/ruby/plugins/ast.dr
|
73
|
+
- lib/depager/ruby/plugins/ast.rb
|
74
|
+
- lib/depager/ruby/plugins/astdf.rb
|
75
|
+
- lib/depager/ruby/plugins/atree.dr
|
76
|
+
- lib/depager/ruby/plugins/atree.rb
|
77
|
+
- lib/depager/ruby/plugins/cst.dr
|
78
|
+
- lib/depager/ruby/plugins/cst.rb
|
79
|
+
- lib/depager/ruby/plugins/lex.dr
|
80
|
+
- lib/depager/ruby/plugins/lex.rb
|
81
|
+
- lib/depager/ruby/plugins/nvaction.rb
|
82
|
+
- lib/depager/ruby/plugins/slex.dr
|
83
|
+
- lib/depager/ruby/plugins/slex.rb
|
84
|
+
- lib/depager/ruby/plugins/srp.rb
|
85
|
+
- lib/depager/ruby/templates/extension_lalr_master.erb
|
86
|
+
- lib/depager/ruby/templates/extension_lalr_slave.erb
|
87
|
+
- lib/depager/ruby/templates/simple.erb
|
88
|
+
- lib/depager/ruby/templates/single_lalr_parser.erb
|
100
89
|
- lib/depager/utils.rb
|
101
90
|
- lib/depager/version.rb
|
102
91
|
- setup.rb
|
103
|
-
|
104
|
-
|
92
|
+
has_rdoc: true
|
93
|
+
homepage: http://depager.rubyforge.org
|
94
|
+
post_install_message:
|
105
95
|
rdoc_options: []
|
106
96
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: "0"
|
104
|
+
version:
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: "0"
|
110
|
+
version:
|
113
111
|
requirements: []
|
114
112
|
|
115
|
-
|
113
|
+
rubyforge_project: depager
|
114
|
+
rubygems_version: 1.0.1
|
115
|
+
signing_key:
|
116
|
+
specification_version: 2
|
117
|
+
summary: A self extensible parser generator.
|
118
|
+
test_files: []
|
116
119
|
|
data/examples/Rakefile
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
Dir.chdir(File.dirname(__FILE__))
|
2
|
-
|
3
|
-
require 'rake/clean'
|
4
|
-
|
5
|
-
DIRS = ['pl0d', 'sample_calc', 'slex_test', 'c89']
|
6
|
-
SRC = DIRS.map{|d| Dir["#{d}/*.dr"] }.flatten
|
7
|
-
OBJ = SRC.map{|i| i.sub(/\.dr$/, '.tab.rb')}
|
8
|
-
CLEAN.include(OBJ)
|
9
|
-
|
10
|
-
LIBDIR = "../lib"
|
11
|
-
BINDIR = "../bin"
|
12
|
-
|
13
|
-
SRC.each do |fn|
|
14
|
-
fn.match(/^([^\/]+)\/([^.]+)(\..+)?\.dr$/)
|
15
|
-
dir = $1
|
16
|
-
base = $2
|
17
|
-
dst = "#{dir}/#{$2}#{$3}.tab.rb"
|
18
|
-
exts = $3 && $3.split('.')[1 .. -1]
|
19
|
-
depends = exts ? exts.map{|i| "#{LIBDIR}/depager/#{i}.rb" } : []
|
20
|
-
depends = depends.unshift fn
|
21
|
-
test_ext = base.split('_')[0]
|
22
|
-
|
23
|
-
# puts %!#{dst} =>\n #{depends.join("\n ")}!
|
24
|
-
file dst => depends do
|
25
|
-
sh "ruby -I#{LIBDIR} #{BINDIR}/depager -g -o #{dst} #{fn}"
|
26
|
-
# raise unless $! == 0
|
27
|
-
Dir["#{dir}/*.#{test_ext}"].each do |t|
|
28
|
-
sh "ruby -I#{LIBDIR} #{dst} < #{t}"
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
task :default => OBJ
|
34
|
-
DIRS.each do |d|
|
35
|
-
task d => OBJ.select{|i| i =~ /^#{d}\// }
|
36
|
-
end
|
data/lib/depager/Rakefile
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
Dir.chdir(File.dirname(__FILE__))
|
2
|
-
|
3
|
-
require 'rake/clean'
|
4
|
-
|
5
|
-
SRC = FileList["*.dr"]
|
6
|
-
SRC.delete('lex.dr')
|
7
|
-
SRC.unshift('lex.dr')
|
8
|
-
OBJ = SRC.ext('rb')
|
9
|
-
CLEAN.include(OBJ)
|
10
|
-
# CLOBBER.include('extensions')
|
11
|
-
PREFIX="../.."
|
12
|
-
LIBDIR = "#{PREFIX}/lib"
|
13
|
-
BINDIR = "#{PREFIX}/bin"
|
14
|
-
|
15
|
-
task :default => OBJ
|
16
|
-
|
17
|
-
SRC.each do |fn|
|
18
|
-
fn = fn.sub(/\.[^.]*$/, '')
|
19
|
-
depends = [
|
20
|
-
"#{fn}.dr",
|
21
|
-
'lex.dr',
|
22
|
-
'nvaction.rb',
|
23
|
-
'Rakefile',
|
24
|
-
'template/extension_lalr_master.erb',
|
25
|
-
'template/extension_lalr_slave.erb',
|
26
|
-
].uniq
|
27
|
-
file("#{fn}.rb" => depends) do
|
28
|
-
sh "ruby -I#{LIBDIR} #{BINDIR}/depager -g -o #{fn}.rb #{fn}.dr"
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
file ".extensions" => OBJ do
|
33
|
-
#sh "touch .extensions"
|
34
|
-
end
|
data/lib/depager/lr_put_table.rb
DELETED
@@ -1,116 +0,0 @@
|
|
1
|
-
class Depager::LALR::Table
|
2
|
-
def state_info
|
3
|
-
@state_info
|
4
|
-
end
|
5
|
-
|
6
|
-
#puts grammar.closure.values.join("\n")
|
7
|
-
#puts "-----"
|
8
|
-
#puts grammar.memo_closure1.sort_by{|k,v| k}.map{|k,v| "#{k.inspect}:\n#{v.join("\n")}"}
|
9
|
-
#puts 'mk'; mk.each{|k, v| p "#{k}=>#{v}"}
|
10
|
-
#trn = trn.uniq
|
11
|
-
#puts 'trn',trn.map{|a| "<#{a[2].n}>#{a[0]} => <#{a[3].n}>#{a[1]}"}
|
12
|
-
|
13
|
-
def gen_state_info
|
14
|
-
g = grammar
|
15
|
-
@state_info = []
|
16
|
-
@action_table.each_with_index {|s,x|
|
17
|
-
shi, red, acc = "", "", ""
|
18
|
-
s.each_with_index {|t, y|
|
19
|
-
next unless t
|
20
|
-
hd = (" %-15s" % g.symname(y + g.nonterms.size))
|
21
|
-
if t == 'ACC'
|
22
|
-
acc = "#{hd} accept\n\n"
|
23
|
-
elsif t < 0
|
24
|
-
red << "#{hd} reduce using rule #{-t} (#{g.symname g[-t].lhs})\n"
|
25
|
-
else
|
26
|
-
shi << "#{hd} shift, and goto to state #{t}\n"
|
27
|
-
end
|
28
|
-
}
|
29
|
-
if t = @defred_table[x]
|
30
|
-
as = @defred_after_shift_table[x] ? " [after shift]" : ""
|
31
|
-
red << (" %-15s" % '$default') <<
|
32
|
-
"reduce using rule #{-t} (#{g.symname g[-t].lhs}) #{as}\n"
|
33
|
-
end
|
34
|
-
|
35
|
-
@state_info << (@states[x].to_s << "\n\n#{shi}\n#{red}\n#{acc}")
|
36
|
-
}
|
37
|
-
end
|
38
|
-
|
39
|
-
def verbose dp
|
40
|
-
g, output = grammar, []
|
41
|
-
|
42
|
-
if $MP_DEBUG.match(/f/)
|
43
|
-
output << "** FIRST1 **"
|
44
|
-
str = g.first1.map{|k, v|
|
45
|
-
"#{g.symname k} => #{v.map{|i| g.symname i}.join(' ')}"
|
46
|
-
}.join("\n")
|
47
|
-
output << "#{str}\n\n"
|
48
|
-
end
|
49
|
-
|
50
|
-
if $MP_DEBUG.match(/e/)
|
51
|
-
output << "** Empty Reduction **"
|
52
|
-
str = g.f0e.map {|k, v|
|
53
|
-
"#{g.symname k} =>\n" << v.map{|n, f|
|
54
|
-
" #{rulelist[n]} ? #{f.map{|i| g.symname i}.join(' ')}"
|
55
|
-
}.join("\n")
|
56
|
-
}.join("\n")
|
57
|
-
output << "#{str}\n\n"
|
58
|
-
end
|
59
|
-
|
60
|
-
if $MP_DEBUG.match(/s/)
|
61
|
-
output << "** SYMBOLS **"
|
62
|
-
str = g.syms.map{|k,_| "#{"%03i" % k} #{ g.symname k}"}.sort.join("\n")
|
63
|
-
output << "#{str}\n\n"
|
64
|
-
end
|
65
|
-
|
66
|
-
if $MP_DEBUG.match(/g/)
|
67
|
-
output << "*** Grammar ***"
|
68
|
-
str = g.rulelist.join("\n")
|
69
|
-
output << "#{str}\n\n"
|
70
|
-
end
|
71
|
-
|
72
|
-
if $MP_DEBUG.match(/c/)
|
73
|
-
output << "*** States ***"
|
74
|
-
output << @state_info.to_s
|
75
|
-
end
|
76
|
-
|
77
|
-
nssize = g.nonterms.size
|
78
|
-
if $MP_DEBUG.match(/t/)
|
79
|
-
output << "*** Action Table ***"
|
80
|
-
ws = (nssize...g.syms.size).map{|i| j = g.symname(i).size; j < 6 ? 6 : j}
|
81
|
-
str = " |"
|
82
|
-
(nssize...g.syms.size).each_with_index{|i, x|
|
83
|
-
str << ("%0#{ws[x]}s|" % g.symname(i))
|
84
|
-
}; str << " $default|\n"
|
85
|
-
|
86
|
-
@action_table.each_with_index{|i,x|
|
87
|
-
str << ("%03i|" % x)
|
88
|
-
i.each_with_index{|j, y|
|
89
|
-
str << ("%0#{ws[y]}s|" % j)
|
90
|
-
}
|
91
|
-
str << ("%04s,%04s|\n" % [@defred_table[x], @defred_after_shift_table[x]])
|
92
|
-
}
|
93
|
-
output << "#{str}\n\n"
|
94
|
-
|
95
|
-
output << "*** Goto Table ***"
|
96
|
-
ws = (1...nssize).map{|i| j = g.symname(i).size; j < 6 ? 6 : j}
|
97
|
-
str = " |"
|
98
|
-
(1...nssize).each_with_index{|i, x|
|
99
|
-
str << ("%0#{ws[x]}s|" % g.symname(i))
|
100
|
-
}; str << "\n"
|
101
|
-
|
102
|
-
@goto_table.each_with_index{|i,x|
|
103
|
-
str << ("%03i|" % x)
|
104
|
-
i.each_with_index{|j, y|
|
105
|
-
str << ("%0#{ws[y]}s|" % j)
|
106
|
-
}
|
107
|
-
str << "\n"
|
108
|
-
}
|
109
|
-
output << "#{str}\n\n"
|
110
|
-
end
|
111
|
-
|
112
|
-
File.open("#{File.basename(dp.files.fname, '.dr')}.output", "w"){|f|
|
113
|
-
f.write output.join("\n")
|
114
|
-
}
|
115
|
-
end
|
116
|
-
end
|