From 6df65cf57e93b550de5abe41a1ec44c3715bc24a Mon Sep 17 00:00:00 2001 From: ydah Date: Thu, 12 Nov 2026 16:14:18 +0900 Subject: [PATCH] Fix missing IELR splits for nested rules --- lib/lrama/state.rb | 19 +++++++---- .../fixtures/ielr/nested_rule_contributions.y | 33 +++++++++++++++++++ spec/ielr/grammar_spec.rb | 2 +- spec/lrama/integration_spec.rb | 17 ++++++++++ spec/lrama/state_spec.rb | 11 +++++++ spec/lrama/states_spec.rb | 18 ++++++++++ 6 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 spec/fixtures/ielr/nested_rule_contributions.y diff --git a/lib/lrama/state.rb b/lib/lrama/state.rb index fc9497c6..d9e8eacc 100644 --- a/lib/lrama/state.rb +++ b/lib/lrama/state.rb @@ -454,14 +454,19 @@ def annotate_predecessor(predecessor) cs = 0 predecessor.lane_items[self].each {|pred_kernel, kernel| next unless contributions[kernel_indices.fetch(kernel)] == 1 - pred_index = predecessor.kernel_indices[pred_kernel] - next unless pred_index - c = - (kernel.position > 1 && predecessor.item_lookahead_set[pred_kernel][annotation.token.number] == 1) || - (kernel.position == 1 && predecessor.lhs_contributions(kernel.lhs, annotation.token)[pred_kernel]) - cs |= (1 << pred_index) if c - propagates = true if c + + if kernel.position == 1 + # Enclosing rules can contribute through kernels outside this transition lane. + predecessor.lhs_contributions(kernel.lhs, annotation.token).each do |contributing_kernel, contributes| + cs |= (1 << predecessor.kernel_indices.fetch(contributing_kernel)) if contributes + end + else + pred_index = predecessor.kernel_indices[pred_kernel] + next unless pred_index + cs |= (1 << pred_index) if predecessor.item_lookahead_set[pred_kernel][annotation.token.number] == 1 + end } + propagates = true if cs != 0 contribution_matrix[action] = cs end } diff --git a/spec/fixtures/ielr/nested_rule_contributions.y b/spec/fixtures/ielr/nested_rule_contributions.y new file mode 100644 index 00000000..e705753b --- /dev/null +++ b/spec/fixtures/ielr/nested_rule_contributions.y @@ -0,0 +1,33 @@ +%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' left_tail; +right: 'x' right_tail; +left_tail: 'y'; +right_tail: '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 51321cfb..d9c40a96 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(5) + expect(states.sr_conflicts_count).to eq(4) 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 a540e637..01c75e44 100644 --- a/spec/lrama/integration_spec.rb +++ b/spec/lrama/integration_spec.rb @@ -105,6 +105,23 @@ def generate_object(grammar_file_path, c_path, obj_path, command_args: []) end end + it "accepts all paths through nested IELR rules" do + Dir.mktmpdir do |directory| + c_path = File.join(directory, "nested#{file_extension}") + obj_path = File.join(directory, "nested") + generate_object(fixture_path("ielr/nested_rule_contributions.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 "accepts all paths through shared multi-symbol IELR reductions" do Dir.mktmpdir do |directory| c_path = File.join(directory, "multiple_predecessors#{file_extension}") diff --git a/spec/lrama/state_spec.rb b/spec/lrama/state_spec.rb index 35863b12..3390d572 100644 --- a/spec/lrama/state_spec.rb +++ b/spec/lrama/state_spec.rb @@ -509,6 +509,17 @@ def annotation(contributions, from: origin, on: token, with: actions) expect(predecessor.annotation_list.map(&:state)).to eq([origin, state]) end + it "propagates first-position contributions through kernels outside the transition lane" do + predecessor = described_class.new(0, token, [last_item, next_item]) + predecessor.lane_items[state] = [[item, next_item]] + allow(predecessor).to receive(:lhs_contributions).with(rule.lhs, token).and_return({ last_item => true, next_item => true }) + state.annotation_list << annotation(0b01) + + expect(state.annotate_predecessor(predecessor)).to be(true) + + expect(predecessor.annotation_list.first.contribution_matrix).to eq({ actions.first => 0b11 }) + end + it "shares the unconditional contribution check across actions" do state.annotation_list << Lrama::State::InadequacyAnnotation.new(origin, token, actions + [other_action], { actions.first => 0b01, other_action => 0b01, diff --git a/spec/lrama/states_spec.rb b/spec/lrama/states_spec.rb index c080145f..6e6031be 100644 --- a/spec/lrama/states_spec.rb +++ b/spec/lrama/states_spec.rb @@ -213,6 +213,24 @@ end end + describe 'IELR lookaheads through nested rules' do + ["'y'", "'y' 'z'", "'y' 'z' 'w'"].each do |rhs| + it "splits nested reductions with the shared tail #{rhs} without conflicts" do + source = File.read(fixture_path("ielr/nested_rule_contributions.y")).gsub("'y'", rhs) + grammar = Lrama::Parser.new(source, "nested.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"