gd-racc 1.4.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gemtest +0 -0
- data/COPYING +515 -0
- data/ChangeLog +846 -0
- data/DEPENDS +4 -0
- data/Manifest.txt +100 -0
- data/README.ja.rdoc +96 -0
- data/README.rdoc +86 -0
- data/Rakefile +54 -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 +828 -0
- data/ext/racc/depend +1 -0
- data/ext/racc/extconf.rb +7 -0
- data/fastcache/extconf.rb +2 -0
- data/fastcache/fastcache.c +185 -0
- data/lib/racc.rb +6 -0
- data/lib/racc/compat.rb +32 -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 +631 -0
- data/lib/racc/parser.rb +626 -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/grammar.en.rdoc +230 -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 +87 -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 +212 -0
data/lib/racc/info.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#
|
2
|
+
# $Id: 90afb43b1438ad2bb681a529146d1cb5cd7e45ac $
|
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.11'
|
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,631 @@
|
|
1
|
+
module Racc
|
2
|
+
PARSER_TEXT = <<'__end_of_file__'
|
3
|
+
#
|
4
|
+
# $Id: 3c520ba1f2996b86abc21eeb54768934a7be9d0c $
|
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
|
+
#
|
32
|
+
# == Command-line Reference
|
33
|
+
#
|
34
|
+
# racc [-o<var>filename</var>] [--output-file=<var>filename</var>]
|
35
|
+
# [-e<var>rubypath</var>] [--embedded=<var>rubypath</var>]
|
36
|
+
# [-v] [--verbose]
|
37
|
+
# [-O<var>filename</var>] [--log-file=<var>filename</var>]
|
38
|
+
# [-g] [--debug]
|
39
|
+
# [-E] [--embedded]
|
40
|
+
# [-l] [--no-line-convert]
|
41
|
+
# [-c] [--line-convert-all]
|
42
|
+
# [-a] [--no-omit-actions]
|
43
|
+
# [-C] [--check-only]
|
44
|
+
# [-S] [--output-status]
|
45
|
+
# [--version] [--copyright] [--help] <var>grammarfile</var>
|
46
|
+
#
|
47
|
+
# [+filename+]
|
48
|
+
# Racc grammar file. Any extention is permitted.
|
49
|
+
# [-o+outfile+, --output-file=+outfile+]
|
50
|
+
# A filename for output. default is <+filename+>.tab.rb
|
51
|
+
# [-O+filename+, --log-file=+filename+]
|
52
|
+
# Place logging output in file +filename+.
|
53
|
+
# Default log file name is <+filename+>.output.
|
54
|
+
# [-e+rubypath+, --executable=+rubypath+]
|
55
|
+
# output executable file(mode 755). where +path+ is the Ruby interpreter.
|
56
|
+
# [-v, --verbose]
|
57
|
+
# verbose mode. create +filename+.output file, like yacc's y.output file.
|
58
|
+
# [-g, --debug]
|
59
|
+
# add debug code to parser class. To display debuggin information,
|
60
|
+
# use this '-g' option and set @yydebug true in parser class.
|
61
|
+
# [-E, --embedded]
|
62
|
+
# Output parser which doesn't need runtime files (racc/parser.rb).
|
63
|
+
# [-C, --check-only]
|
64
|
+
# Check syntax of racc grammer file and quit.
|
65
|
+
# [-S, --output-status]
|
66
|
+
# Print messages time to time while compiling.
|
67
|
+
# [-l, --no-line-convert]
|
68
|
+
# turns off line number converting.
|
69
|
+
# [-c, --line-convert-all]
|
70
|
+
# Convert line number of actions, inner, header and footer.
|
71
|
+
# [-a, --no-omit-actions]
|
72
|
+
# Call all actions, even if an action is empty.
|
73
|
+
# [--version]
|
74
|
+
# print Racc version and quit.
|
75
|
+
# [--copyright]
|
76
|
+
# Print copyright and quit.
|
77
|
+
# [--help]
|
78
|
+
# Print usage and quit.
|
79
|
+
#
|
80
|
+
# == Generating Parser Using Racc
|
81
|
+
#
|
82
|
+
# To compile Racc grammar file, simply type:
|
83
|
+
#
|
84
|
+
# $ racc parse.y
|
85
|
+
#
|
86
|
+
# This creates Ruby script file "parse.tab.y". The -o option can change the output filename.
|
87
|
+
#
|
88
|
+
# == Writing A Racc Grammar File
|
89
|
+
#
|
90
|
+
# If you want your own parser, you have to write a grammar file.
|
91
|
+
# A grammar file contains the name of your parser class, grammar for the parser,
|
92
|
+
# user code, and anything else.
|
93
|
+
# When writing a grammar file, yacc's knowledge is helpful.
|
94
|
+
# If you have not used yacc before, Racc is not too difficult.
|
95
|
+
#
|
96
|
+
# Here's an example Racc grammar file.
|
97
|
+
#
|
98
|
+
# class Calcparser
|
99
|
+
# rule
|
100
|
+
# target: exp { print val[0] }
|
101
|
+
#
|
102
|
+
# exp: exp '+' exp
|
103
|
+
# | exp '*' exp
|
104
|
+
# | '(' exp ')'
|
105
|
+
# | NUMBER
|
106
|
+
# end
|
107
|
+
#
|
108
|
+
# Racc grammar files resemble yacc files.
|
109
|
+
# But (of course), this is Ruby code.
|
110
|
+
# yacc's $$ is the 'result', $0, $1... is
|
111
|
+
# an array called 'val', and $-1, $-2... is an array called '_values'.
|
112
|
+
#
|
113
|
+
# See the {Grammar File Reference}[rdoc-ref:lib/racc/rdoc/grammar.en.rdoc] for
|
114
|
+
# more information on grammar files.
|
115
|
+
#
|
116
|
+
# == Parser
|
117
|
+
#
|
118
|
+
# Then you must prepare the parse entry method. There are two types of
|
119
|
+
# parse methods in Racc, Racc::Parser#do_parse and Racc::Parser#yyparse
|
120
|
+
#
|
121
|
+
# Racc::Parser#do_parse is simple.
|
122
|
+
#
|
123
|
+
# It's yyparse() of yacc, and Racc::Parser#next_token is yylex().
|
124
|
+
# This method must returns an array like [TOKENSYMBOL, ITS_VALUE].
|
125
|
+
# EOF is [false, false].
|
126
|
+
# (TOKENSYMBOL is a Ruby symbol (taken from String#intern) by default.
|
127
|
+
# If you want to change this, see the grammar reference.
|
128
|
+
#
|
129
|
+
# Racc::Parser#yyparse is little complicated, but useful.
|
130
|
+
# It does not use Racc::Parser#next_token, instead it gets tokens from any iterator.
|
131
|
+
#
|
132
|
+
# For example, <code>yyparse(obj, :scan)</code> causes
|
133
|
+
# calling +obj#scan+, and you can return tokens by yielding them from +obj#scan+.
|
134
|
+
#
|
135
|
+
# == Debugging
|
136
|
+
#
|
137
|
+
# When debugging, "-v" or/and the "-g" option is helpful.
|
138
|
+
#
|
139
|
+
# "-v" creates verbose log file (.output).
|
140
|
+
# "-g" creates a "Verbose Parser".
|
141
|
+
# Verbose Parser prints the internal status when parsing.
|
142
|
+
# But it's _not_ automatic.
|
143
|
+
# You must use -g option and set +@yydebug+ to +true+ in order to get output.
|
144
|
+
# -g option only creates the verbose parser.
|
145
|
+
#
|
146
|
+
# === Racc reported syntax error.
|
147
|
+
#
|
148
|
+
# Isn't there too many "end"?
|
149
|
+
# grammar of racc file is changed in v0.10.
|
150
|
+
#
|
151
|
+
# Racc does not use '%' mark, while yacc uses huge number of '%' marks..
|
152
|
+
#
|
153
|
+
# === Racc reported "XXXX conflicts".
|
154
|
+
#
|
155
|
+
# Try "racc -v xxxx.y".
|
156
|
+
# It causes producing racc's internal log file, xxxx.output.
|
157
|
+
#
|
158
|
+
# === Generated parsers does not work correctly
|
159
|
+
#
|
160
|
+
# Try "racc -g xxxx.y".
|
161
|
+
# This command let racc generate "debugging parser".
|
162
|
+
# Then set @yydebug=true in your parser.
|
163
|
+
# It produces a working log of your parser.
|
164
|
+
#
|
165
|
+
# == Re-distributing Racc runtime
|
166
|
+
#
|
167
|
+
# A parser, which is created by Racc, requires the Racc runtime module;
|
168
|
+
# racc/parser.rb.
|
169
|
+
#
|
170
|
+
# Ruby 1.8.x comes with Racc runtime module,
|
171
|
+
# you need NOT distribute Racc runtime files.
|
172
|
+
#
|
173
|
+
# If you want to include the Racc runtime module with your parser.
|
174
|
+
# This can be done by using '-E' option:
|
175
|
+
#
|
176
|
+
# $ racc -E -omyparser.rb myparser.y
|
177
|
+
#
|
178
|
+
# This command creates myparser.rb which `includes' Racc runtime.
|
179
|
+
# Only you must do is to distribute your parser file (myparser.rb).
|
180
|
+
#
|
181
|
+
# Note: parser.rb is LGPL, but your parser is not.
|
182
|
+
# Your own parser is completely yours.
|
183
|
+
module Racc
|
184
|
+
|
185
|
+
unless defined?(Racc_No_Extentions)
|
186
|
+
Racc_No_Extentions = false # :nodoc:
|
187
|
+
end
|
188
|
+
|
189
|
+
class Parser
|
190
|
+
|
191
|
+
Racc_Runtime_Version = ::Racc::VERSION
|
192
|
+
Racc_Runtime_Revision = '$Id: 3c520ba1f2996b86abc21eeb54768934a7be9d0c $'
|
193
|
+
|
194
|
+
Racc_Runtime_Core_Version_R = ::Racc::VERSION
|
195
|
+
Racc_Runtime_Core_Revision_R = '$Id: 3c520ba1f2996b86abc21eeb54768934a7be9d0c $'.split[1]
|
196
|
+
begin
|
197
|
+
require 'racc/cparse'
|
198
|
+
# Racc_Runtime_Core_Version_C = (defined in extention)
|
199
|
+
Racc_Runtime_Core_Revision_C = Racc_Runtime_Core_Id_C.split[2]
|
200
|
+
unless new.respond_to?(:_racc_do_parse_c, true)
|
201
|
+
raise LoadError, 'old cparse.so'
|
202
|
+
end
|
203
|
+
if Racc_No_Extentions
|
204
|
+
raise LoadError, 'selecting ruby version of racc runtime core'
|
205
|
+
end
|
206
|
+
|
207
|
+
Racc_Main_Parsing_Routine = :_racc_do_parse_c # :nodoc:
|
208
|
+
Racc_YY_Parse_Method = :_racc_yyparse_c # :nodoc:
|
209
|
+
Racc_Runtime_Core_Version = Racc_Runtime_Core_Version_C # :nodoc:
|
210
|
+
Racc_Runtime_Core_Revision = Racc_Runtime_Core_Revision_C # :nodoc:
|
211
|
+
Racc_Runtime_Type = 'c' # :nodoc:
|
212
|
+
rescue LoadError
|
213
|
+
Racc_Main_Parsing_Routine = :_racc_do_parse_rb
|
214
|
+
Racc_YY_Parse_Method = :_racc_yyparse_rb
|
215
|
+
Racc_Runtime_Core_Version = Racc_Runtime_Core_Version_R
|
216
|
+
Racc_Runtime_Core_Revision = Racc_Runtime_Core_Revision_R
|
217
|
+
Racc_Runtime_Type = 'ruby'
|
218
|
+
end
|
219
|
+
|
220
|
+
def Parser.racc_runtime_type # :nodoc:
|
221
|
+
Racc_Runtime_Type
|
222
|
+
end
|
223
|
+
|
224
|
+
def _racc_setup
|
225
|
+
@yydebug = false unless self.class::Racc_debug_parser
|
226
|
+
@yydebug = false unless defined?(@yydebug)
|
227
|
+
if @yydebug
|
228
|
+
@racc_debug_out = $stderr unless defined?(@racc_debug_out)
|
229
|
+
@racc_debug_out ||= $stderr
|
230
|
+
end
|
231
|
+
arg = self.class::Racc_arg
|
232
|
+
arg[13] = true if arg.size < 14
|
233
|
+
arg
|
234
|
+
end
|
235
|
+
|
236
|
+
def _racc_init_sysvars
|
237
|
+
@racc_state = [0]
|
238
|
+
@racc_tstack = []
|
239
|
+
@racc_vstack = []
|
240
|
+
|
241
|
+
@racc_t = nil
|
242
|
+
@racc_val = nil
|
243
|
+
|
244
|
+
@racc_read_next = true
|
245
|
+
|
246
|
+
@racc_user_yyerror = false
|
247
|
+
@racc_error_status = 0
|
248
|
+
end
|
249
|
+
|
250
|
+
# The entry point of the parser. This method is used with #next_token.
|
251
|
+
# If Racc wants to get token (and its value), calls next_token.
|
252
|
+
#
|
253
|
+
# Example:
|
254
|
+
# def parse
|
255
|
+
# @q = [[1,1],
|
256
|
+
# [2,2],
|
257
|
+
# [3,3],
|
258
|
+
# [false, '$']]
|
259
|
+
# do_parse
|
260
|
+
# end
|
261
|
+
#
|
262
|
+
# def next_token
|
263
|
+
# @q.shift
|
264
|
+
# end
|
265
|
+
def do_parse
|
266
|
+
__send__(Racc_Main_Parsing_Routine, _racc_setup(), false)
|
267
|
+
end
|
268
|
+
|
269
|
+
# The method to fetch next token.
|
270
|
+
# If you use #do_parse method, you must implement #next_token.
|
271
|
+
#
|
272
|
+
# The format of return value is [TOKEN_SYMBOL, VALUE].
|
273
|
+
# +token-symbol+ is represented by Ruby's symbol by default, e.g. :IDENT
|
274
|
+
# for 'IDENT'. ";" (String) for ';'.
|
275
|
+
#
|
276
|
+
# The final symbol (End of file) must be false.
|
277
|
+
def next_token
|
278
|
+
raise NotImplementedError, "#{self.class}\#next_token is not defined"
|
279
|
+
end
|
280
|
+
|
281
|
+
def _racc_do_parse_rb(arg, in_debug)
|
282
|
+
action_table, action_check, action_default, action_pointer,
|
283
|
+
_, _, _, _,
|
284
|
+
_, _, token_table, * = arg
|
285
|
+
|
286
|
+
_racc_init_sysvars
|
287
|
+
tok = act = i = nil
|
288
|
+
|
289
|
+
catch(:racc_end_parse) {
|
290
|
+
while true
|
291
|
+
if i = action_pointer[@racc_state[-1]]
|
292
|
+
if @racc_read_next
|
293
|
+
if @racc_t != 0 # not EOF
|
294
|
+
tok, @racc_val = next_token()
|
295
|
+
unless tok # EOF
|
296
|
+
@racc_t = 0
|
297
|
+
else
|
298
|
+
@racc_t = (token_table[tok] or 1) # error token
|
299
|
+
end
|
300
|
+
racc_read_token(@racc_t, tok, @racc_val) if @yydebug
|
301
|
+
@racc_read_next = false
|
302
|
+
end
|
303
|
+
end
|
304
|
+
i += @racc_t
|
305
|
+
unless i >= 0 and
|
306
|
+
act = action_table[i] and
|
307
|
+
action_check[i] == @racc_state[-1]
|
308
|
+
act = action_default[@racc_state[-1]]
|
309
|
+
end
|
310
|
+
else
|
311
|
+
act = action_default[@racc_state[-1]]
|
312
|
+
end
|
313
|
+
while act = _racc_evalact(act, arg)
|
314
|
+
;
|
315
|
+
end
|
316
|
+
end
|
317
|
+
}
|
318
|
+
end
|
319
|
+
|
320
|
+
# Another entry point for the parser.
|
321
|
+
# If you use this method, you must implement RECEIVER#METHOD_ID method.
|
322
|
+
#
|
323
|
+
# RECEIVER#METHOD_ID is a method to get next token.
|
324
|
+
# It must 'yield' the token, which format is [TOKEN-SYMBOL, VALUE].
|
325
|
+
def yyparse(recv, mid)
|
326
|
+
__send__(Racc_YY_Parse_Method, recv, mid, _racc_setup(), true)
|
327
|
+
end
|
328
|
+
|
329
|
+
def _racc_yyparse_rb(recv, mid, arg, c_debug)
|
330
|
+
action_table, action_check, action_default, action_pointer,
|
331
|
+
_, _, _, _,
|
332
|
+
_, _, token_table, * = arg
|
333
|
+
|
334
|
+
_racc_init_sysvars
|
335
|
+
|
336
|
+
catch(:racc_end_parse) {
|
337
|
+
until i = action_pointer[@racc_state[-1]]
|
338
|
+
while act = _racc_evalact(action_default[@racc_state[-1]], arg)
|
339
|
+
;
|
340
|
+
end
|
341
|
+
end
|
342
|
+
recv.__send__(mid) do |tok, val|
|
343
|
+
unless tok
|
344
|
+
@racc_t = 0
|
345
|
+
else
|
346
|
+
@racc_t = (token_table[tok] or 1) # error token
|
347
|
+
end
|
348
|
+
@racc_val = val
|
349
|
+
@racc_read_next = false
|
350
|
+
|
351
|
+
i += @racc_t
|
352
|
+
unless i >= 0 and
|
353
|
+
act = action_table[i] and
|
354
|
+
action_check[i] == @racc_state[-1]
|
355
|
+
act = action_default[@racc_state[-1]]
|
356
|
+
end
|
357
|
+
while act = _racc_evalact(act, arg)
|
358
|
+
;
|
359
|
+
end
|
360
|
+
|
361
|
+
while !(i = action_pointer[@racc_state[-1]]) ||
|
362
|
+
! @racc_read_next ||
|
363
|
+
@racc_t == 0 # $
|
364
|
+
unless i and i += @racc_t and
|
365
|
+
i >= 0 and
|
366
|
+
act = action_table[i] and
|
367
|
+
action_check[i] == @racc_state[-1]
|
368
|
+
act = action_default[@racc_state[-1]]
|
369
|
+
end
|
370
|
+
while act = _racc_evalact(act, arg)
|
371
|
+
;
|
372
|
+
end
|
373
|
+
end
|
374
|
+
end
|
375
|
+
}
|
376
|
+
end
|
377
|
+
|
378
|
+
###
|
379
|
+
### common
|
380
|
+
###
|
381
|
+
|
382
|
+
def _racc_evalact(act, arg)
|
383
|
+
action_table, action_check, _, action_pointer,
|
384
|
+
_, _, _, _,
|
385
|
+
_, _, _, shift_n,
|
386
|
+
reduce_n, * = arg
|
387
|
+
nerr = 0 # tmp
|
388
|
+
|
389
|
+
if act > 0 and act < shift_n
|
390
|
+
#
|
391
|
+
# shift
|
392
|
+
#
|
393
|
+
if @racc_error_status > 0
|
394
|
+
@racc_error_status -= 1 unless @racc_t == 1 # error token
|
395
|
+
end
|
396
|
+
@racc_vstack.push @racc_val
|
397
|
+
@racc_state.push act
|
398
|
+
@racc_read_next = true
|
399
|
+
if @yydebug
|
400
|
+
@racc_tstack.push @racc_t
|
401
|
+
racc_shift @racc_t, @racc_tstack, @racc_vstack
|
402
|
+
end
|
403
|
+
|
404
|
+
elsif act < 0 and act > -reduce_n
|
405
|
+
#
|
406
|
+
# reduce
|
407
|
+
#
|
408
|
+
code = catch(:racc_jump) {
|
409
|
+
@racc_state.push _racc_do_reduce(arg, act)
|
410
|
+
false
|
411
|
+
}
|
412
|
+
if code
|
413
|
+
case code
|
414
|
+
when 1 # yyerror
|
415
|
+
@racc_user_yyerror = true # user_yyerror
|
416
|
+
return -reduce_n
|
417
|
+
when 2 # yyaccept
|
418
|
+
return shift_n
|
419
|
+
else
|
420
|
+
raise '[Racc Bug] unknown jump code'
|
421
|
+
end
|
422
|
+
end
|
423
|
+
|
424
|
+
elsif act == shift_n
|
425
|
+
#
|
426
|
+
# accept
|
427
|
+
#
|
428
|
+
racc_accept if @yydebug
|
429
|
+
throw :racc_end_parse, @racc_vstack[0]
|
430
|
+
|
431
|
+
elsif act == -reduce_n
|
432
|
+
#
|
433
|
+
# error
|
434
|
+
#
|
435
|
+
case @racc_error_status
|
436
|
+
when 0
|
437
|
+
unless arg[21] # user_yyerror
|
438
|
+
nerr += 1
|
439
|
+
on_error @racc_t, @racc_val, @racc_vstack
|
440
|
+
end
|
441
|
+
when 3
|
442
|
+
if @racc_t == 0 # is $
|
443
|
+
throw :racc_end_parse, nil
|
444
|
+
end
|
445
|
+
@racc_read_next = true
|
446
|
+
end
|
447
|
+
@racc_user_yyerror = false
|
448
|
+
@racc_error_status = 3
|
449
|
+
while true
|
450
|
+
if i = action_pointer[@racc_state[-1]]
|
451
|
+
i += 1 # error token
|
452
|
+
if i >= 0 and
|
453
|
+
(act = action_table[i]) and
|
454
|
+
action_check[i] == @racc_state[-1]
|
455
|
+
break
|
456
|
+
end
|
457
|
+
end
|
458
|
+
throw :racc_end_parse, nil if @racc_state.size <= 1
|
459
|
+
@racc_state.pop
|
460
|
+
@racc_vstack.pop
|
461
|
+
if @yydebug
|
462
|
+
@racc_tstack.pop
|
463
|
+
racc_e_pop @racc_state, @racc_tstack, @racc_vstack
|
464
|
+
end
|
465
|
+
end
|
466
|
+
return act
|
467
|
+
|
468
|
+
else
|
469
|
+
raise "[Racc Bug] unknown action #{act.inspect}"
|
470
|
+
end
|
471
|
+
|
472
|
+
racc_next_state(@racc_state[-1], @racc_state) if @yydebug
|
473
|
+
|
474
|
+
nil
|
475
|
+
end
|
476
|
+
|
477
|
+
def _racc_do_reduce(arg, act)
|
478
|
+
_, _, _, _,
|
479
|
+
goto_table, goto_check, goto_default, goto_pointer,
|
480
|
+
nt_base, reduce_table, _, _,
|
481
|
+
_, use_result, * = arg
|
482
|
+
|
483
|
+
state = @racc_state
|
484
|
+
vstack = @racc_vstack
|
485
|
+
tstack = @racc_tstack
|
486
|
+
|
487
|
+
i = act * -3
|
488
|
+
len = reduce_table[i]
|
489
|
+
reduce_to = reduce_table[i+1]
|
490
|
+
method_id = reduce_table[i+2]
|
491
|
+
void_array = []
|
492
|
+
|
493
|
+
tmp_t = tstack[-len, len] if @yydebug
|
494
|
+
tmp_v = vstack[-len, len]
|
495
|
+
tstack[-len, len] = void_array if @yydebug
|
496
|
+
vstack[-len, len] = void_array
|
497
|
+
state[-len, len] = void_array
|
498
|
+
|
499
|
+
# tstack must be updated AFTER method call
|
500
|
+
if use_result
|
501
|
+
vstack.push __send__(method_id, tmp_v, vstack, tmp_v[0])
|
502
|
+
else
|
503
|
+
vstack.push __send__(method_id, tmp_v, vstack)
|
504
|
+
end
|
505
|
+
tstack.push reduce_to
|
506
|
+
|
507
|
+
racc_reduce(tmp_t, reduce_to, tstack, vstack) if @yydebug
|
508
|
+
|
509
|
+
k1 = reduce_to - nt_base
|
510
|
+
if i = goto_pointer[k1]
|
511
|
+
i += state[-1]
|
512
|
+
if i >= 0 and (curstate = goto_table[i]) and goto_check[i] == k1
|
513
|
+
return curstate
|
514
|
+
end
|
515
|
+
end
|
516
|
+
goto_default[k1]
|
517
|
+
end
|
518
|
+
|
519
|
+
# This method is called when a parse error is found.
|
520
|
+
#
|
521
|
+
# ERROR_TOKEN_ID is an internal ID of token which caused error.
|
522
|
+
# You can get string representation of this ID by calling
|
523
|
+
# #token_to_str.
|
524
|
+
#
|
525
|
+
# ERROR_VALUE is a value of error token.
|
526
|
+
#
|
527
|
+
# value_stack is a stack of symbol values.
|
528
|
+
# DO NOT MODIFY this object.
|
529
|
+
#
|
530
|
+
# This method raises ParseError by default.
|
531
|
+
#
|
532
|
+
# If this method returns, parsers enter "error recovering mode".
|
533
|
+
def on_error(t, val, vstack)
|
534
|
+
raise ParseError, sprintf("\nparse error on value %s (%s)",
|
535
|
+
val.inspect, token_to_str(t) || '?')
|
536
|
+
end
|
537
|
+
|
538
|
+
# Enter error recovering mode.
|
539
|
+
# This method does not call #on_error.
|
540
|
+
def yyerror
|
541
|
+
throw :racc_jump, 1
|
542
|
+
end
|
543
|
+
|
544
|
+
# Exit parser.
|
545
|
+
# Return value is Symbol_Value_Stack[0].
|
546
|
+
def yyaccept
|
547
|
+
throw :racc_jump, 2
|
548
|
+
end
|
549
|
+
|
550
|
+
# Leave error recovering mode.
|
551
|
+
def yyerrok
|
552
|
+
@racc_error_status = 0
|
553
|
+
end
|
554
|
+
|
555
|
+
# For debugging output
|
556
|
+
def racc_read_token(t, tok, val)
|
557
|
+
@racc_debug_out.print 'read '
|
558
|
+
@racc_debug_out.print tok.inspect, '(', racc_token2str(t), ') '
|
559
|
+
@racc_debug_out.puts val.inspect
|
560
|
+
@racc_debug_out.puts
|
561
|
+
end
|
562
|
+
|
563
|
+
def racc_shift(tok, tstack, vstack)
|
564
|
+
@racc_debug_out.puts "shift #{racc_token2str tok}"
|
565
|
+
racc_print_stacks tstack, vstack
|
566
|
+
@racc_debug_out.puts
|
567
|
+
end
|
568
|
+
|
569
|
+
def racc_reduce(toks, sim, tstack, vstack)
|
570
|
+
out = @racc_debug_out
|
571
|
+
out.print 'reduce '
|
572
|
+
if toks.empty?
|
573
|
+
out.print ' <none>'
|
574
|
+
else
|
575
|
+
toks.each {|t| out.print ' ', racc_token2str(t) }
|
576
|
+
end
|
577
|
+
out.puts " --> #{racc_token2str(sim)}"
|
578
|
+
racc_print_stacks tstack, vstack
|
579
|
+
@racc_debug_out.puts
|
580
|
+
end
|
581
|
+
|
582
|
+
def racc_accept
|
583
|
+
@racc_debug_out.puts 'accept'
|
584
|
+
@racc_debug_out.puts
|
585
|
+
end
|
586
|
+
|
587
|
+
def racc_e_pop(state, tstack, vstack)
|
588
|
+
@racc_debug_out.puts 'error recovering mode: pop token'
|
589
|
+
racc_print_states state
|
590
|
+
racc_print_stacks tstack, vstack
|
591
|
+
@racc_debug_out.puts
|
592
|
+
end
|
593
|
+
|
594
|
+
def racc_next_state(curstate, state)
|
595
|
+
@racc_debug_out.puts "goto #{curstate}"
|
596
|
+
racc_print_states state
|
597
|
+
@racc_debug_out.puts
|
598
|
+
end
|
599
|
+
|
600
|
+
def racc_print_stacks(t, v)
|
601
|
+
out = @racc_debug_out
|
602
|
+
out.print ' ['
|
603
|
+
t.each_index do |i|
|
604
|
+
out.print ' (', racc_token2str(t[i]), ' ', v[i].inspect, ')'
|
605
|
+
end
|
606
|
+
out.puts ' ]'
|
607
|
+
end
|
608
|
+
|
609
|
+
def racc_print_states(s)
|
610
|
+
out = @racc_debug_out
|
611
|
+
out.print ' ['
|
612
|
+
s.each {|st| out.print ' ', st }
|
613
|
+
out.puts ' ]'
|
614
|
+
end
|
615
|
+
|
616
|
+
def racc_token2str(tok)
|
617
|
+
self.class::Racc_token_to_s_table[tok] or
|
618
|
+
raise "[Racc Bug] can't convert token #{tok} to string"
|
619
|
+
end
|
620
|
+
|
621
|
+
# Convert internal ID of token symbol to the string.
|
622
|
+
def token_to_str(t)
|
623
|
+
self.class::Racc_token_to_s_table[t]
|
624
|
+
end
|
625
|
+
|
626
|
+
end
|
627
|
+
|
628
|
+
end
|
629
|
+
|
630
|
+
__end_of_file__
|
631
|
+
end
|