ed-precompiled_racc 1.8.1-x86_64-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/racc/iset.rb ADDED
@@ -0,0 +1,92 @@
1
+ #--
2
+ #
3
+ #
4
+ #
5
+ # Copyright (c) 1999-2006 Minero Aoki
6
+ #
7
+ # This program is free software.
8
+ # You can distribute/modify this program under the same terms of ruby.
9
+ # see the file "COPYING".
10
+ #
11
+ #++
12
+
13
+ module Racc
14
+
15
+ # An "indexed" set. All items must respond to :ident.
16
+ class ISet
17
+
18
+ def initialize(a = [])
19
+ @set = a
20
+ end
21
+
22
+ attr_reader :set
23
+
24
+ def add(i)
25
+ @set[i.ident] = i
26
+ end
27
+
28
+ def [](key)
29
+ @set[key.ident]
30
+ end
31
+
32
+ def []=(key, val)
33
+ @set[key.ident] = val
34
+ end
35
+
36
+ alias include? []
37
+ alias key? []
38
+
39
+ def update(other)
40
+ s = @set
41
+ o = other.set
42
+ o.each_index do |idx|
43
+ if t = o[idx]
44
+ s[idx] = t
45
+ end
46
+ end
47
+ end
48
+
49
+ def update_a(a)
50
+ s = @set
51
+ a.each {|i| s[i.ident] = i }
52
+ end
53
+
54
+ def delete(key)
55
+ i = @set[key.ident]
56
+ @set[key.ident] = nil
57
+ i
58
+ end
59
+
60
+ def each(&block)
61
+ @set.compact.each(&block)
62
+ end
63
+
64
+ def to_a
65
+ @set.compact
66
+ end
67
+
68
+ def to_s
69
+ "[#{@set.compact.join(' ')}]"
70
+ end
71
+
72
+ alias inspect to_s
73
+
74
+ def size
75
+ @set.nitems
76
+ end
77
+
78
+ def empty?
79
+ @set.nitems == 0
80
+ end
81
+
82
+ def clear
83
+ @set.clear
84
+ end
85
+
86
+ def dup
87
+ ISet.new(@set.dup)
88
+ end
89
+
90
+ end # class ISet
91
+
92
+ end # module Racc
@@ -0,0 +1,212 @@
1
+ #--
2
+ #
3
+ #
4
+ #
5
+ # Copyright (c) 1999-2006 Minero Aoki
6
+ #
7
+ # This program is free software.
8
+ # You can distribute/modify this program under the same terms of ruby.
9
+ # see the file "COPYING".
10
+ #
11
+ #++
12
+
13
+ module Racc
14
+
15
+ class LogFileGenerator
16
+
17
+ def initialize(states, debug_flags = DebugFlags.new)
18
+ @states = states
19
+ @grammar = states.grammar
20
+ @debug_flags = debug_flags
21
+ end
22
+
23
+ def output(out)
24
+ output_conflict out; out.puts
25
+ output_useless out; out.puts
26
+ output_rule out; out.puts
27
+ output_token out; out.puts
28
+ output_state out
29
+ end
30
+
31
+ #
32
+ # Warnings
33
+ #
34
+
35
+ def output_conflict(out)
36
+ @states.each do |state|
37
+ if state.srconf
38
+ out.printf "state %d contains %d shift/reduce conflicts\n",
39
+ state.stateid, state.srconf.size
40
+ end
41
+ if state.rrconf
42
+ out.printf "state %d contains %d reduce/reduce conflicts\n",
43
+ state.stateid, state.rrconf.size
44
+ end
45
+ end
46
+ end
47
+
48
+ def output_useless(out)
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
+ sr = state.srconf && state.srconf.dup
94
+ rr = state.rrconf && state.rrconf.dup
95
+ acts = state.action
96
+ keys = acts.keys
97
+ keys.sort! {|a,b| a.ident <=> b.ident }
98
+
99
+ [ Shift, Reduce, Error, Accept ].each do |klass|
100
+ keys.delete_if do |tok|
101
+ act = acts[tok]
102
+ if act.kind_of?(klass)
103
+ outact f, tok, act
104
+ if sr and c = sr.delete(tok)
105
+ outsrconf f, c
106
+ end
107
+ if rr and c = rr.delete(tok)
108
+ outrrconf f, c
109
+ end
110
+
111
+ true
112
+ else
113
+ false
114
+ end
115
+ end
116
+ end
117
+ sr.each {|tok, c| outsrconf f, c } if sr
118
+ rr.each {|tok, c| outrrconf f, c } if rr
119
+
120
+ act = state.defact
121
+ if not act.kind_of?(Error) or @debug_flags.any?
122
+ outact f, '$default', act
123
+ end
124
+
125
+ f.puts
126
+ state.goto_table.each do |t, st|
127
+ if t.nonterminal?
128
+ f.printf " %-12s go to state %d\n", t.to_s, st.ident
129
+ end
130
+ end
131
+ end
132
+
133
+ def outact(f, t, act)
134
+ case act
135
+ when Shift
136
+ f.printf " %-12s shift, and go to state %d\n",
137
+ t.to_s, act.goto_id
138
+ when Reduce
139
+ f.printf " %-12s reduce using rule %d (%s)\n",
140
+ t.to_s, act.ruleid, act.rule.target.to_s
141
+ when Accept
142
+ f.printf " %-12s accept\n", t.to_s
143
+ when Error
144
+ f.printf " %-12s error\n", t.to_s
145
+ else
146
+ raise "racc: fatal: wrong act for outact: act=#{act}(#{act.class})"
147
+ end
148
+ end
149
+
150
+ def outsrconf(f, confs)
151
+ confs.each do |c|
152
+ r = c.reduce
153
+ f.printf " %-12s [reduce using rule %d (%s)]\n",
154
+ c.shift.to_s, r.ident, r.target.to_s
155
+ end
156
+ end
157
+
158
+ def outrrconf(f, confs)
159
+ confs.each do |c|
160
+ r = c.low_prec
161
+ f.printf " %-12s [reduce using rule %d (%s)]\n",
162
+ c.token.to_s, r.ident, r.target.to_s
163
+ end
164
+ end
165
+
166
+ #
167
+ # Rules
168
+ #
169
+
170
+ def output_rule(out)
171
+ out.print "-------- Grammar --------\n\n"
172
+ @grammar.each do |rl|
173
+ if @debug_flags.any? or rl.ident != 0
174
+ out.printf "rule %d %s: %s\n",
175
+ rl.ident, rl.target.to_s, rl.symbols.join(' ')
176
+ end
177
+ end
178
+ end
179
+
180
+ #
181
+ # Tokens
182
+ #
183
+
184
+ def output_token(out)
185
+ out.print "------- Symbols -------\n\n"
186
+
187
+ out.print "**Nonterminals, with rules where they appear\n\n"
188
+ @grammar.each_nonterminal do |t|
189
+ tmp = <<SRC
190
+ %s (%d)
191
+ on right: %s
192
+ on left : %s
193
+ SRC
194
+ out.printf tmp, t.to_s, t.ident,
195
+ symbol_locations(t.locate).join(' '),
196
+ symbol_locations(t.heads).join(' ')
197
+ end
198
+
199
+ out.print "\n**Terminals, with rules where they appear\n\n"
200
+ @grammar.each_terminal do |t|
201
+ out.printf " %s (%d) %s\n",
202
+ t.to_s, t.ident, symbol_locations(t.locate).join(' ')
203
+ end
204
+ end
205
+
206
+ def symbol_locations(locs)
207
+ locs.map {|loc| loc.rule.ident }.reject {|n| n == 0 }.uniq
208
+ end
209
+
210
+ end
211
+
212
+ end # module Racc