From 999ea11d4ae5d7fbae7bec892160c23058573327 Mon Sep 17 00:00:00 2001 From: ydah Date: Tue, 10 Nov 2026 23:08:24 +0900 Subject: [PATCH] Fix missing IELR splits for multi-symbol reductions --- lib/lrama/state.rb | 7 ++-- .../ielr/multiple_predecessor_lookaheads.y | 31 ++++++++++++++ spec/ielr/grammar_spec.rb | 6 +-- spec/lrama/integration_spec.rb | 17 ++++++++ spec/lrama/states_spec.rb | 40 +++++++++++++++++++ 5 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 spec/fixtures/ielr/multiple_predecessor_lookaheads.y diff --git a/lib/lrama/state.rb b/lib/lrama/state.rb index 1cd1192f..93caaeec 100644 --- a/lib/lrama/state.rb +++ b/lib/lrama/state.rb @@ -532,9 +532,10 @@ def item_lookahead_set prev_items = predecessors_with_item(kernel) prev_items.map {|st, i| st.item_lookahead_set[i] }.reduce(0) {|acc, bitmap| acc | bitmap } elsif kernel.position == 1 - prev_state = @predecessors.find {|p| p.transitions_by_symbol_number.key?(kernel.lhs.number) } - goto = prev_state.transitions_by_symbol_number[kernel.lhs.number] #: Action::Goto? - prev_state.goto_follows[goto] + @predecessors.reduce(0) do |lookaheads, prev_state| + goto = prev_state.transitions_by_symbol_number.fetch(kernel.lhs.number) #: Action::Goto + lookaheads | prev_state.goto_follows.fetch(goto) + end end [kernel, value] }.to_h.compare_by_identity diff --git a/spec/fixtures/ielr/multiple_predecessor_lookaheads.y b/spec/fixtures/ielr/multiple_predecessor_lookaheads.y new file mode 100644 index 00000000..bd56c3fb --- /dev/null +++ b/spec/fixtures/ielr/multiple_predecessor_lookaheads.y @@ -0,0 +1,31 @@ +%define lr.type ielr +%define api.pure full +%locations +%code provides { + static int yylex(YYSTYPE *, YYLTYPE *); + static void yyerror(YYLTYPE *, const char *); +} +%% +start: 'a' left 'd' | 'a' right 'e' + | 'b' left 'e' | 'b' right 'd'; +left: 'x' 'y'; +right: 'x' 'y'; +%% +static const char *input; + +static int yylex(YYSTYPE *value, YYLTYPE *location) { + (void)value; + (void)location; + return *input ? *input++ : 0; +} + +static void yyerror(YYLTYPE *location, const char *message) { + (void)location; + (void)message; +} + +int main(int argc, char **argv) { + if (argc != 2) return 2; + input = argv[1]; + return yyparse(); +} diff --git a/spec/ielr/grammar_spec.rb b/spec/ielr/grammar_spec.rb index 68fc5f59..51321cfb 100644 --- a/spec/ielr/grammar_spec.rb +++ b/spec/ielr/grammar_spec.rb @@ -64,7 +64,7 @@ expect(states.rr_conflicts_count).to eq(0) end - it "preserves always contributions without splitting equivalent states" do + it "builds parser tables for a grammar with always contributions" do path = fixture_path("ielr/always_contributions.y") grammar = Lrama::Parser.new(File.read(path), path).parse grammar.prepare @@ -74,8 +74,8 @@ states.compute_ielr - expect(states.states.length).to eq(15) - expect(states.sr_conflicts_count).to eq(4) + expect(states.states.length).to eq(17) + expect(states.sr_conflicts_count).to eq(5) expect(states.rr_conflicts_count).to eq(1) expect { Lrama::Context.new(states) }.not_to raise_error end diff --git a/spec/lrama/integration_spec.rb b/spec/lrama/integration_spec.rb index 9f885b14..c1973f7f 100644 --- a/spec/lrama/integration_spec.rb +++ b/spec/lrama/integration_spec.rb @@ -75,6 +75,23 @@ def generate_object(grammar_file_path, c_path, obj_path, command_args: []) include IntegrationHelper + it "accepts all paths through shared multi-symbol IELR reductions" do + Dir.mktmpdir do |directory| + c_path = File.join(directory, "multiple_predecessors#{file_extension}") + obj_path = File.join(directory, "multiple_predecessors") + generate_object(fixture_path("ielr/multiple_predecessor_lookaheads.y"), c_path, obj_path) + + %w[axyd axye bxyd bxye].each do |input| + _, error, status = Open3.capture3(obj_path, input) + expect(status.success?).to be(true), "#{input}: #{error}" + end + ["", "axy", "axyde", "cxyd", "axxd", "xyde"].each do |input| + _, _, status = Open3.capture3(obj_path, input) + expect(status.success?).to be(false), input + end + end + end + it "uses structured tags in declarations and explicit references" do c_path = Dir.tmpdir + "/structured_tags#{file_extension}" obj_path = Dir.tmpdir + "/structured_tags" diff --git a/spec/lrama/states_spec.rb b/spec/lrama/states_spec.rb index 0a8bc042..4dc2d2dc 100644 --- a/spec/lrama/states_spec.rb +++ b/spec/lrama/states_spec.rb @@ -159,6 +159,46 @@ end end + describe 'IELR lookaheads from multiple predecessors' do + let(:source) { File.read(fixture_path("ielr/multiple_predecessor_lookaheads.y")) } + + [false, true].each do |reverse| + it "unions every predecessor's goto follows with reverse order #{reverse}" do + grammar = Lrama::Parser.new(source, "lookaheads.y").parse + grammar.prepare + states = described_class.new(grammar, Lrama::Tracer.new(Lrama::Logger.new)) + states.compute + states.send(:compute_predecessors) + states.send(:compute_follow_kernel_items) + always_follows = states.send(:compute_always_follows) + states.send(:compute_goto_follows, always_follows) + target = states.states.find { |state| state.kernels.any? { |item| item.lhs.name == "left" && item.position == 1 } } + target.predecessors.reverse! if reverse + + lookaheads = target.kernels.map do |item| + Lrama::Bitmap.to_array(target.item_lookahead_set.fetch(item)).map { |number| grammar.find_symbol_by_number!(number).name } + end + + expect(lookaheads).to eq([["'d'", "'e'"], ["'d'", "'e'"]]) + end + end + + ["'x'", "'x' 'y'", "'x' 'y' 'z'"].each do |rhs| + it "splits reductions with the shared RHS #{rhs} without conflicts" do + grammar = Lrama::Parser.new(source.gsub("'x' 'y'", rhs), "lookaheads.y").parse + grammar.prepare + states = described_class.new(grammar, Lrama::Tracer.new(Lrama::Logger.new)) + states.compute + + states.compute_ielr + + expect(states.sr_conflicts_count).to eq(0) + expect(states.rr_conflicts_count).to eq(0) + expect { Lrama::Context.new(states) }.not_to raise_error + end + end + end + describe '#compute' do it "basic" do path = "common/basic.y"