Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions lib/lrama/reporter/conflicts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ def report_conflicts(io, states)
has_conflict = false

states.states.each do |state|
messages = format_conflict_messages(state.conflicts)
messages = [] #: Array[String]
sr_count = state.sr_conflicts_count
rr_count = state.rr_conflicts_count
messages << "#{sr_count} shift/reduce" if sr_count > 0
messages << "#{rr_count} reduce/reduce" if rr_count > 0

unless messages.empty?
has_conflict = true
Expand All @@ -26,19 +30,6 @@ def report_conflicts(io, states)

io << "\n\n" if has_conflict
end

# @rbs (Array[(Lrama::State::ShiftReduceConflict | Lrama::State::ReduceReduceConflict)] conflicts) -> Array[String]
def format_conflict_messages(conflicts)
conflict_types = {
shift_reduce: "shift/reduce",
reduce_reduce: "reduce/reduce"
}

conflict_types.keys.map do |type|
type_conflicts = conflicts.select { |c| c.type == type }
"#{type_conflicts.count} #{conflict_types[type]}" unless type_conflicts.empty?
end.compact
end
end
end
end
16 changes: 16 additions & 0 deletions lib/lrama/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,22 @@ def rr_conflicts
end
end

# @rbs () -> Integer
def sr_conflicts_count
return 0 if @conflicts.empty?
sr_conflicts.flat_map(&:symbols).uniq.count
end

# @rbs () -> Integer
def rr_conflicts_count
return 0 if @conflicts.empty?
# Pairs follow reduction order, so each reduction after the first counts once per token.
rr_conflicts.flat_map do |conflict|
# @type var conflict: ReduceReduceConflict
conflict.symbols.map { |symbol| [symbol, conflict.reduce2] }
end.uniq.count
end

# Clear information related to conflicts.
# IELR computation re-calculates conflicts and default reduction of states
# after LALR computation.
Expand Down
4 changes: 2 additions & 2 deletions lib/lrama/states.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ def la

# @rbs () -> Integer
def sr_conflicts_count
@sr_conflicts_count ||= @states.flat_map(&:sr_conflicts).count
@states.sum(&:sr_conflicts_count)
end

# @rbs () -> Integer
def rr_conflicts_count
@rr_conflicts_count ||= @states.flat_map(&:rr_conflicts).count
@states.sum(&:rr_conflicts_count)
end

# @rbs (Logger logger) -> void
Expand Down
3 changes: 0 additions & 3 deletions sig/generated/lrama/reporter/conflicts.rbs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions sig/generated/lrama/state.rbs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spec/ielr/grammar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
states.compute_ielr

expect(states.states.length).to eq(17)
expect(states.sr_conflicts_count).to eq(4)
expect(states.sr_conflicts_count).to eq(3)
expect(states.rr_conflicts_count).to eq(1)
expect { Lrama::Context.new(states) }.not_to raise_error
end
Expand Down
126 changes: 126 additions & 0 deletions spec/lrama/conflict_counts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# frozen_string_literal: true

RSpec.describe Lrama::States do
describe 'conflict counts' do
cases = [
['multiple reductions on one shifted token', <<~GRAMMAR, 1, 1, 2, 1],
start: 'a' 'b' | first 'b' | second 'b';
first: 'a';
second: 'a';
GRAMMAR
['multiple shifted tokens', <<~GRAMMAR, 2, 0, 2, 0],
start: 'a' 'b' | 'a' 'c' | first 'b' | first 'c';
first: 'a';
GRAMMAR
['multiple lookaheads for a reduction pair', <<~GRAMMAR, 0, 2, 0, 1],
start: first 'b' | first 'c' | second 'b' | second 'c';
first: 'a';
second: 'a';
GRAMMAR
['three reductions on one token', <<~GRAMMAR, 0, 2, 0, 3],
start: first 'b' | second 'b' | third 'b';
first: 'a';
second: 'a';
third: 'a';
GRAMMAR
['partially overlapping reduction lookaheads', <<~GRAMMAR, 0, 4, 0, 3],
start: first 'b' | first 'c' | first 'd'
| second 'b' | second 'c' | second 'd' | third 'c';
first: 'a';
second: 'a';
third: 'a';
GRAMMAR
['duplicate rules at the same source location', <<~GRAMMAR, 0, 2, 0, 3],
start: expr 'b';
expr: 'a' | 'a' | 'a';
GRAMMAR
['the same lookahead in different states', <<~GRAMMAR, 0, 2, 0, 2],
start: first 'b' | second 'b';
first: 'a' | 'c';
second: 'a' | 'c';
GRAMMAR
]

[:lalr, :ielr].each do |lr_type|
cases.each do |description, rules, sr_count, rr_count, sr_pairs, rr_pairs|
it "counts #{description} in #{lr_type}" do
grammar = Lrama::Parser.new("%define lr.type #{lr_type}\n%%\n#{rules}", 'conflict_counts.y').parse
grammar.prepare
states = described_class.new(grammar, Lrama::Tracer.new(Lrama::Logger.new))
states.compute
states.compute_ielr if lr_type == :ielr

expect(states.sr_conflicts_count).to eq(sr_count)
expect(states.rr_conflicts_count).to eq(rr_count)
expect(states.states.sum { |state| state.sr_conflicts.count }).to eq(sr_pairs)
expect(states.states.sum { |state| state.rr_conflicts.count }).to eq(rr_pairs)

