diff --git a/lib/lrama/states.rb b/lib/lrama/states.rb index 20edcb2d..b7d42e0a 100644 --- a/lib/lrama/states.rb +++ b/lib/lrama/states.rb @@ -601,7 +601,7 @@ def compute_reduce_reduce_conflicts state.reduces.combination(2) do |reduce1, reduce2| next if reduce1.look_ahead.nil? || reduce2.look_ahead.nil? - intersection = reduce1.look_ahead & reduce2.look_ahead + intersection = reduce1.selected_look_ahead & reduce2.selected_look_ahead unless intersection.empty? state.conflicts << State::ReduceReduceConflict.new(symbols: intersection, reduce1: reduce1, reduce2: reduce2) diff --git a/spec/fixtures/ielr/selected_reduction_lookaheads.y b/spec/fixtures/ielr/selected_reduction_lookaheads.y index bdcc776e..1272fe8d 100644 --- a/spec/fixtures/ielr/selected_reduction_lookaheads.y +++ b/spec/fixtures/ielr/selected_reduction_lookaheads.y @@ -1,4 +1,5 @@ %define api.pure full +%expect 0 %locations %code provides { static int yylex(YYSTYPE *, YYLTYPE *); diff --git a/spec/lrama/command_spec.rb b/spec/lrama/command_spec.rb index ccb42474..49ebbdde 100644 --- a/spec/lrama/command_spec.rb +++ b/spec/lrama/command_spec.rb @@ -127,6 +127,19 @@ end end + [:lalr, :ielr].each do |lr_type| + it "honors %expect 0 after discarding #{lr_type} reduction lookaheads" do + Dir.mktmpdir do |dir| + outfile = File.join(dir, "parse.c") + command = Lrama::Command.new(["-o", outfile, "-Dlr.type=#{lr_type}", + fixture_path("ielr/selected_reduction_lookaheads.y")]) + + expect { command.run }.not_to raise_error + expect(File).to exist(outfile) + end + end + end + it "generates a parser and all reports for duplicate rules on the same line" do Dir.mktmpdir do |dir| outfile = File.join(dir, "parse.c") diff --git a/spec/lrama/states_spec.rb b/spec/lrama/states_spec.rb index b9b8841f..a197dffe 100644 --- a/spec/lrama/states_spec.rb +++ b/spec/lrama/states_spec.rb @@ -1437,6 +1437,33 @@ class go to state 5 expect(state.term_transitions.find { |shift| shift.next_sym == token }.not_selected).to be(true) expect(reduces.first.selected_look_ahead.include?(token)).to be(resolution == :reduce) expect(reduces.last.selected_look_ahead).to include(token) + expect(state.rr_conflicts.count).to eq(resolution == :error ? 0 : 1) + end + end + + [false, true].each do |shared_token| + it "reports #{shared_token ? 'only the remaining' : 'no'} #{lr_type} R/R conflict after precedence resolution" do + grammar = Lrama::Parser.new(<<~GRAMMAR, "selected_conflicts.y").parse + %define lr.type #{lr_type} + %precedence LOW + %precedence 'b' + %precedence HIGH + %% + start: 'a' empty 'b' 'x' | direct 'b' 'y' | 'a' 'b' 'z' | 'a' empty 'c' + #{"| direct 'c'" if shared_token}; + empty: %empty %prec LOW; + direct: 'a' %prec HIGH; + GRAMMAR + grammar.prepare + states = described_class.new(grammar, Lrama::Tracer.new(Lrama::Logger.new)) + states.compute + states.compute_ielr if lr_type == :ielr + state = states.states.find { |s| s.reduces.length == 2 } + token = grammar.find_symbol_by_s_value!("'b'") + + expect(state.reduces.all? { |reduce| reduce.look_ahead.include?(token) }).to be(true) + expect(state.rr_conflicts.flat_map(&:symbols).map(&:name)).to eq(shared_token ? ["'c'"] : []) + expect(states.rr_conflicts_count).to eq(shared_token ? 1 : 0) end end end