depager 0.2.0 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +4 -0
- data/README.en +5 -10
- data/bin/depager +17 -20
- data/examples/c89/c89.tab.rb +5632 -702
- data/examples/pl0d/pl0ds.dr +41 -41
- data/examples/pl0d/pl0ds.tab.rb +1887 -874
- data/examples/sample_calc/calc.action.tab.rb +243 -69
- data/examples/sample_calc/{calc.astl.action.dr → calc.ast.action.dr} +7 -7
- data/examples/sample_calc/calc.ast.action.tab.rb +755 -0
- data/examples/sample_calc/{calc.astl.dr → calc.ast.dr} +7 -7
- data/examples/sample_calc/calc.ast.tab.rb +672 -0
- data/examples/sample_calc/calc.astdf.dr +5 -5
- data/examples/sample_calc/calc.astdf.tab.rb +405 -202
- data/examples/sample_calc/calc.atree.tab.rb +243 -69
- data/examples/sample_calc/calc.cst.tab.rb +275 -109
- data/examples/sample_calc/calc.lex.tab.rb +210 -28
- data/examples/sample_calc/calc.nvaction.tab.rb +251 -77
- data/examples/sample_calc/calc.tab.rb +210 -28
- data/examples/sample_calc/calc_prec.nvaction.tab.rb +224 -50
- data/examples/slex_test/divreg.slex.tab.rb +97 -21
- data/examples/slex_test/ljoin.slex.tab.rb +128 -35
- data/lib/depager.rb +77 -44
- data/lib/depager/{ast_base.dr → ast.dr} +56 -18
- data/lib/depager/{ast_base.rb → ast.rb} +432 -424
- data/lib/depager/astdf.rb +3 -6
- data/lib/depager/atree.rb +54 -62
- data/lib/depager/cst.dr +2 -2
- data/lib/depager/cst.rb +64 -77
- data/lib/depager/grammar.rb +225 -66
- data/lib/depager/lex.dr +1 -1
- data/lib/depager/lex.rb +45 -54
- data/lib/depager/lr.rb +181 -262
- data/lib/depager/lr_put_table.rb +116 -0
- data/lib/depager/nvaction.rb +1 -1
- data/lib/depager/parser.rb +23 -2
- data/lib/depager/slex.dr +3 -3
- data/lib/depager/slex.rb +148 -169
- data/lib/depager/srp.rb +1 -1
- data/lib/depager/template/ast.erbs +69 -0
- data/lib/depager/template/extension_lalr_master.erb +3 -3
- data/lib/depager/template/extension_lalr_slave.erb +7 -7
- data/lib/depager/template/simple.erb +4 -2
- data/lib/depager/template/single_lalr_parser.erb +30 -10
- data/lib/depager/utils.rb +10 -9
- data/lib/depager/version.rb +2 -8
- metadata +10 -11
- data/examples/sample_calc/calc.astl.action.tab.rb +0 -593
- data/examples/sample_calc/calc.astl.tab.rb +0 -501
- data/lib/depager/astl.rb +0 -14
- data/lib/depager/template/astdf.erbs +0 -57
- data/lib/depager/template/astl.erbs +0 -57
data/lib/depager/srp.rb
CHANGED
@@ -0,0 +1,69 @@
|
|
1
|
+
===:defnode
|
2
|
+
class Node
|
3
|
+
attr_accessor :lineno
|
4
|
+
attr_accessor <%= _opt_attr.map{|i| ":#{i}" }.join(', ') %>
|
5
|
+
<%= ini %>
|
6
|
+
def self.[] lineno, *args
|
7
|
+
self.new lineno, *args
|
8
|
+
end
|
9
|
+
def accept v
|
10
|
+
end
|
11
|
+
end
|
12
|
+
class NodeList < Node
|
13
|
+
attr_accessor :lineno
|
14
|
+
def initialize(lineno, lst=[])
|
15
|
+
@lineno = lineno
|
16
|
+
@lst = lst.to_a.select{|i| ! i.is_a?(NilNode)}
|
17
|
+
end
|
18
|
+
<% _opt_attr.each do |i| %>
|
19
|
+
def all_<%= i %>
|
20
|
+
@lst.map{|i| i.<%= i %>}
|
21
|
+
end
|
22
|
+
<% end %>
|
23
|
+
def size
|
24
|
+
@lst.size
|
25
|
+
end
|
26
|
+
alias length size
|
27
|
+
def push(i)
|
28
|
+
@lst.push i unless i.is_a? NilNode
|
29
|
+
self
|
30
|
+
end
|
31
|
+
def concat(i)
|
32
|
+
@lst.concat i
|
33
|
+
self
|
34
|
+
end
|
35
|
+
def to_ary()
|
36
|
+
@lst
|
37
|
+
end
|
38
|
+
alias to_a to_ary
|
39
|
+
alias list to_ary
|
40
|
+
def accept(v)
|
41
|
+
@lst.each{|i| i.accept(v) }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
class NilNode
|
45
|
+
attr_accessor :lineno
|
46
|
+
attr_accessor <%= _opt_attr.map{|i| ":#{i}" }.join(', ') %>
|
47
|
+
def initialize(lineno, *args)
|
48
|
+
@lineno = lineno
|
49
|
+
end
|
50
|
+
def accept v
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
===:node
|
55
|
+
class Node_<%= name %> < Node
|
56
|
+
attr_accessor <%= nodes.map{|i| ":#{i}" }.join(', ') %>
|
57
|
+
attr_accessor <%= attrs.map{|i| ":#{i}" }.join(', ') %>
|
58
|
+
def initialize <%= nodes.unshift('lineno').join(', ') %>
|
59
|
+
super()
|
60
|
+
@lineno = lineno
|
61
|
+
<%= nodes.inject(''){|r,i| r << " @#{i} = #{i}\n"} %>
|
62
|
+
end
|
63
|
+
def accept v
|
64
|
+
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
65
|
+
<%= accept %>
|
66
|
+
v.visit_Node_<%= name %>(self)
|
67
|
+
self
|
68
|
+
end
|
69
|
+
end
|
@@ -1,17 +1,17 @@
|
|
1
1
|
<%
|
2
2
|
target_name = g_parser.target_name
|
3
3
|
table = g_parser.table
|
4
|
-
g = table.
|
4
|
+
g = table.grammar
|
5
5
|
rulelist = g.rulelist
|
6
|
-
t2i = (g.
|
7
|
-
nt2i = (1 ... g.
|
6
|
+
t2i = (g.nonterms.size ... g.syms.size).map{|i| [ g.syms[i].inspect, i - g.nonterms.size] }
|
7
|
+
nt2i = (1 ... g.nonterms.size).map{|i| [ ":#{g.syms[i]}", i-1] }
|
8
8
|
taction = table.action_table
|
9
9
|
tgoto = table.goto_table
|
10
10
|
tdefred = table.defred_table
|
11
11
|
tdefreds = table.defred_after_shift_table
|
12
|
-
optouter = g_parser.optouter
|
13
|
-
optinner = g_parser.optinner
|
14
|
-
optmain = g_parser.optmain
|
12
|
+
optouter = g_parser.optouter.join
|
13
|
+
optinner = g_parser.optinner.join
|
14
|
+
optmain = g_parser.optmain.join
|
15
15
|
nparams = g_parser.nparams
|
16
16
|
decparser = @deco.inject(@basis_name) { |r, i|
|
17
17
|
"#{i.sub(/^@/, "D4#{target_name}::")}.new(#{r})"
|
@@ -32,7 +32,7 @@ class <%= target_name %> < Depager::LALR::Basis #:nodoc:all
|
|
32
32
|
|
33
33
|
### Reduce Table
|
34
34
|
reduce_table = [<% rulelist.each do |i| %>
|
35
|
-
[ <%= i.
|
35
|
+
[ <%= i.lhs - 1%>, <%= i.rhs.size %> ],<% end %>
|
36
36
|
]
|
37
37
|
### Extension Params
|
38
38
|
nparams = {<% nparams.each do |c, i| %>
|
@@ -1,6 +1,7 @@
|
|
1
1
|
<%
|
2
|
-
|
3
|
-
|
2
|
+
target_name = g_parser.target_name
|
3
|
+
optouter = (@optouter + g_parser.optouter).join
|
4
|
+
optmain = @optmain.join
|
4
5
|
req = @req.uniq
|
5
6
|
mixin = @mixin
|
6
7
|
%>
|
@@ -9,6 +10,7 @@ require 'depager/parser.rb'
|
|
9
10
|
<%req.each do |i|%>require <%=i%>
|
10
11
|
<%end%>
|
11
12
|
|
13
|
+
module Depager::DecoratorUtils; end
|
12
14
|
module D4<%= target_name %> ; end
|
13
15
|
|
14
16
|
### Outer Code
|
@@ -1,31 +1,36 @@
|
|
1
1
|
<%
|
2
2
|
target_name = g_parser.target_name
|
3
3
|
table = g_parser.table
|
4
|
-
g = table.
|
4
|
+
g = table.grammar
|
5
5
|
nparams = g_parser.nparams
|
6
6
|
rulelist = g.rulelist
|
7
|
-
t2i = (g.
|
8
|
-
nt2i = (1 ... g.
|
7
|
+
t2i = (g.nonterms.size ... g.syms.size).map{|i| [ g.syms[i].inspect, i - g.nonterms.size] }
|
8
|
+
nt2i = (1 ... g.nonterms.size).map{|i| [ ":#{g.syms[i]}", i-1] }
|
9
9
|
taction = table.action_table
|
10
10
|
tgoto = table.goto_table
|
11
11
|
tdefred = table.defred_table
|
12
12
|
tdefreds = table.defred_after_shift_table
|
13
|
-
optouter = @optouter + g_parser.optouter
|
14
|
-
optinner = @optinner + g_parser.optinner
|
15
|
-
optmain = @optmain
|
13
|
+
optouter = (@optouter + g_parser.optouter).join
|
14
|
+
optinner = (@optinner + g_parser.optinner).join
|
15
|
+
optmain = @optmain.join
|
16
16
|
decparser = @deco.inject(@basis_name) { |r, i|
|
17
17
|
"#{i.sub(/^@/, "D4#{target_name}::")}.new(#{r})"
|
18
18
|
}
|
19
19
|
req = @req.uniq
|
20
20
|
mixin = @mixin
|
21
|
+
req_rt = if @dr_option['embed_runtime']
|
22
|
+
File.read File.expand_path("#{Depager::Tmpldir}/../parser.rb")
|
23
|
+
else
|
24
|
+
"require 'depager/parser.rb'"
|
25
|
+
end
|
21
26
|
%>
|
22
27
|
begin; require 'rubygems'; rescue Exception; end
|
23
|
-
|
28
|
+
<%= req_rt %>
|
24
29
|
<%req.each do |i|%>require <%=i%>
|
25
30
|
<%end%>
|
26
31
|
|
27
|
-
module
|
28
|
-
end
|
32
|
+
module Depager::DecoratorUtils; end
|
33
|
+
module D4<%= target_name %>; end
|
29
34
|
|
30
35
|
class <%= target_name %> < Depager::LALR::Basis
|
31
36
|
<% mixin.each do |i| %>
|
@@ -33,7 +38,7 @@ class <%= target_name %> < Depager::LALR::Basis
|
|
33
38
|
|
34
39
|
### Reduce Table
|
35
40
|
reduce_table = [<% rulelist.each do |i| %>
|
36
|
-
[ <%= '%04s'%(i.
|
41
|
+
[ <%= '%04s'%(i.lhs - 1)%>, <%= "%04s"%i.rhs.size %> ], # <%= i %><% end %>
|
37
42
|
]
|
38
43
|
### Extension Params
|
39
44
|
nparams = {<% nparams.each do |c, i| %>
|
@@ -71,6 +76,21 @@ class <%= target_name %> < Depager::LALR::Basis
|
|
71
76
|
[ <% i.each do |j| %><%= j ? "#{j}" : 'nil' %>, <% end %>], <% end %>
|
72
77
|
]
|
73
78
|
|
79
|
+
<% if $MP_DEBUG %>
|
80
|
+
alias orig_error error
|
81
|
+
def error
|
82
|
+
orig_error
|
83
|
+
warn "current state: #{StateInfo[@stack.last]}"
|
84
|
+
end
|
85
|
+
|
86
|
+
### States
|
87
|
+
StateInfo = [ <% table.state_info.each do |s| %>
|
88
|
+
<<'----------',
|
89
|
+
<%=s.chomp%>
|
90
|
+
----------
|
91
|
+
<% end %>]
|
92
|
+
<% end %>
|
93
|
+
|
74
94
|
Tables = [ reduce_table, nparams, action_table,
|
75
95
|
defred_table, defred_after_shift_table, goto_table,
|
76
96
|
t2i, i2t, nt2i, i2nt ]
|
data/lib/depager/utils.rb
CHANGED
@@ -78,7 +78,7 @@ module Depager
|
|
78
78
|
#{mixin_code}
|
79
79
|
on_reduce = [
|
80
80
|
nil,
|
81
|
-
#{on_reduce_code}
|
81
|
+
#{on_reduce_code.join}
|
82
82
|
]
|
83
83
|
Tables = [on_reduce]
|
84
84
|
def initialize inside
|
@@ -89,7 +89,8 @@ module Depager
|
|
89
89
|
\n$$!! @OUTER @!!$$
|
90
90
|
end
|
91
91
|
}; #code
|
92
|
-
|
92
|
+
optouter = optouter.join if optouter.is_a? Array
|
93
|
+
unindent(code).sub!('$$!! @OUTER @!!$$', optouter)
|
93
94
|
end
|
94
95
|
|
95
96
|
#
|
@@ -116,10 +117,10 @@ module Depager
|
|
116
117
|
# lineno:: line no
|
117
118
|
# d:: input line no - output line no
|
118
119
|
#
|
119
|
-
def gen_defm_code sig, body, lineno, d=0
|
120
|
+
def gen_defm_code sig, body, lineno=1, d=0
|
120
121
|
t = Time.now.to_i
|
121
122
|
r = rand(0xffff)
|
122
|
-
body = "
|
123
|
+
body = "def #{sig}\n#{body}\nend"
|
123
124
|
code = %{
|
124
125
|
module_eval <<\-'.,.,#{t}#{r}.,.,', '#{files.path}', #{lineno-d-1}
|
125
126
|
$$!!@ BODY @!!$$
|
@@ -155,9 +156,9 @@ module Depager
|
|
155
156
|
|
156
157
|
# unindent
|
157
158
|
def unindent s
|
158
|
-
s = s.to_s
|
159
|
-
ind = s.match(/\n(\s+)/)
|
160
|
-
s.gsub(/^#{ind}/m, '')
|
159
|
+
s = s.join rescue s.to_s
|
160
|
+
ind = s.match(/\n(\s+)/)
|
161
|
+
s.gsub(/^#{ind[1]}/m, '')
|
161
162
|
end
|
162
163
|
end
|
163
164
|
|
@@ -237,7 +238,7 @@ module Depager
|
|
237
238
|
|
238
239
|
# get nunber of rules
|
239
240
|
def nrules
|
240
|
-
@g_parser.
|
241
|
+
@g_parser.rules.size - 2
|
241
242
|
end
|
242
243
|
|
243
244
|
# get nunber of rhs rules
|
@@ -313,7 +314,7 @@ module Depager
|
|
313
314
|
else ; @g_parser.getnparam(key)
|
314
315
|
end
|
315
316
|
self.methods.sort.each do |m|
|
316
|
-
h = m.downcase
|
317
|
+
h = m.to_s.downcase
|
317
318
|
if Hooks[h]
|
318
319
|
p.send(h).push [self, m.intern]
|
319
320
|
end
|
data/lib/depager/version.rb
CHANGED
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: depager
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.2.
|
7
|
-
date:
|
6
|
+
version: 0.2.2
|
7
|
+
date: 2008-05-05 00:00:00 +09:00
|
8
8
|
summary: A self extensible parser generator.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -46,12 +46,12 @@ files:
|
|
46
46
|
- examples/pl0d/test.pl0ds
|
47
47
|
- examples/sample_calc/calc.action.dr
|
48
48
|
- examples/sample_calc/calc.action.tab.rb
|
49
|
+
- examples/sample_calc/calc.ast.action.dr
|
50
|
+
- examples/sample_calc/calc.ast.action.tab.rb
|
51
|
+
- examples/sample_calc/calc.ast.dr
|
52
|
+
- examples/sample_calc/calc.ast.tab.rb
|
49
53
|
- examples/sample_calc/calc.astdf.dr
|
50
54
|
- examples/sample_calc/calc.astdf.tab.rb
|
51
|
-
- examples/sample_calc/calc.astl.action.dr
|
52
|
-
- examples/sample_calc/calc.astl.action.tab.rb
|
53
|
-
- examples/sample_calc/calc.astl.dr
|
54
|
-
- examples/sample_calc/calc.astl.tab.rb
|
55
55
|
- examples/sample_calc/calc.atree.dr
|
56
56
|
- examples/sample_calc/calc.atree.tab.rb
|
57
57
|
- examples/sample_calc/calc.cst.dr
|
@@ -74,10 +74,9 @@ files:
|
|
74
74
|
- lib/depager.rb
|
75
75
|
- lib/depager/Rakefile
|
76
76
|
- lib/depager/action.rb
|
77
|
-
- lib/depager/
|
78
|
-
- lib/depager/
|
77
|
+
- lib/depager/ast.dr
|
78
|
+
- lib/depager/ast.rb
|
79
79
|
- lib/depager/astdf.rb
|
80
|
-
- lib/depager/astl.rb
|
81
80
|
- lib/depager/atree.dr
|
82
81
|
- lib/depager/atree.rb
|
83
82
|
- lib/depager/cst.dr
|
@@ -86,14 +85,14 @@ files:
|
|
86
85
|
- lib/depager/lex.dr
|
87
86
|
- lib/depager/lex.rb
|
88
87
|
- lib/depager/lr.rb
|
88
|
+
- lib/depager/lr_put_table.rb
|
89
89
|
- lib/depager/nvaction.rb
|
90
90
|
- lib/depager/parse_action.rb
|
91
91
|
- lib/depager/parser.rb
|
92
92
|
- lib/depager/slex.dr
|
93
93
|
- lib/depager/slex.rb
|
94
94
|
- lib/depager/srp.rb
|
95
|
-
- lib/depager/template/
|
96
|
-
- lib/depager/template/astl.erbs
|
95
|
+
- lib/depager/template/ast.erbs
|
97
96
|
- lib/depager/template/extension_lalr_master.erb
|
98
97
|
- lib/depager/template/extension_lalr_slave.erb
|
99
98
|
- lib/depager/template/simple.erb
|
@@ -1,593 +0,0 @@
|
|
1
|
-
|
2
|
-
begin; require 'rubygems'; rescue Exception; end
|
3
|
-
require 'depager/parser.rb'
|
4
|
-
|
5
|
-
|
6
|
-
module D4TinyCalc
|
7
|
-
end
|
8
|
-
|
9
|
-
class TinyCalc < Depager::LALR::Basis
|
10
|
-
|
11
|
-
include Depager
|
12
|
-
|
13
|
-
### Reduce Table
|
14
|
-
reduce_table = [
|
15
|
-
[ -1, 1 ], # ( 0) $start : expr
|
16
|
-
[ 0, 3 ], # ( 1) expr : expr + term
|
17
|
-
[ 0, 3 ], # ( 2) expr : expr - term
|
18
|
-
[ 0, 1 ], # ( 3) expr : term
|
19
|
-
[ 1, 3 ], # ( 4) term : term * fact
|
20
|
-
[ 1, 3 ], # ( 5) term : term / fact
|
21
|
-
[ 1, 1 ], # ( 6) term : fact
|
22
|
-
[ 2, 1 ], # ( 7) fact : NUM
|
23
|
-
[ 2, 3 ], # ( 8) fact : ( expr )
|
24
|
-
]
|
25
|
-
### Extension Params
|
26
|
-
nparams = {
|
27
|
-
'Action' => 3,
|
28
|
-
'ASTBuilderLazy' => 2,
|
29
|
-
}
|
30
|
-
### Term to Int
|
31
|
-
t2i = {
|
32
|
-
nil => 0,
|
33
|
-
false => 1,
|
34
|
-
"+" => 2,
|
35
|
-
"-" => 3,
|
36
|
-
"*" => 4,
|
37
|
-
"/" => 5,
|
38
|
-
:NUM => 6,
|
39
|
-
"(" => 7,
|
40
|
-
")" => 8,
|
41
|
-
}
|
42
|
-
### Int to Term
|
43
|
-
i2t = [
|
44
|
-
nil,
|
45
|
-
false,
|
46
|
-
"+",
|
47
|
-
"-",
|
48
|
-
"*",
|
49
|
-
"/",
|
50
|
-
:NUM,
|
51
|
-
"(",
|
52
|
-
")",
|
53
|
-
]
|
54
|
-
### Action Table
|
55
|
-
action_table = [
|
56
|
-
[ nil, nil, nil, nil, nil, nil, 5, 1, nil, ],
|
57
|
-
[ nil, nil, nil, nil, nil, nil, 5, 1, nil, ],
|
58
|
-
[ ACC, nil, 7, 8, nil, nil, nil, nil, nil, ],
|
59
|
-
[ nil, nil, nil, nil, 9, 10, nil, nil, nil, ],
|
60
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
61
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
62
|
-
[ nil, nil, 7, 8, nil, nil, nil, nil, 11, ],
|
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, 5, 1, nil, ],
|
66
|
-
[ nil, nil, nil, nil, nil, nil, 5, 1, nil, ],
|
67
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
68
|
-
[ nil, nil, nil, nil, 9, 10, nil, nil, nil, ],
|
69
|
-
[ nil, nil, nil, nil, 9, 10, nil, nil, nil, ],
|
70
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
71
|
-
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
72
|
-
]
|
73
|
-
### Default Reduce Table
|
74
|
-
defred_table = [
|
75
|
-
nil,
|
76
|
-
nil,
|
77
|
-
nil,
|
78
|
-
-3,
|
79
|
-
-6,
|
80
|
-
-7,
|
81
|
-
nil,
|
82
|
-
nil,
|
83
|
-
nil,
|
84
|
-
nil,
|
85
|
-
nil,
|
86
|
-
-8,
|
87
|
-
-1,
|
88
|
-
-2,
|
89
|
-
-4,
|
90
|
-
-5,
|
91
|
-
]
|
92
|
-
defred_after_shift_table = [
|
93
|
-
nil,
|
94
|
-
nil,
|
95
|
-
nil,
|
96
|
-
nil,
|
97
|
-
-6,
|
98
|
-
-7,
|
99
|
-
nil,
|
100
|
-
nil,
|
101
|
-
nil,
|
102
|
-
nil,
|
103
|
-
nil,
|
104
|
-
-8,
|
105
|
-
nil,
|
106
|
-
nil,
|
107
|
-
-4,
|
108
|
-
-5,
|
109
|
-
]
|
110
|
-
### Nonterm to Int
|
111
|
-
nt2i = {
|
112
|
-
:expr => 0,
|
113
|
-
:term => 1,
|
114
|
-
:fact => 2,
|
115
|
-
}
|
116
|
-
### Int to Nonterm
|
117
|
-
i2nt = [
|
118
|
-
:expr,
|
119
|
-
:term,
|
120
|
-
:fact,
|
121
|
-
]
|
122
|
-
### Goto Table
|
123
|
-
goto_table = [
|
124
|
-
[ 2, 3, 4, ],
|
125
|
-
[ 6, 3, 4, ],
|
126
|
-
[ nil, nil, nil, ],
|
127
|
-
[ nil, nil, nil, ],
|
128
|
-
[ nil, nil, nil, ],
|
129
|
-
[ nil, nil, nil, ],
|
130
|
-
[ nil, nil, nil, ],
|
131
|
-
[ nil, 12, 4, ],
|
132
|
-
[ nil, 13, 4, ],
|
133
|
-
[ nil, nil, 14, ],
|
134
|
-
[ nil, nil, 15, ],
|
135
|
-
[ nil, nil, nil, ],
|
136
|
-
[ nil, nil, nil, ],
|
137
|
-
[ nil, nil, nil, ],
|
138
|
-
[ nil, nil, nil, ],
|
139
|
-
[ nil, nil, nil, ],
|
140
|
-
]
|
141
|
-
|
142
|
-
Tables = [ reduce_table, nparams, action_table,
|
143
|
-
defred_table, defred_after_shift_table, goto_table,
|
144
|
-
t2i, i2t, nt2i, i2nt ]
|
145
|
-
|
146
|
-
def self.createDecoratedParser
|
147
|
-
D4TinyCalc::Action.new(D4TinyCalc::ASTBuilderLazy.new(TinyCalc.new()))
|
148
|
-
end
|
149
|
-
|
150
|
-
### Inner Code
|
151
|
-
|
152
|
-
def lex
|
153
|
-
begin
|
154
|
-
until @line.empty?
|
155
|
-
case @line
|
156
|
-
|
157
|
-
when /\A\s+/, /\A\#.*/, /\A\n/
|
158
|
-
@oldline = @line; @line = $'
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
when /\A[1-9][0-9]*/
|
163
|
-
@oldline = @line; @line = $'
|
164
|
-
yield _Token(:NUM, $&.to_i)
|
165
|
-
|
166
|
-
|
167
|
-
when /\A./
|
168
|
-
@oldline = @line; @line = $'
|
169
|
-
yield _Token($&, $&)
|
170
|
-
|
171
|
-
|
172
|
-
else
|
173
|
-
raise RuntimeError, "must not happen #{@line}"
|
174
|
-
end
|
175
|
-
end
|
176
|
-
end while @line0 = @line = getline
|
177
|
-
yield nil, nil
|
178
|
-
end
|
179
|
-
|
180
|
-
end
|
181
|
-
|
182
|
-
### Outer Code
|
183
|
-
|
184
|
-
class Node
|
185
|
-
attr_accessor :lineno
|
186
|
-
attr_accessor :value
|
187
|
-
|
188
|
-
def initialize
|
189
|
-
@value = nil
|
190
|
-
|
191
|
-
end
|
192
|
-
|
193
|
-
def to_ary
|
194
|
-
[self]
|
195
|
-
end
|
196
|
-
def self.[] lineno, *args
|
197
|
-
self.new lineno, *args
|
198
|
-
end
|
199
|
-
def accept v
|
200
|
-
end
|
201
|
-
end
|
202
|
-
require 'forwardable'
|
203
|
-
class NodeList < Node
|
204
|
-
attr_accessor :lineno
|
205
|
-
extend Forwardable
|
206
|
-
_methods = Array.instance_methods -
|
207
|
-
Object.instance_methods -
|
208
|
-
['push', 'concat', 'to_ary', 'map']
|
209
|
-
def_delegators(:@lst, *_methods)
|
210
|
-
def initialize(lineno, lst=[])
|
211
|
-
@lineno = lineno
|
212
|
-
@lst = lst.to_a
|
213
|
-
end
|
214
|
-
|
215
|
-
def value
|
216
|
-
@lst.map{|i| "i.value"}
|
217
|
-
end
|
218
|
-
|
219
|
-
def push(i) @lst.push i; self; end
|
220
|
-
def concat(i) @lst.concat i; self; end
|
221
|
-
def map(&b) @lst.map(&b); end
|
222
|
-
def to_ary() @lst; end
|
223
|
-
def accept(v)
|
224
|
-
@lst.each{|i| i.accept(v) }
|
225
|
-
end
|
226
|
-
end
|
227
|
-
|
228
|
-
class Node_add < Node
|
229
|
-
attr_accessor :left, :right
|
230
|
-
attr_accessor
|
231
|
-
def initialize lineno, left, right
|
232
|
-
super()
|
233
|
-
@lineno = lineno
|
234
|
-
@lineno = lineno
|
235
|
-
@left = left
|
236
|
-
@right = right
|
237
|
-
|
238
|
-
end
|
239
|
-
|
240
|
-
def _left_ v
|
241
|
-
@left.accept(v); @left
|
242
|
-
end
|
243
|
-
|
244
|
-
def _right_ v
|
245
|
-
@right.accept(v); @right
|
246
|
-
end
|
247
|
-
|
248
|
-
def accept v
|
249
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
250
|
-
v.visit_Node_add(self)
|
251
|
-
self
|
252
|
-
end
|
253
|
-
end
|
254
|
-
|
255
|
-
class Node_sub < Node
|
256
|
-
attr_accessor :left, :right
|
257
|
-
attr_accessor
|
258
|
-
def initialize lineno, left, right
|
259
|
-
super()
|
260
|
-
@lineno = lineno
|
261
|
-
@lineno = lineno
|
262
|
-
@left = left
|
263
|
-
@right = right
|
264
|
-
|
265
|
-
end
|
266
|
-
|
267
|
-
def _left_ v
|
268
|
-
@left.accept(v); @left
|
269
|
-
end
|
270
|
-
|
271
|
-
def _right_ v
|
272
|
-
@right.accept(v); @right
|
273
|
-
end
|
274
|
-
|
275
|
-
def accept v
|
276
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
277
|
-
v.visit_Node_sub(self)
|
278
|
-
self
|
279
|
-
end
|
280
|
-
end
|
281
|
-
|
282
|
-
class Node_mul < Node
|
283
|
-
attr_accessor :left, :right
|
284
|
-
attr_accessor
|
285
|
-
def initialize lineno, left, right
|
286
|
-
super()
|
287
|
-
@lineno = lineno
|
288
|
-
@lineno = lineno
|
289
|
-
@left = left
|
290
|
-
@right = right
|
291
|
-
|
292
|
-
end
|
293
|
-
|
294
|
-
def _left_ v
|
295
|
-
@left.accept(v); @left
|
296
|
-
end
|
297
|
-
|
298
|
-
def _right_ v
|
299
|
-
@right.accept(v); @right
|
300
|
-
end
|
301
|
-
|
302
|
-
def accept v
|
303
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
304
|
-
v.visit_Node_mul(self)
|
305
|
-
self
|
306
|
-
end
|
307
|
-
end
|
308
|
-
|
309
|
-
class Node_div < Node
|
310
|
-
attr_accessor :left, :right
|
311
|
-
attr_accessor
|
312
|
-
def initialize lineno, left, right
|
313
|
-
super()
|
314
|
-
@lineno = lineno
|
315
|
-
@lineno = lineno
|
316
|
-
@left = left
|
317
|
-
@right = right
|
318
|
-
|
319
|
-
end
|
320
|
-
|
321
|
-
def _left_ v
|
322
|
-
@left.accept(v); @left
|
323
|
-
end
|
324
|
-
|
325
|
-
def _right_ v
|
326
|
-
@right.accept(v); @right
|
327
|
-
end
|
328
|
-
|
329
|
-
def accept v
|
330
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
331
|
-
v.visit_Node_div(self)
|
332
|
-
self
|
333
|
-
end
|
334
|
-
end
|
335
|
-
|
336
|
-
class Node_literal < Node
|
337
|
-
attr_accessor :n
|
338
|
-
attr_accessor :n
|
339
|
-
def initialize lineno, n
|
340
|
-
super()
|
341
|
-
@lineno = lineno
|
342
|
-
@lineno = lineno
|
343
|
-
@n = n
|
344
|
-
|
345
|
-
end
|
346
|
-
|
347
|
-
def accept v
|
348
|
-
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
349
|
-
v.visit_Node_literal(self)
|
350
|
-
self
|
351
|
-
end
|
352
|
-
end
|
353
|
-
|
354
|
-
class Visitor
|
355
|
-
def visit node
|
356
|
-
node.accept(self)
|
357
|
-
end
|
358
|
-
|
359
|
-
module_eval <<-'.,.,118754108857908.,.,', 'sample_calc/calc.astl.action.dr', 19
|
360
|
-
|
361
|
-
|
362
|
-
.,.,118754108857908.,.,
|
363
|
-
|
364
|
-
module_eval <<-'.,.,118754108863035.,.,', 'sample_calc/calc.astl.action.dr', 19
|
365
|
-
def visit_Node_add node
|
366
|
-
node.value = visit(node.left).value + visit(node.right).value
|
367
|
-
|
368
|
-
end
|
369
|
-
|
370
|
-
.,.,118754108863035.,.,
|
371
|
-
|
372
|
-
module_eval <<-'.,.,11875410891486.,.,', 'sample_calc/calc.astl.action.dr', 20
|
373
|
-
def visit_Node_sub node
|
374
|
-
node.value = visit(node.left).value - visit(node.right).value
|
375
|
-
|
376
|
-
end
|
377
|
-
|
378
|
-
.,.,11875410891486.,.,
|
379
|
-
|
380
|
-
module_eval <<-'.,.,118754108931106.,.,', 'sample_calc/calc.astl.action.dr', 21
|
381
|
-
def visit_Node_mul node
|
382
|
-
node.value = visit(node.left).value * visit(node.right).value
|
383
|
-
|
384
|
-
end
|
385
|
-
|
386
|
-
.,.,118754108931106.,.,
|
387
|
-
|
388
|
-
module_eval <<-'.,.,118754108926185.,.,', 'sample_calc/calc.astl.action.dr', 22
|
389
|
-
def visit_Node_div node
|
390
|
-
node.value = visit(node.left).value / visit(node.right).value
|
391
|
-
|
392
|
-
end
|
393
|
-
|
394
|
-
.,.,118754108926185.,.,
|
395
|
-
|
396
|
-
module_eval <<-'.,.,118754108942617.,.,', 'sample_calc/calc.astl.action.dr', 23
|
397
|
-
def visit_Node_literal node
|
398
|
-
node.value = node.n.value
|
399
|
-
|
400
|
-
end
|
401
|
-
|
402
|
-
.,.,118754108942617.,.,
|
403
|
-
|
404
|
-
end
|
405
|
-
|
406
|
-
class D4TinyCalc::ASTBuilderLazy < Depager::LALR::Action #:nodoc:all
|
407
|
-
include Depager::DecoratorUtils
|
408
|
-
|
409
|
-
on_reduce = [
|
410
|
-
nil,
|
411
|
-
:_ast_0,
|
412
|
-
:_ast_1,
|
413
|
-
:_ast_2,
|
414
|
-
:_ast_3,
|
415
|
-
:_ast_4,
|
416
|
-
:_ast_5,
|
417
|
-
:_ast_6,
|
418
|
-
:_ast_7,
|
419
|
-
|
420
|
-
]
|
421
|
-
Tables = [on_reduce]
|
422
|
-
def initialize inside
|
423
|
-
super inside, 'ASTBuilderLazy'
|
424
|
-
@on_reduce, = self.class::Tables
|
425
|
-
init_parser
|
426
|
-
end
|
427
|
-
|
428
|
-
|
429
|
-
module_eval <<-'.,.,11875410898722.,.,', 'sample_calc/calc.astl.action.dr', 29
|
430
|
-
def _ast_0 val
|
431
|
-
Node_add.new(val[0].lineno, val[0], val[2])
|
432
|
-
end
|
433
|
-
|
434
|
-
.,.,11875410898722.,.,
|
435
|
-
|
436
|
-
module_eval <<-'.,.,118754108942559.,.,', 'sample_calc/calc.astl.action.dr', 32
|
437
|
-
def _ast_1 val
|
438
|
-
Node_sub.new(val[0].lineno, val[0], val[2])
|
439
|
-
end
|
440
|
-
|
441
|
-
.,.,118754108942559.,.,
|
442
|
-
|
443
|
-
module_eval <<-'.,.,11875410893975.,.,', 'sample_calc/calc.astl.action.dr', 35
|
444
|
-
def _ast_2 val
|
445
|
-
val[0]
|
446
|
-
end
|
447
|
-
|
448
|
-
.,.,11875410893975.,.,
|
449
|
-
|
450
|
-
module_eval <<-'.,.,11875410897017.,.,', 'sample_calc/calc.astl.action.dr', 40
|
451
|
-
def _ast_3 val
|
452
|
-
Node_mul.new(val[0].lineno, val[0], val[2])
|
453
|
-
end
|
454
|
-
|
455
|
-
.,.,11875410897017.,.,
|
456
|
-
|
457
|
-
module_eval <<-'.,.,118754108932608.,.,', 'sample_calc/calc.astl.action.dr', 43
|
458
|
-
def _ast_4 val
|
459
|
-
Node_div.new(val[0].lineno, val[0], val[2])
|
460
|
-
end
|
461
|
-
|
462
|
-
.,.,118754108932608.,.,
|
463
|
-
|
464
|
-
module_eval <<-'.,.,11875410897850.,.,', 'sample_calc/calc.astl.action.dr', 46
|
465
|
-
def _ast_5 val
|
466
|
-
val[0]
|
467
|
-
end
|
468
|
-
|
469
|
-
.,.,11875410897850.,.,
|
470
|
-
|
471
|
-
module_eval <<-'.,.,11875410893154.,.,', 'sample_calc/calc.astl.action.dr', 51
|
472
|
-
def _ast_6 val
|
473
|
-
Node_literal.new(val[0].lineno, val[0])
|
474
|
-
end
|
475
|
-
|
476
|
-
.,.,11875410893154.,.,
|
477
|
-
|
478
|
-
module_eval <<-'.,.,118754108961354.,.,', 'sample_calc/calc.astl.action.dr', 54
|
479
|
-
def _ast_7 val
|
480
|
-
val[1]
|
481
|
-
end
|
482
|
-
|
483
|
-
.,.,118754108961354.,.,
|
484
|
-
|
485
|
-
end
|
486
|
-
|
487
|
-
class D4TinyCalc::Action < Depager::LALR::Action #:nodoc:all
|
488
|
-
include Depager::DecoratorUtils
|
489
|
-
include Depager
|
490
|
-
|
491
|
-
on_reduce = [
|
492
|
-
nil,
|
493
|
-
:_act_0,
|
494
|
-
:_act_1,
|
495
|
-
:_act_2,
|
496
|
-
:_act_3,
|
497
|
-
:_act_4,
|
498
|
-
:_act_5,
|
499
|
-
:_act_6,
|
500
|
-
:_act_7,
|
501
|
-
|
502
|
-
]
|
503
|
-
Tables = [on_reduce]
|
504
|
-
def initialize inside
|
505
|
-
super inside, 'Action'
|
506
|
-
@on_reduce, = self.class::Tables
|
507
|
-
init_parser
|
508
|
-
end
|
509
|
-
|
510
|
-
|
511
|
-
module_eval <<-'.,.,118754108929417.,.,', 'sample_calc/calc.astl.action.dr', 30
|
512
|
-
def _act_0 val
|
513
|
-
val[0] + val[2]
|
514
|
-
|
515
|
-
end
|
516
|
-
|
517
|
-
.,.,118754108929417.,.,
|
518
|
-
|
519
|
-
module_eval <<-'.,.,118754108959230.,.,', 'sample_calc/calc.astl.action.dr', 33
|
520
|
-
def _act_1 val
|
521
|
-
val[0] - val[2]
|
522
|
-
|
523
|
-
end
|
524
|
-
|
525
|
-
.,.,118754108959230.,.,
|
526
|
-
|
527
|
-
module_eval <<-'.,.,118754108949805.,.,', 'sample_calc/calc.astl.action.dr', 36
|
528
|
-
def _act_2 val
|
529
|
-
val[0]
|
530
|
-
|
531
|
-
end
|
532
|
-
|
533
|
-
.,.,118754108949805.,.,
|
534
|
-
|
535
|
-
module_eval <<-'.,.,118754108963438.,.,', 'sample_calc/calc.astl.action.dr', 41
|
536
|
-
def _act_3 val
|
537
|
-
val[0] * val[2]
|
538
|
-
|
539
|
-
end
|
540
|
-
|
541
|
-
.,.,118754108963438.,.,
|
542
|
-
|
543
|
-
module_eval <<-'.,.,118754108920628.,.,', 'sample_calc/calc.astl.action.dr', 44
|
544
|
-
def _act_4 val
|
545
|
-
val[0] / val[2]
|
546
|
-
|
547
|
-
end
|
548
|
-
|
549
|
-
.,.,118754108920628.,.,
|
550
|
-
|
551
|
-
module_eval <<-'.,.,11875410896708.,.,', 'sample_calc/calc.astl.action.dr', 47
|
552
|
-
def _act_5 val
|
553
|
-
val[0]
|
554
|
-
|
555
|
-
end
|
556
|
-
|
557
|
-
.,.,11875410896708.,.,
|
558
|
-
|
559
|
-
module_eval <<-'.,.,118754108933319.,.,', 'sample_calc/calc.astl.action.dr', 52
|
560
|
-
def _act_6 val
|
561
|
-
val[0].value
|
562
|
-
|
563
|
-
end
|
564
|
-
|
565
|
-
.,.,118754108933319.,.,
|
566
|
-
|
567
|
-
module_eval <<-'.,.,118754108926354.,.,', 'sample_calc/calc.astl.action.dr', 55
|
568
|
-
def _act_7 val
|
569
|
-
val[1].value
|
570
|
-
|
571
|
-
end
|
572
|
-
|
573
|
-
.,.,118754108926354.,.,
|
574
|
-
|
575
|
-
end
|
576
|
-
|
577
|
-
|
578
|
-
def createDecoratedTinyCalc
|
579
|
-
D4TinyCalc::Action.new(D4TinyCalc::ASTBuilderLazy.new(TinyCalc.new()))
|
580
|
-
end
|
581
|
-
|
582
|
-
### main
|
583
|
-
if __FILE__ == $0
|
584
|
-
### Main Code
|
585
|
-
require 'pp'
|
586
|
-
parser = createDecoratedTinyCalc()
|
587
|
-
t, r = parser.yyparse(STDIN)
|
588
|
-
v = Visitor.new
|
589
|
-
pp r
|
590
|
-
pp t.accept(v).value
|
591
|
-
pp t
|
592
|
-
|
593
|
-
end
|