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
2 changes: 1 addition & 1 deletion lib/lrama/states.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/ielr/selected_reduction_lookaheads.y
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
%define api.pure full
%expect 0
%locations
%code provides {
static int yylex(YYSTYPE *, YYLTYPE *);
Expand Down
13 changes: 13 additions & 0 deletions spec/lrama/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
27 changes: 27 additions & 0 deletions spec/lrama/states_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading