racc 1.4.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. data/.gitattributes +2 -0
  2. data/.gitignore +7 -0
  3. data/COPYING +515 -0
  4. data/ChangeLog +846 -0
  5. data/DEPENDS +4 -0
  6. data/README.en.rdoc +86 -0
  7. data/README.ja.rdoc +96 -0
  8. data/Rakefile +15 -0
  9. data/TODO +5 -0
  10. data/bin/racc +308 -0
  11. data/bin/racc2y +195 -0
  12. data/bin/y2racc +339 -0
  13. data/doc/en/NEWS.en.rdoc +282 -0
  14. data/doc/en/command.en.html +78 -0
  15. data/doc/en/debug.en.rdoc +20 -0
  16. data/doc/en/grammar.en.rdoc +230 -0
  17. data/doc/en/index.en.html +10 -0
  18. data/doc/en/parser.en.rdoc +74 -0
  19. data/doc/en/usage.en.html +92 -0
  20. data/doc/ja/NEWS.ja.rdoc +307 -0
  21. data/doc/ja/command.ja.html +94 -0
  22. data/doc/ja/debug.ja.rdoc +36 -0
  23. data/doc/ja/grammar.ja.rdoc +348 -0
  24. data/doc/ja/index.ja.html +10 -0
  25. data/doc/ja/parser.ja.rdoc +125 -0
  26. data/doc/ja/usage.ja.html +414 -0
  27. data/ext/racc/cparse/MANIFEST +4 -0
  28. data/ext/racc/cparse/cparse.c +824 -0
  29. data/ext/racc/cparse/depend +1 -0
  30. data/ext/racc/cparse/extconf.rb +7 -0
  31. data/fastcache/extconf.rb +2 -0
  32. data/fastcache/fastcache.c +185 -0
  33. data/lib/racc.rb +6 -0
  34. data/lib/racc/compat.rb +40 -0
  35. data/lib/racc/debugflags.rb +59 -0
  36. data/lib/racc/exception.rb +15 -0
  37. data/lib/racc/grammar.rb +1115 -0
  38. data/lib/racc/grammarfileparser.rb +559 -0
  39. data/lib/racc/info.rb +16 -0
  40. data/lib/racc/iset.rb +91 -0
  41. data/lib/racc/logfilegenerator.rb +214 -0
  42. data/lib/racc/parser.rb +439 -0
  43. data/lib/racc/parserfilegenerator.rb +511 -0
  44. data/lib/racc/pre-setup +13 -0
  45. data/lib/racc/sourcetext.rb +34 -0
  46. data/lib/racc/state.rb +971 -0
  47. data/lib/racc/statetransitiontable.rb +316 -0
  48. data/lib/racc/static.rb +5 -0
  49. data/misc/dist.sh +31 -0
  50. data/sample/array.y +67 -0
  51. data/sample/array2.y +59 -0
  52. data/sample/calc-ja.y +66 -0
  53. data/sample/calc.y +65 -0
  54. data/sample/conflict.y +15 -0
  55. data/sample/hash.y +60 -0
  56. data/sample/lalr.y +17 -0
  57. data/sample/lists.y +57 -0
  58. data/sample/syntax.y +46 -0
  59. data/sample/yyerr.y +46 -0
  60. data/setup.rb +1587 -0
  61. data/tasks/doc.rb +12 -0
  62. data/tasks/email.rb +55 -0
  63. data/tasks/file.rb +37 -0
  64. data/tasks/gem.rb +37 -0
  65. data/tasks/test.rb +16 -0
  66. data/test/assets/chk.y +126 -0
  67. data/test/assets/conf.y +16 -0
  68. data/test/assets/digraph.y +29 -0
  69. data/test/assets/echk.y +118 -0
  70. data/test/assets/err.y +60 -0
  71. data/test/assets/expect.y +7 -0
  72. data/test/assets/firstline.y +4 -0
  73. data/test/assets/ichk.y +102 -0
  74. data/test/assets/intp.y +546 -0
  75. data/test/assets/mailp.y +437 -0
  76. data/test/assets/newsyn.y +25 -0
  77. data/test/assets/noend.y +4 -0
  78. data/test/assets/nonass.y +41 -0
  79. data/test/assets/normal.y +27 -0
  80. data/test/assets/norule.y +4 -0
  81. data/test/assets/nullbug1.y +25 -0
  82. data/test/assets/nullbug2.y +15 -0
  83. data/test/assets/opt.y +123 -0
  84. data/test/assets/percent.y +35 -0
  85. data/test/assets/recv.y +97 -0
  86. data/test/assets/rrconf.y +14 -0
  87. data/test/assets/scan.y +72 -0
  88. data/test/assets/syntax.y +50 -0
  89. data/test/assets/unterm.y +5 -0
  90. data/test/assets/useless.y +12 -0
  91. data/test/assets/yyerr.y +46 -0
  92. data/test/bench.y +36 -0
  93. data/test/helper.rb +88 -0
  94. data/test/infini.y +8 -0
  95. data/test/scandata/brace +7 -0
  96. data/test/scandata/gvar +1 -0
  97. data/test/scandata/normal +4 -0
  98. data/test/scandata/percent +18 -0
  99. data/test/scandata/slash +10 -0
  100. data/test/src.intp +34 -0
  101. data/test/start.y +20 -0
  102. data/test/test_chk_y.rb +51 -0
  103. data/test/test_grammar_file_parser.rb +15 -0
  104. data/test/test_racc_command.rb +155 -0
  105. data/test/test_scan_y.rb +51 -0
  106. data/test/testscanner.rb +51 -0
  107. data/web/racc.en.rhtml +42 -0
  108. data/web/racc.ja.rhtml +51 -0
  109. metadata +166 -0
