From 552c40e4e0436e8db8322343d8e246101173d157 Mon Sep 17 00:00:00 2001 From: ydah Date: Wed, 11 Nov 2026 17:11:11 +0900 Subject: [PATCH] Resolve reduce/reduce conflicts in grammar rule order --- lib/lrama/context.rb | 4 +-- spec/fixtures/ielr/reduce_reduce_rule_order.y | 29 +++++++++++++++++++ spec/lrama/context_spec.rb | 23 +++++++++++++++ spec/lrama/integration_spec.rb | 16 ++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/ielr/reduce_reduce_rule_order.y diff --git a/lib/lrama/context.rb b/lib/lrama/context.rb index 7369eab0..4c768148 100644 --- a/lib/lrama/context.rb +++ b/lib/lrama/context.rb @@ -220,8 +220,8 @@ def compute_yydefact actions = Array.new(@states.terms.count, 0) if state.reduces.map(&:selected_look_ahead).any? {|la| !la.empty? } - # Iterate reduces with reverse order so that first rule is used. - state.reduces.reverse_each do |reduce| + # Visit later rules first so earlier rules win reduce/reduce conflicts. + state.reduces.sort_by {|reduce| reduce.rule.id }.reverse_each do |reduce| reduce.look_ahead.each do |term| actions[term.number] = rule_id_to_action_number(reduce.rule.id) end diff --git a/spec/fixtures/ielr/reduce_reduce_rule_order.y b/spec/fixtures/ielr/reduce_reduce_rule_order.y new file mode 100644 index 00000000..1b94ba95 --- /dev/null +++ b/spec/fixtures/ielr/reduce_reduce_rule_order.y @@ -0,0 +1,29 @@ +%define api.pure full +%locations +%code provides { + static int yylex(YYSTYPE *, YYLTYPE *); + static void yyerror(YYLTYPE *, const char *); +} +%% +start: 'a' empty 'b' 'x' | direct 'b' 'y'; +empty: %empty; +direct: 'a'; +%% +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/lrama/context_spec.rb b/spec/lrama/context_spec.rb index 260662fe..58a6a8c3 100644 --- a/spec/lrama/context_spec.rb +++ b/spec/lrama/context_spec.rb @@ -216,6 +216,29 @@ describe "compute_yydefact" do [:lalr, :ielr].each do |lr_type| + ["empty: %empty;\ndirect: 'a';", "direct: 'a';\nempty: %empty;"].each do |rules| + it "resolves #{lr_type} R/R conflicts by rule order with #{rules.lines.first.strip} first" do + grammar = Lrama::Parser.new(<<~GRAMMAR, "reduce_order.y").parse + %define lr.type #{lr_type} + %% + start: 'a' empty 'b' 'x' | direct 'b' 'y'; + #{rules} + GRAMMAR + grammar.prepare + states = Lrama::States.new(grammar, Lrama::Tracer.new(Lrama::Logger.new)) + states.compute + states.compute_ielr if lr_type == :ielr + context = Lrama::Context.new(states) + + expect(states.rr_conflicts_count).to eq(1) + state = states.states.find { |s| s.reduces.length == 2 } + token = grammar.find_symbol_by_s_value!("'b'") + index = context.yypact[state.id] + token.number + expect(context.yycheck[index]).to eq(token.number) + expect(context.yytable[index]).to eq(-(grammar.rules[3].id + 1)) + end + end + ["'a' | 'a'", "|"].each do |alternatives| it "builds #{lr_type} tables for duplicate alternatives #{alternatives.inspect}" do grammar = Lrama::Parser.new(<<~GRAMMAR, "duplicate.y").parse diff --git a/spec/lrama/integration_spec.rb b/spec/lrama/integration_spec.rb index c1973f7f..6f4e4eda 100644 --- a/spec/lrama/integration_spec.rb +++ b/spec/lrama/integration_spec.rb @@ -75,6 +75,22 @@ def generate_object(grammar_file_path, c_path, obj_path, command_args: []) include IntegrationHelper + [:lalr, :ielr].each do |lr_type| + it "prefers the earlier empty rule in #{lr_type} R/R conflicts" do + Dir.mktmpdir do |directory| + c_path = File.join(directory, "reduce_order#{file_extension}") + obj_path = File.join(directory, "reduce_order") + generate_object(fixture_path("ielr/reduce_reduce_rule_order.y"), c_path, obj_path, + command_args: ["-Dlr.type=#{lr_type}"]) + + {"abx" => true, "aby" => false}.each do |input, accepted| + _, error, status = Open3.capture3(obj_path, input) + expect(status.success?).to be(accepted), "#{input}: #{error}" + end + 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}")