From 08aa82a288f5417dc889c4a87026f8a7b5f99fc6 Mon Sep 17 00:00:00 2001 From: ydah Date: Tue, 17 Nov 2026 20:17:31 +0900 Subject: [PATCH] Count conflicts by lookahead token --- lib/lrama/reporter/conflicts.rb | 19 +--- lib/lrama/state.rb | 16 +++ lib/lrama/states.rb | 4 +- sig/generated/lrama/reporter/conflicts.rbs | 3 - sig/generated/lrama/state.rbs | 6 + spec/ielr/grammar_spec.rb | 2 +- spec/lrama/conflict_counts_spec.rb | 126 +++++++++++++++++++++ spec/lrama/states_spec.rb | 8 +- spec/lrama/warnings/conflicts_spec.rb | 4 +- spec/lrama/warnings_spec.rb | 2 +- 10 files changed, 163 insertions(+), 27 deletions(-) create mode 100644 spec/lrama/conflict_counts_spec.rb diff --git a/lib/lrama/reporter/conflicts.rb b/lib/lrama/reporter/conflicts.rb index f4d8c604c..940aeadfa 100644 --- a/lib/lrama/reporter/conflicts.rb +++ b/lib/lrama/reporter/conflicts.rb @@ -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 @@ -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 diff --git a/lib/lrama/state.rb b/lib/lrama/state.rb index 1786d18a6..200055e23 100644 --- a/lib/lrama/state.rb +++ b/lib/lrama/state.rb @@ -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. diff --git a/lib/lrama/states.rb b/lib/lrama/states.rb index 238abc08d..aef1146c0 100644 --- a/lib/lrama/states.rb +++ b/lib/lrama/states.rb @@ -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 diff --git a/sig/generated/lrama/reporter/conflicts.rbs b/sig/generated/lrama/reporter/conflicts.rbs index e7daedeef..8ee513214 100644 --- a/sig/generated/lrama/reporter/conflicts.rbs +++ b/sig/generated/lrama/reporter/conflicts.rbs @@ -10,9 +10,6 @@ module Lrama # @rbs (IO io, Lrama::States states) -> void def report_conflicts: (IO io, Lrama::States states) -> void - - # @rbs (Array[(Lrama::State::ShiftReduceConflict | Lrama::State::ReduceReduceConflict)] conflicts) -> Array[String] - def format_conflict_messages: (Array[Lrama::State::ShiftReduceConflict | Lrama::State::ReduceReduceConflict] conflicts) -> Array[String] end end end diff --git a/sig/generated/lrama/state.rbs b/sig/generated/lrama/state.rbs index aa968ecf5..1f657fce5 100644 --- a/sig/generated/lrama/state.rbs +++ b/sig/generated/lrama/state.rbs @@ -162,6 +162,12 @@ module Lrama # @rbs () -> Array[conflict] def rr_conflicts: () -> Array[conflict] + # @rbs () -> Integer + def sr_conflicts_count: () -> Integer + + # @rbs () -> Integer + def rr_conflicts_count: () -> Integer + # Clear information related to conflicts. # IELR computation re-calculates conflicts and default reduction of states # after LALR computation. diff --git a/spec/ielr/grammar_spec.rb b/spec/ielr/grammar_spec.rb index d9c40a969..7e92bd007 100644 --- a/spec/ielr/grammar_spec.rb +++ b/spec/ielr/grammar_spec.rb @@ -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 diff --git a/spec/lrama/conflict_counts_spec.rb b/spec/lrama/conflict_counts_spec.rb new file mode 100644 index 000000000..05658e77d --- /dev/null +++ b/spec/lrama/conflict_counts_spec.rb @@ -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 diff --git a/spec/lrama/states_spec.rb b/spec/lrama/states_spec.rb index cddf92f61..a3b3d1a0c 100644 --- a/spec/lrama/states_spec.rb +++ b/spec/lrama/states_spec.rb @@ -289,7 +289,7 @@ 10 '>' - State 1 conflicts: 2 shift/reduce, 1 reduce/reduce + State 1 conflicts: 1 shift/reduce, 1 reduce/reduce Grammar @@ -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 @@ -3769,7 +3769,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS // Prologue %} - %expect 2 + %expect 1 STR end @@ -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 diff --git a/spec/lrama/warnings/conflicts_spec.rb b/spec/lrama/warnings/conflicts_spec.rb index 73a445662..85c2c820d 100644 --- a/spec/lrama/warnings/conflicts_spec.rb +++ b/spec/lrama/warnings/conflicts_spec.rb @@ -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 @@ -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 diff --git a/spec/lrama/warnings_spec.rb b/spec/lrama/warnings_spec.rb index 91b63854a..f3c198415 100644 --- a/spec/lrama/warnings_spec.rb +++ b/spec/lrama/warnings_spec.rb @@ -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