@@ -0,0 +1,16 @@
1
+ #
2
+ # $Id$
3
+ #
4
+ # Copyright (c) 1999-2006 Minero Aoki
5
+ #
6
+ # This program is free software.
7
+ # You can distribute/modify this program under the terms of
8
+ # the GNU LGPL, Lesser General Public License version 2.1.
9
+ # For details of the GNU LGPL, see the file "COPYING".
10
+ #
11
+
12
+ module Racc
13
+ Version = '1.4.6'
14
+ VERSION = Version
15
+ Copyright = 'Copyright (c) 1999-2006 Minero Aoki'
16
+ end
@@ -0,0 +1,91 @@
1
+ #
2
+ # $Id$
3
+ #
4
+ # Copyright (c) 1999-2006 Minero Aoki
5
+ #
6
+ # This program is free software.
7
+ # You can distribute/modify this program under the terms of
8
+ # the GNU LGPL, Lesser General Public License version 2.1.
9
+ # For details of the GNU LGPL, see the file "COPYING".
10
+ #
11
+
12
+ module Racc
13
+
14
+ # An "indexed" set. All items must respond to :ident.
15
+ class ISet
16
+
17
+ def initialize(a = [])
18
+ @set = a
19
+ end
20
+
21
+ attr_reader :set
22
+
23
+ def add(i)
24
+ @set[i.ident] = i
25
+ end
26
+
27
+ def [](key)
28
+ @set[key.ident]
29
+ end
30
+
31
+ def []=(key, val)
32
+ @set[key.ident] = val
33
+ end
34
+
35
+ alias include? []
36
+ alias key? []
37
+
38
+ def update(other)
39
+ s = @set
40
+ o = other.set
41
+ o.each_index do |idx|
42
+ if t = o[idx]
43
+ s[idx] = t
44
+ end
45
+ end
46
+ end
47
+
48
+ def update_a(a)
49
+ s = @set
50
+ a.each {|i| s[i.ident] = i }
51
+ end
52
+
53
+ def delete(key)
54
+ i = @set[key.ident]
55
+ @set[key.ident] = nil
56
+ i
57
+ end
58
+
59
+ def each(&block)
60
+ @set.compact.each(&block)
61
+ end
62
+
63
+ def to_a
64
+ @set.compact
65
+ end
66
+
67
+ def to_s
68
+ "[#{@set.compact.join(' ')}]"
69
+ end
70
+
71
+ alias inspect to_s
72
+
73
+ def size
74
+ @set.nitems
75
+ end
76
+
77
+ def empty?
78
+ @set.nitems == 0
79
+ end
80
+
81
+ def clear
82
+ @set.clear
83
+ end
84
+
85
+ def dup
86
+ ISet.new(@set.dup)
87
+ end
88
+
89
+ end # class ISet
90
+
91
+ end # module Racc
@@ -0,0 +1,214 @@
1
+ #
2
+ # $Id$
3
+ #
4
+ # Copyright (c) 1999-2006 Minero Aoki
5
+ #
6
+ # This program is free software.
7
+ # You can distribute/modify this program under the terms of
8
+ # the GNU LGPL, Lesser General Public License version 2.1.
9
+ # For details of the GNU LGPL, see the file "COPYING".
10
+ #
11
+
12
+ module Racc
13
+
14
+ class LogFileGenerator
15
+
16
+ def initialize(states, debug_flags = DebugFlags.new)
17
+ @states = states
18
+ @grammar = states.grammar
19
+ @debug_flags = debug_flags
20
+ end
21
+
22
+ def output(out)
23
+ output_conflict out; out.puts
24
+ output_useless out; out.puts
25
+ output_rule out; out.puts
26
+ output_token out; out.puts
27
+ output_state out
28
+ end
29
+
30
+ #
31
+ # Warnings
32
+ #
33
+
34
+ def output_conflict(out)
35
+ @states.each do |state|
36
+ if state.srconf
37
+ out.printf "state %d contains %d shift/reduce conflicts\n",
38
+ state.stateid, state.srconf.size
39
+ end
40
+ if state.rrconf
41
+ out.printf "state %d contains %d reduce/reduce conflicts\n",
42
+ state.stateid, state.rrconf.size
43
+ end
44
+ end
45
+ end
46
+
47
+ def output_useless(out)
48
+ used = []
49
+ @grammar.each do |rl|
50
+ if rl.useless?
51
+ out.printf "rule %d (%s) never reduced\n",
52
+ rl.ident, rl.target.to_s
53
+ end
54
+ end
55
+ @grammar.each_nonterminal do |t|
56
+ if t.useless?
57
+ out.printf "useless nonterminal %s\n", t.to_s
58
+ end
59
+ end
60
+ end
61
+
62
+ #
63
+ # States
64
+ #
65
+
66
+ def output_state(out)
67
+ out << "--------- State ---------\n"
68
+
69
+ showall = @debug_flags.la || @debug_flags.state
70
+ @states.each do |state|
71
+ out << "\nstate #{state.ident}\n\n"
72
+
73
+ (showall ? state.closure : state.core).each do |ptr|
74
+ pointer_out(out, ptr) if ptr.rule.ident != 0 or showall
75
+ end
76
+ out << "\n"
77
+
78
+ action_out out, state
79
+ end
80
+ end
81
+
82
+ def pointer_out(out, ptr)
83
+ buf = sprintf("%4d) %s :", ptr.rule.ident, ptr.rule.target.to_s)
84
+ ptr.rule.symbols.each_with_index do |tok, idx|
85
+ buf << ' _' if idx == ptr.index
86
+ buf << ' ' << tok.to_s
87
+ end
88
+ buf << ' _' if ptr.reduce?
89
+ out.puts buf
90
+ end
91
+
92
+ def action_out(f, state)
93
+ r = ''
94
+ e = ''
95
+ sr = state.srconf && state.srconf.dup
96
+ rr = state.rrconf && state.rrconf.dup
97
+ acts = state.action
98
+ keys = acts.keys
99
+ keys.sort! {|a,b| a.ident <=> b.ident }
100
+
101
+ [ Shift, Reduce, Error, Accept ].each do |klass|
102
+ keys.delete_if do |tok|
103
+ act = acts[tok]
104
+ if act.kind_of?(klass)
105
+ outact f, tok, act
106
+ if sr and c = sr.delete(tok)
107
+ outsrconf f, c
108
+ end
109
+ if rr and c = rr.delete(tok)
110
+ outrrconf f, c
111
+ end
112
+
113
+ true
114
+ else
115
+ false
116
+ end
117
+ end
118
+ end
119
+ sr.each {|tok, c| outsrconf f, c } if sr
120
+ rr.each {|tok, c| outrrconf f, c } if rr
121
+
122
+ act = state.defact
123
+ if not act.kind_of?(Error) or @debug_flags.any?
124
+ outact f, '$default', act
125
+ end
126
+
127
+ f.puts
128
+ state.goto_table.each do |t, st|
129
+ if t.nonterminal?
130
+ f.printf " %-12s go to state %d\n", t.to_s, st.ident
131
+ end
132
+ end
133
+ end
134
+
135
+ def outact(f, t, act)
136
+ case act
137
+ when Shift
138
+ f.printf " %-12s shift, and go to state %d\n",
139
+ t.to_s, act.goto_id
140
+ when Reduce
141
+ f.printf " %-12s reduce using rule %d (%s)\n",
142
+ t.to_s, act.ruleid, act.rule.target.to_s
143
+ when Accept
144
+ f.printf " %-12s accept\n", t.to_s
145
+ when Error
146
+ f.printf " %-12s error\n", t.to_s
147
+ else
148
+ raise "racc: fatal: wrong act for outact: act=#{act}(#{act.class})"
149
+ end
150
+ end
151
+
152
+ def outsrconf(f, confs)
153
+ confs.each do |c|
154
+ r = c.reduce
155
+ f.printf " %-12s [reduce using rule %d (%s)]\n",
156
+ c.shift.to_s, r.ident, r.target.to_s
157
+ end
158
+ end
159
+
160
+ def outrrconf(f, confs)
161
+ confs.each do |c|
162
+ r = c.low_prec
163
+ f.printf " %-12s [reduce using rule %d (%s)]\n",
164
+ c.token.to_s, r.ident, r.target.to_s
165
+ end
166
+ end
167
+
168
+ #
169
+ # Rules
170
+ #
171
+
172
+ def output_rule(out)
173
+ out.print "-------- Grammar --------\n\n"
174
+ @grammar.each do |rl|
175
+ if @debug_flags.any? or rl.ident != 0
176
+ out.printf "rule %d %s: %s\n",
177
+ rl.ident, rl.target.to_s, rl.symbols.join(' ')
178
+ end
179
+ end
180
+ end
181
+
182
+ #
183
+ # Tokens
184
+ #
185
+
186
+ def output_token(out)
187
+ out.print "------- Symbols -------\n\n"
188
+
189
+ out.print "**Nonterminals, with rules where they appear\n\n"
190
+ @grammar.each_nonterminal do |t|
191
+ tmp = <<SRC
192
+ %s (%d)
193
+ on right: %s
194
+ on left : %s
195
+ SRC
196
+ out.printf tmp, t.to_s, t.ident,
197
+ symbol_locations(t.locate).join(' '),
198
+ symbol_locations(t.heads).join(' ')
199
+ end
200
+
201
+ out.print "\n**Terminals, with rules where they appear\n\n"
202
+ @grammar.each_terminal do |t|
203
+ out.printf " %s (%d) %s\n",
204
+ t.to_s, t.ident, symbol_locations(t.locate).join(' ')
205
+ end
206
+ end
207
+
208
+ def symbol_locations(locs)
209
+ locs.map {|loc| loc.rule.ident }.reject {|n| n == 0 }.uniq
210
+ end
211
+
212
+ end
213
+
214
+ end # module Racc
@@ -0,0 +1,439 @@
1
+ #
2
+ # $Id$
3
+ #
4
+ # Copyright (c) 1999-2006 Minero Aoki
5
+ #
6
+ # This program is free software.
7
+ # You can distribute/modify this program under the same terms of ruby.
8
+ #
9
+ # As a special exception, when this code is copied by Racc
10
+ # into a Racc output file, you may use that output file
11
+ # without restriction.
12
+ #
13
+
14
+ unless defined?(NotImplementedError)
15
+ NotImplementedError = NotImplementError
16
+ end
17
+
18
+ module Racc
19
+ class ParseError < StandardError; end
20
+ end
21
+ unless defined?(::ParseError)
22
+ ParseError = Racc::ParseError
23
+ end
24
+
25
+ module Racc
26
+
27
+ unless defined?(Racc_No_Extentions)
28
+ Racc_No_Extentions = false
29
+ end
30
+
31
+ class Parser
32
+
33
+ Racc_Runtime_Version = '1.4.6'
34
+ Racc_Runtime_Revision = '$Id$'
35
+
36
+ Racc_Runtime_Core_Version_R = '1.4.6'
37
+ Racc_Runtime_Core_Revision_R = '$Id$'.split[1]
38
+ begin
39
+ require 'racc/cparse'
40
+ # Racc_Runtime_Core_Version_C = (defined in extention)
41
+ Racc_Runtime_Core_Revision_C = Racc_Runtime_Core_Id_C.split[2]
42
+ unless new.respond_to?(:_racc_do_parse_c, true)
43
+ raise LoadError, 'old cparse.so'
44
+ end
45
+ if Racc_No_Extentions
46
+ raise LoadError, 'selecting ruby version of racc runtime core'
47
+ end
48
+
49
+ Racc_Main_Parsing_Routine = :_racc_do_parse_c
50
+ Racc_YY_Parse_Method = :_racc_yyparse_c
51
+ Racc_Runtime_Core_Version = Racc_Runtime_Core_Version_C
52
+ Racc_Runtime_Core_Revision = Racc_Runtime_Core_Revision_C
53
+ Racc_Runtime_Type = 'c'
54
+ rescue LoadError
55
+ Racc_Main_Parsing_Routine = :_racc_do_parse_rb
56
+ Racc_YY_Parse_Method = :_racc_yyparse_rb
57
+ Racc_Runtime_Core_Version = Racc_Runtime_Core_Version_R
58
+ Racc_Runtime_Core_Revision = Racc_Runtime_Core_Revision_R
59
+ Racc_Runtime_Type = 'ruby'
60
+ end
61
+
62
+ def Parser.racc_runtime_type
63
+ Racc_Runtime_Type
64
+ end
65
+
66
+ private
67
+
68
+ def _racc_setup
69
+ @yydebug = false unless self.class::Racc_debug_parser
70
+ @yydebug = false unless defined?(@yydebug)
71
+ if @yydebug
72
+ @racc_debug_out = $stderr unless defined?(@racc_debug_out)
73
+ @racc_debug_out ||= $stderr
74
+ end
75
+ arg = self.class::Racc_arg
76
+ arg[13] = true if arg.size < 14
77
+ arg
78
+ end
79
+
80
+ def _racc_init_sysvars
81
+ @racc_state = [0]
82
+ @racc_tstack = []
83
+ @racc_vstack = []
84
+
85
+ @racc_t = nil
86
+ @racc_val = nil
87
+
88
+ @racc_read_next = true
89
+
90
+ @racc_user_yyerror = false
91
+ @racc_error_status = 0
92
+ end
93
+
94
+ ###
95
+ ### do_parse
96
+ ###
97
+
98
+ def do_parse
99
+ __send__(Racc_Main_Parsing_Routine, _racc_setup(), false)
100
+ end
101
+
102
+ def next_token
103
+ raise NotImplementedError, "#{self.class}\#next_token is not defined"
104
+ end
105
+
106
+ def _racc_do_parse_rb(arg, in_debug)
107
+ action_table, action_check, action_default, action_pointer,
108
+ goto_table, goto_check, goto_default, goto_pointer,
109
+ nt_base, reduce_table, token_table, shift_n,
110
+ reduce_n, use_result, * = arg
111
+
112
+ _racc_init_sysvars
113
+ tok = act = i = nil
114
+ nerr = 0
115
+
116
+ catch(:racc_end_parse) {
117
+ while true
118
+ if i = action_pointer[@racc_state[-1]]
119
+ if @racc_read_next
120
+ if @racc_t != 0 # not EOF
121
+ tok, @racc_val = next_token()
122
+ unless tok # EOF
123
+ @racc_t = 0
124
+ else
125
+ @racc_t = (token_table[tok] or 1) # error token
126
+ end
127
+ racc_read_token(@racc_t, tok, @racc_val) if @yydebug
128
+ @racc_read_next = false
129
+ end
130
+ end
131
+ i += @racc_t
132
+ unless i >= 0 and
133
+ act = action_table[i] and
134
+ action_check[i] == @racc_state[-1]
135
+ act = action_default[@racc_state[-1]]
136
+ end
137
+ else
138
+ act = action_default[@racc_state[-1]]
139
+ end
140
+ while act = _racc_evalact(act, arg)
141
+ ;
142
+ end
143
+ end
144
+ }
145
+ end
146
+
147
+ ###
148
+ ### yyparse
149
+ ###
150
+
151
+ def yyparse(recv, mid)
152
+ __send__(Racc_YY_Parse_Method, recv, mid, _racc_setup(), true)
153
+ end
154
+
155
+ def _racc_yyparse_rb(recv, mid, arg, c_debug)
156
+ action_table, action_check, action_default, action_pointer,
157
+ goto_table, goto_check, goto_default, goto_pointer,
158
+ nt_base, reduce_table, token_table, shift_n,
159
+ reduce_n, use_result, * = arg
160
+
161
+ _racc_init_sysvars
162
+ nerr = 0
163
+
164
+ catch(:racc_end_parse) {
165
+ until i = action_pointer[@racc_state[-1]]
166
+ while act = _racc_evalact(action_default[@racc_state[-1]], arg)
167
+ ;
168
+ end
169
+ end
170
+ recv.__send__(mid) do |tok, val|
171
+ unless tok
172
+ @racc_t = 0
173
+ else
174
+ @racc_t = (token_table[tok] or 1) # error token
175
+ end
176
+ @racc_val = val
177
+ @racc_read_next = false
178
+
179
+ i += @racc_t
180
+ unless i >= 0 and
181
+ act = action_table[i] and
182
+ action_check[i] == @racc_state[-1]
183
+ act = action_default[@racc_state[-1]]
184
+ end
185
+ while act = _racc_evalact(act, arg)
186
+ ;
187
+ end
188
+
189
+ while !(i = action_pointer[@racc_state[-1]]) ||
190
+ ! @racc_read_next ||
191
+ @racc_t == 0 # $
192
+ unless i and i += @racc_t and
193
+ i >= 0 and
194
+ act = action_table[i] and
195
+ action_check[i] == @racc_state[-1]
196
+ act = action_default[@racc_state[-1]]
197
+ end
198
+ while act = _racc_evalact(act, arg)
199
+ ;
200
+ end
201
+ end
202
+ end
203
+ }
204
+ end
205
+
206
+ ###
207
+ ### common
208
+ ###
209
+
210
+ def _racc_evalact(act, arg)
211
+ action_table, action_check, action_default, action_pointer,
212
+ goto_table, goto_check, goto_default, goto_pointer,
213
+ nt_base, reduce_table, token_table, shift_n,
214
+ reduce_n, use_result, * = arg
215
+ nerr = 0 # tmp
216
+
217
+ if act > 0 and act < shift_n
218
+ #
219
+ # shift
220
+ #
221
+ if @racc_error_status > 0
222
+ @racc_error_status -= 1 unless @racc_t == 1 # error token
223
+ end
224
+ @racc_vstack.push @racc_val
225
+ @racc_state.push act
226
+ @racc_read_next = true
227
+ if @yydebug
228
+ @racc_tstack.push @racc_t
229
+ racc_shift @racc_t, @racc_tstack, @racc_vstack
230
+ end
231
+
232
+ elsif act < 0 and act > -reduce_n
233
+ #
234
+ # reduce
235
+ #
236
+ code = catch(:racc_jump) {
237
+ @racc_state.push _racc_do_reduce(arg, act)
238
+ false
239
+ }
240
+ if code
241
+ case code
242
+ when 1 # yyerror
243
+ @racc_user_yyerror = true # user_yyerror
244
+ return -reduce_n
245
+ when 2 # yyaccept
246
+ return shift_n
247
+ else
248
+ raise '[Racc Bug] unknown jump code'
249
+ end
250
+ end
251
+
252
+ elsif act == shift_n
253
+ #
254
+ # accept
255
+ #
256
+ racc_accept if @yydebug
257
+ throw :racc_end_parse, @racc_vstack[0]
258
+
259
+ elsif act == -reduce_n
260
+ #
261
+ # error
262
+ #
263
+ case @racc_error_status
264
+ when 0
265
+ unless arg[21] # user_yyerror
266
+ nerr += 1
267
+ on_error @racc_t, @racc_val, @racc_vstack
268
+ end
269
+ when 3
270
+ if @racc_t == 0 # is $
271
+ throw :racc_end_parse, nil
272
+ end
273
+ @racc_read_next = true
274
+ end
275
+ @racc_user_yyerror = false
276
+ @racc_error_status = 3
277
+ while true
278
+ if i = action_pointer[@racc_state[-1]]
279
+ i += 1 # error token
280
+ if i >= 0 and
281
+ (act = action_table[i]) and
282
+ action_check[i] == @racc_state[-1]
283
+ break
284
+ end
285
+ end
286
+ throw :racc_end_parse, nil if @racc_state.size <= 1
287
+ @racc_state.pop
288
+ @racc_vstack.pop
289
+ if @yydebug
290
+ @racc_tstack.pop
291
+ racc_e_pop @racc_state, @racc_tstack, @racc_vstack
292
+ end
293
+ end
294
+ return act
295
+
296
+ else
297
+ raise "[Racc Bug] unknown action #{act.inspect}"
298
+ end
299
+
300
+ racc_next_state(@racc_state[-1], @racc_state) if @yydebug
301
+
302
+ nil
303
+ end
304
+
305
+ def _racc_do_reduce(arg, act)
306
+ action_table, action_check, action_default, action_pointer,
307
+ goto_table, goto_check, goto_default, goto_pointer,
308
+ nt_base, reduce_table, token_table, shift_n,
309
+ reduce_n, use_result, * = arg
310
+ state = @racc_state
311
+ vstack = @racc_vstack
312
+ tstack = @racc_tstack
313
+
314
+ i = act * -3
315
+ len = reduce_table[i]
316
+ reduce_to = reduce_table[i+1]
317
+ method_id = reduce_table[i+2]
318
+ void_array = []
319
+
320
+ tmp_t = tstack[-len, len] if @yydebug
321
+ tmp_v = vstack[-len, len]
322
+ tstack[-len, len] = void_array if @yydebug
323
+ vstack[-len, len] = void_array
324
+ state[-len, len] = void_array
325
+
326
+ # tstack must be updated AFTER method call
327
+ if use_result
328
+ vstack.push __send__(method_id, tmp_v, vstack, tmp_v[0])
329
+ else
330
+ vstack.push __send__(method_id, tmp_v, vstack)
331
+ end
332
+ tstack.push reduce_to
333
+
334
+ racc_reduce(tmp_t, reduce_to, tstack, vstack) if @yydebug
335
+
336
+ k1 = reduce_to - nt_base
337
+ if i = goto_pointer[k1]
338
+ i += state[-1]
339
+ if i >= 0 and (curstate = goto_table[i]) and goto_check[i] == k1
340
+ return curstate
341
+ end
342
+ end
343
+ goto_default[k1]
344
+ end
345
+
346
+ def on_error(t, val, vstack)
347
+ raise ParseError, sprintf("\nparse error on value %s (%s)",
348
+ val.inspect, token_to_str(t) || '?')
349
+ end
350
+
351
+ def yyerror
352
+ throw :racc_jump, 1
353
+ end
354
+
355
+ def yyaccept
356
+ throw :racc_jump, 2
357
+ end
358
+
359
+ def yyerrok
360
+ @racc_error_status = 0
361
+ end
362
+
363
+ #
364
+ # for debugging output
365
+ #
366
+
367
+ def racc_read_token(t, tok, val)
368
+ @racc_debug_out.print 'read '
369
+ @racc_debug_out.print tok.inspect, '(', racc_token2str(t), ') '
370
+ @racc_debug_out.puts val.inspect
371
+ @racc_debug_out.puts
372
+ end
373
+
374
+ def racc_shift(tok, tstack, vstack)
375
+ @racc_debug_out.puts "shift #{racc_token2str tok}"
376
+ racc_print_stacks tstack, vstack
377
+ @racc_debug_out.puts
378
+ end
379
+
380
+ def racc_reduce(toks, sim, tstack, vstack)
381
+ out = @racc_debug_out
382
+ out.print 'reduce '
383
+ if toks.empty?
384
+ out.print ' <none>'
385
+ else
386
+ toks.each {|t| out.print ' ', racc_token2str(t) }
387
+ end
388
+ out.puts " --> #{racc_token2str(sim)}"
389
+
390
+ racc_print_stacks tstack, vstack
391
+ @racc_debug_out.puts
392
+ end
393
+
394
+ def racc_accept
395
+ @racc_debug_out.puts 'accept'
396
+ @racc_debug_out.puts
397
+ end
398
+
399
+ def racc_e_pop(state, tstack, vstack)
400
+ @racc_debug_out.puts 'error recovering mode: pop token'
401
+ racc_print_states state
402
+ racc_print_stacks tstack, vstack
403
+ @racc_debug_out.puts
404
+ end
405
+
406
+ def racc_next_state(curstate, state)
407
+ @racc_debug_out.puts "goto #{curstate}"
408
+ racc_print_states state
409
+ @racc_debug_out.puts
410
+ end
411
+
412
+ def racc_print_stacks(t, v)
413
+ out = @racc_debug_out
414
+ out.print ' ['
415
+ t.each_index do |i|
416
+ out.print ' (', racc_token2str(t[i]), ' ', v[i].inspect, ')'
417
+ end
418
+ out.puts ' ]'
419
+ end
420
+
421
+ def racc_print_states(s)
422
+ out = @racc_debug_out
423
+ out.print ' ['
424
+ s.each {|st| out.print ' ', st }
425
+ out.puts ' ]'
426
+ end
427
+
428
+ def racc_token2str(tok)
429
+ self.class::Racc_token_to_s_table[tok] or
430
+ raise "[Racc Bug] can't convert token #{tok} to string"
431
+ end
432
+
433
+ def token_to_str(t)
434
+ self.class::Racc_token_to_s_table[t]
435
+ end
436
+
437
+ end
438
+
439
+ end