io = StringIO.new
Lrama::Reporter::Conflicts.new.report(io, states)
expect(io.string.scan(/(\d+) shift\/reduce/).flatten.map(&:to_i).sum).to eq(sr_count)
expect(io.string.scan(/(\d+) reduce\/reduce/).flatten.map(&:to_i).sum).to eq(rr_count)

logger = Lrama::Logger.new
allow(logger).to receive(:warn)
Lrama::Warnings::Conflicts.new(logger, true).warn(states)
expect(logger).to have_received(:warn).with("shift/reduce conflicts: #{sr_count} found") if sr_count > 0
expect(logger).to have_received(:warn).with("reduce/reduce conflicts: #{rr_count} found") if rr_count > 0
end
end
end

it 'does not retain counts queried before computation' do
grammar = Lrama::Parser.new("%%\nstart: expr 'b' | 'a' 'b';\nexpr: 'a' | 'a';\n", 'conflict_counts.y').parse
grammar.prepare
states = described_class.new(grammar, Lrama::Tracer.new(Lrama::Logger.new))
expect(states.sr_conflicts_count).to eq(0)
expect(states.rr_conflicts_count).to eq(0)

states.compute

expect(states.sr_conflicts_count).to eq(1)
expect(states.rr_conflicts_count).to eq(1)
end

it 'reports the current counts after IELR removes LALR conflicts' do
grammar = Lrama::Parser.new(<<~GRAMMAR, 'conflict_counts.y').parse
%define lr.type ielr
%expect 0
%%
start: 'a' first 'd' | 'b' first 'e' | 'a' second 'e' | 'b' second 'd';
first: 'c';
second: 'c';
GRAMMAR
grammar.prepare
states = described_class.new(grammar, Lrama::Tracer.new(Lrama::Logger.new))
states.compute
expect(states.rr_conflicts_count).to eq(2)

states.compute_ielr

expect(states.rr_conflicts_count).to eq(0)
expect { states.validate!(Lrama::Logger.new) }.not_to raise_error
end

it 'uses lookahead counts in expected-conflict diagnostics' do
grammar = Lrama::Parser.new(<<~GRAMMAR, 'conflict_counts.y').parse
%expect 0
%%
start: 'a' 'b' | first 'b' | second 'b' | third 'b';
first: 'a';
second: 'a';
third: 'a';
GRAMMAR
grammar.prepare
states = described_class.new(grammar, Lrama::Tracer.new(Lrama::Logger.new))
states.compute
logger = Lrama::Logger.new
allow(logger).to receive(:error)

expect { states.validate!(logger) }.to raise_error(SystemExit)

expect(logger).to have_received(:error).with('shift/reduce conflicts: 1 found, 0 expected')
expect(logger).to have_received(:error).with('reduce/reduce conflicts: 2 found, 0 expected')
end
end
end
8 changes: 4 additions & 4 deletions spec/lrama/states_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@
10 '>'


State 1 conflicts: 2 shift/reduce, 1 reduce/reduce
State 1 conflicts: 1 shift/reduce, 1 reduce/reduce


Grammar
Expand Down Expand Up @@ -1428,7 +1428,7 @@ class go to state 5
state = states.states.find { |s| s.reduces.length == 2 }

expect(state.sr_conflicts.flat_map(&:symbols).map(&:name)).to eq(unresolved)
expect(states.sr_conflicts_count).to eq(unresolved.length)
expect(states.sr_conflicts_count).to eq(unresolved.uniq.length)
expect(state.rr_conflicts.count).to eq(rr_count)
end
end
Expand Down Expand Up @@ -3769,7 +3769,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS
// Prologue
%}

%expect 2
%expect 1
STR
end

Expand Down Expand Up @@ -3808,7 +3808,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS
allow(logger).to receive(:error)

expect{ states.validate!(logger) }.to raise_error(SystemExit)
expect(logger).to have_received(:error).with("shift/reduce conflicts: 2 found, 0 expected")
expect(logger).to have_received(:error).with("shift/reduce conflicts: 1 found, 0 expected")
expect(logger).to have_received(:error).with("reduce/reduce conflicts: 1 found, 0 expected")
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lrama/warnings/conflicts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS
logger = Lrama::Logger.new
allow(logger).to receive(:warn)
Lrama::Warnings.new(logger, true).warn(grammar, states)
expect(logger).to have_received(:warn).with("shift/reduce conflicts: 2 found")
expect(logger).to have_received(:warn).with("shift/reduce conflicts: 1 found")
expect(logger).to have_received(:warn).with("reduce/reduce conflicts: 1 found")
end
end
Expand All @@ -93,7 +93,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS
logger = Lrama::Logger.new
allow(logger).to receive(:warn)
Lrama::Warnings.new(logger, false).warn(grammar, states)
expect(logger).not_to have_received(:warn).with("shift/reduce conflicts: 2 found")
expect(logger).not_to have_received(:warn).with("shift/reduce conflicts: 1 found")
expect(logger).not_to have_received(:warn).with("reduce/reduce conflicts: 1 found")
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lrama/warnings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS
logger = Lrama::Logger.new
allow(logger).to receive(:warn)
Lrama::Warnings.new(logger, true).warn(grammar, states)
expect(logger).to have_received(:warn).with("shift/reduce conflicts: 2 found")
expect(logger).to have_received(:warn).with("shift/reduce conflicts: 1 found")
expect(logger).to have_received(:warn).with("reduce/reduce conflicts: 1 found")
end
end
Expand Down
Loading