From 423983958b33fa79fd8fdf12e9c0f371623223d1 Mon Sep 17 00:00:00 2001 From: ydah Date: Wed, 11 Nov 2026 22:44:38 +0900 Subject: [PATCH] Keep discarded reductions out of parser tables --- lib/lrama/context.rb | 2 +- .../ielr/selected_reduction_lookaheads.y | 32 +++++++++++++++++++ spec/lrama/context_spec.rb | 22 +++++++++++++ spec/lrama/integration_spec.rb | 14 ++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/ielr/selected_reduction_lookaheads.y diff --git a/lib/lrama/context.rb b/lib/lrama/context.rb index 4c768148..4a85580e 100644 --- a/lib/lrama/context.rb +++ b/lib/lrama/context.rb @@ -222,7 +222,7 @@ def compute_yydefact if state.reduces.map(&:selected_look_ahead).any? {|la| !la.empty? } # 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| + reduce.selected_look_ahead.each do |term| actions[term.number] = rule_id_to_action_number(reduce.rule.id) end end diff --git a/spec/fixtures/ielr/selected_reduction_lookaheads.y b/spec/fixtures/ielr/selected_reduction_lookaheads.y new file mode 100644 index 00000000..bdcc776e --- /dev/null +++ b/spec/fixtures/ielr/selected_reduction_lookaheads.y @@ -0,0 +1,32 @@ +%define api.pure full +%locations +%code provides { + static int yylex(YYSTYPE *, YYLTYPE *); + static void yyerror(YYLTYPE *, const char *); +} +%precedence LOW +%precedence 'b' +%precedence HIGH +%% +start: 'a' empty 'b' 'x' | direct 'b' 'y' | 'a' 'b' 'z' | 'a' empty 'c'; +empty: %empty %prec LOW; +direct: 'a' %prec HIGH; +%% +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 58a6a8c3..5cc1d58c 100644 --- a/spec/lrama/context_spec.rb +++ b/spec/lrama/context_spec.rb @@ -216,6 +216,28 @@ describe "compute_yydefact" do [:lalr, :ielr].each do |lr_type| + it "omits #{lr_type} reductions already discarded by precedence" do + source = "%define lr.type #{lr_type}\n" + File.read(fixture_path("ielr/selected_reduction_lookaheads.y")) + grammar = Lrama::Parser.new(source, "precedence.y").parse + grammar.prepare + states = Lrama::States.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 } + empty = state.reduces.find { |reduce| reduce.rule.rhs.empty? } + direct = state.reduces.find { |reduce| !reduce.rule.rhs.empty? } + token = grammar.find_symbol_by_s_value!("'b'") + + expect(empty.look_ahead).to include(token) + expect(empty.selected_look_ahead).not_to include(token) + expect(empty.selected_look_ahead).to include(grammar.find_symbol_by_s_value!("'c'")) + expect(direct.selected_look_ahead).to include(token) + context = Lrama::Context.new(states) + index = context.yypact[state.id] + token.number + expect(context.yycheck[index]).to eq(token.number) + expect(context.yytable[index]).to eq(-(direct.rule.id + 1)) + end + ["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 diff --git a/spec/lrama/integration_spec.rb b/spec/lrama/integration_spec.rb index 6f4e4eda..a540e637 100644 --- a/spec/lrama/integration_spec.rb +++ b/spec/lrama/integration_spec.rb @@ -76,6 +76,20 @@ def generate_object(grammar_file_path, c_path, obj_path, command_args: []) include IntegrationHelper [:lalr, :ielr].each do |lr_type| + it "honors discarded reduction lookaheads in #{lr_type} parsers" do + Dir.mktmpdir do |directory| + c_path = File.join(directory, "selected_lookaheads#{file_extension}") + obj_path = File.join(directory, "selected_lookaheads") + generate_object(fixture_path("ielr/selected_reduction_lookaheads.y"), c_path, obj_path, + command_args: ["-Dlr.type=#{lr_type}"]) + + {"aby" => true, "ac" => true, "abx" => false, "abz" => false}.each do |input, accepted| + _, error, status = Open3.capture3(obj_path, input) + expect(status.success?).to be(accepted), "#{input}: #{error}" + end + end + end + 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}")