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