1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
module Lrama
class StatesReporter
include Lrama::Report::Duration
def initialize(states)
@states = states
end
def report(io, **options)
report_duration(:report) do
_report(io, **options)
end
end
private
def _report(io, grammar: false, states: false, itemsets: false, lookaheads: false, solved: false, verbose: false)
# TODO: Unused terms
# TODO: Unused rules
report_conflicts(io)
report_grammar(io) if grammar
report_states(io, itemsets, lookaheads, solved, verbose)
end
def report_conflicts(io)
has_conflict = false
@states.states.each do |state|
messages = []
cs = state.conflicts.group_by(&:type)
if cs[:shift_reduce]
messages << "#{cs[:shift_reduce].count} shift/reduce"
end
if cs[:reduce_reduce]
messages << "#{cs[:reduce_reduce].count} reduce/reduce"
end
if !messages.empty?
has_conflict = true
io << "State #{state.id} conflicts: #{messages.join(', ')}\n"
end
end
if has_conflict
io << "\n\n"
end
end
def report_grammar(io)
io << "Grammar\n"
last_lhs = nil
@states.rules.each do |rule|
if rule.rhs.empty?
r = "ε"
else
r = rule.rhs.map(&:display_name).join(" ")
end
if rule.lhs == last_lhs
io << sprintf("%5d %s| %s\n", rule.id, " " * rule.lhs.display_name.length, r)
else
io << "\n"
io << sprintf("%5d %s: %s\n", rule.id, rule.lhs.display_name, r)
end
last_lhs = rule.lhs
end
io << "\n\n"
end
def report_states(io, itemsets, lookaheads, solved, verbose)
@states.states.each do |state|
# Report State
io << "State #{state.id}\n\n"
# Report item
last_lhs = nil
list = itemsets ? state.items : state.kernels
list.sort_by {|i| [i.rule_id, i.position] }.each do |item|
rule = item.rule
position = item.position
if rule.rhs.empty?
r = "ε •"
else
r = rule.rhs.map(&:display_name).insert(position, "•").join(" ")
end
if rule.lhs == last_lhs
l = " " * rule.lhs.id.s_value.length + "|"
else
l = rule.lhs.id.s_value + ":"
end
la = ""
if lookaheads && item.end_of_rule?
reduce = state.find_reduce_by_item!(item)
look_ahead = reduce.selected_look_ahead
if !look_ahead.empty?
la = " [#{look_ahead.map(&:display_name).join(", ")}]"
end
end
last_lhs = rule.lhs
io << sprintf("%5i %s %s%s\n", rule.id, l, r, la)
end
io << "\n"
# Report shifts
tmp = state.term_transitions.select do |shift, _|
!shift.not_selected
end.map do |shift, next_state|
[shift.next_sym, next_state.id]
end
max_len = tmp.map(&:first).map(&:display_name).map(&:length).max
tmp.each do |term, state_id|
io << " #{term.display_name.ljust(max_len)} shift, and go to state #{state_id}\n"
end
io << "\n" if !tmp.empty?
# Report error caused by %nonassoc
nl = false
tmp = state.resolved_conflicts.select do |resolved|
resolved.which == :error
end.map do |error|
error.symbol.display_name
end
max_len = tmp.map(&:length).max
tmp.each do |name|
nl = true
io << " #{name.ljust(max_len)} error (nonassociative)\n"
end
io << "\n" if !tmp.empty?
# Report reduces
nl = false
max_len = state.non_default_reduces.flat_map(&:look_ahead).compact.map(&:display_name).map(&:length).max || 0
max_len = [max_len, "$default".length].max if state.default_reduction_rule
ary = []
state.non_default_reduces.each do |reduce|
reduce.look_ahead.each do |term|
ary << [term, reduce]
end
end
ary.sort_by do |term, reduce|
term.number
end.each do |term, reduce|
rule = reduce.item.rule
io << " #{term.display_name.ljust(max_len)} reduce using rule #{rule.id} (#{rule.lhs.display_name})\n"
nl = true
end
if r = state.default_reduction_rule
nl = true
s = "$default".ljust(max_len)
if r.initial_rule?
io << " #{s} accept\n"
else
io << " #{s} reduce using rule #{r.id} (#{r.lhs.display_name})\n"
end
end
io << "\n" if nl
# Report nonterminal transitions
tmp = []
max_len = 0
state.nterm_transitions.each do |shift, next_state|
nterm = shift.next_sym
tmp << [nterm, next_state.id]
max_len = [max_len, nterm.id.s_value.length].max
end
tmp.uniq!
tmp.sort_by! do |nterm, state_id|
nterm.number
end
tmp.each do |nterm, state_id|
io << " #{nterm.id.s_value.ljust(max_len)} go to state #{state_id}\n"
end
io << "\n" if !tmp.empty?
if solved
# Report conflict resolutions
state.resolved_conflicts.each do |resolved|
io << " #{resolved.report_message}\n"
end
io << "\n" if !state.resolved_conflicts.empty?
end
if verbose
# Report direct_read_sets
io << " [Direct Read sets]\n"
direct_read_sets = @states.direct_read_sets
@states.nterms.each do |nterm|
terms = direct_read_sets[[state.id, nterm.token_id]]
next if !terms
next if terms.empty?
str = terms.map {|sym| sym.id.s_value }.join(", ")
io << " read #{nterm.id.s_value} shift #{str}\n"
end
io << "\n"
# Reprot reads_relation
io << " [Reads Relation]\n"
@states.nterms.each do |nterm|
a = @states.reads_relation[[state.id, nterm.token_id]]
next if !a
a.each do |state_id2, nterm_id2|
n = @states.nterms.find {|n| n.token_id == nterm_id2 }
io << " (State #{state_id2}, #{n.id.s_value})\n"
end
end
io << "\n"
# Reprot read_sets
io << " [Read sets]\n"
read_sets = @states.read_sets
@states.nterms.each do |nterm|
terms = read_sets[[state.id, nterm.token_id]]
next if !terms
next if terms.empty?
terms.each do |sym|
io << " #{sym.id.s_value}\n"
end
end
io << "\n"
# Reprot includes_relation
io << " [Includes Relation]\n"
@states.nterms.each do |nterm|
a = @states.includes_relation[[state.id, nterm.token_id]]
next if !a
a.each do |state_id2, nterm_id2|
n = @states.nterms.find {|n| n.token_id == nterm_id2 }
io << " (State #{state.id}, #{nterm.id.s_value}) -> (State #{state_id2}, #{n.id.s_value})\n"
end
end
io << "\n"
# Report lookback_relation
io << " [Lookback Relation]\n"
@states.rules.each do |rule|
a = @states.lookback_relation[[state.id, rule.id]]
next if !a
a.each do |state_id2, nterm_id2|
n = @states.nterms.find {|n| n.token_id == nterm_id2 }
io << " (Rule: #{rule.to_s}) -> (State #{state_id2}, #{n.id.s_value})\n"
end
end
io << "\n"
# Reprot follow_sets
io << " [Follow sets]\n"
follow_sets = @states.follow_sets
@states.nterms.each do |nterm|
terms = follow_sets[[state.id, nterm.token_id]]
next if !terms
terms.each do |sym|
io << " #{nterm.id.s_value} -> #{sym.id.s_value}\n"
end
end
io << "\n"
# Report LA
io << " [Look-Ahead Sets]\n"
tmp = []
max_len = 0
@states.rules.each do |rule|
syms = @states.la[[state.id, rule.id]]
next if !syms
tmp << [rule, syms]
max_len = ([max_len] + syms.map {|s| s.id.s_value.length }).max
end
tmp.each do |rule, syms|
syms.each do |sym|
io << " #{sym.id.s_value.ljust(max_len)} reduce using rule #{rule.id} (#{rule.lhs.id.s_value})\n"
end
end
io << "\n" if !tmp.empty?
end
# End of Report State
io << "\n"
end
end
end
end
|