From 04e71705dbafb046e1f484be2d9a46485d392dfb Mon Sep 17 00:00:00 2001 From: ydah Date: Tue, 24 Nov 2026 13:28:22 +0900 Subject: [PATCH] Compute initial IELR lookaheads in item dependency order --- lib/lrama/state.rb | 62 ++++++++++---------- sig/generated/lrama/state.rbs | 6 +- spec/fixtures/ielr/initial_item_lookaheads.y | 30 ++++++++++ spec/lrama/integration_spec.rb | 13 ++++ spec/lrama/states_spec.rb | 20 +++++++ 5 files changed, 97 insertions(+), 34 deletions(-) create mode 100644 spec/fixtures/ielr/initial_item_lookaheads.y diff --git a/lib/lrama/state.rb b/lib/lrama/state.rb index 220d900b..3c305177 100644 --- a/lib/lrama/state.rb +++ b/lib/lrama/state.rb @@ -21,6 +21,7 @@ class State # type lookahead_set = Hash[Item, Bitmap::bitmap] # type dominant_contributions = Hash[InadequacyAnnotation, Array[InadequacyAnnotation::action]?] # type item_pair = [State, Item] + # type item_lookahead_dependencies = Hash[Integer, Array[[State, Item, Array[item_pair]]]] # type item_origins_cache = Hash[item_pair, Array[item_pair]] # type annotation_key = [Integer, Grammar::Symbol, Array[InadequacyAnnotation::action], Hash[InadequacyAnnotation::action, Bitmap::bitmap?]] # type annotation_indexes = Hash[Integer, Set[annotation_key]] @@ -542,46 +543,43 @@ def lhs_contributions(sym, token) def item_lookahead_set return @item_lookahead_set if @item_lookahead_set - pending = [[self, false]] #: Array[[State, bool]] - while (entry = pending.pop) - state, dependencies_computed = entry - state.compute_item_lookahead_set(pending, dependencies_computed) + pending = [self] #: Array[State] + dependencies = {} #: item_lookahead_dependencies + while (state = pending.pop) + state.collect_item_lookahead_dependencies(pending, dependencies) + end + + # Predecessor states can form cycles, but the predecessor item's dot is one position earlier. + dependencies.keys.sort.each do |position| + dependencies.fetch(position).each do |state, item, predecessors| + state.item_lookahead_set[item] = predecessors.reduce(0) do |bits, (predecessor, previous_item)| + bits | predecessor.item_lookahead_set.fetch(previous_item) + end + end end @item_lookahead_set end - # @rbs (Array[[State, bool]] pending, bool dependencies_computed) -> void - def compute_item_lookahead_set(pending, dependencies_computed) - unless dependencies_computed - return if @item_lookahead_set + # @rbs (Array[State] pending, item_lookahead_dependencies dependencies) -> void + def collect_item_lookahead_dependencies(pending, dependencies) + return if @item_lookahead_set - @item_lookahead_set = kernels.map {|k| [k, 0] }.to_h.compare_by_identity - pending << [self, true] - # Depth-first order matters when a predecessor refers to an unfinished state. - kernels.reverse_each do |kernel| - next if kernel.lhs.accept_symbol? || kernel.position <= 1 - predecessors_with_item(kernel).reverse_each do |state, _| - pending << [state, false] + @item_lookahead_set = {} #: lookahead_set + @item_lookahead_set.compare_by_identity + kernels.each do |item| + if item.lhs.accept_symbol? + @item_lookahead_set[item] = 0 + elsif item.position == 1 + @item_lookahead_set[item] = @predecessors.reduce(0) do |lookaheads, predecessor| + goto = predecessor.transitions_by_symbol_number.fetch(item.lhs.number) #: Action::Goto + lookaheads | predecessor.goto_follows.fetch(goto) end + else + predecessors = predecessors_with_item(item) + (dependencies[item.position] ||= []) << [self, item, predecessors] + predecessors.each { |predecessor, _| pending << predecessor } end - return end - - @item_lookahead_set = kernels.map {|kernel| - value = - if kernel.lhs.accept_symbol? - 0 - elsif kernel.position > 1 - 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 - @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 end # @rbs (lookahead_set k) -> void diff --git a/sig/generated/lrama/state.rbs b/sig/generated/lrama/state.rbs index 71f8eb7e..a280c7d2 100644 --- a/sig/generated/lrama/state.rbs +++ b/sig/generated/lrama/state.rbs @@ -12,6 +12,8 @@ module Lrama type item_pair = [ State, Item ] + type item_lookahead_dependencies = Hash[Integer, Array[[ State, Item, Array[item_pair] ]]] + type item_origins_cache = Hash[item_pair, Array[item_pair]] type annotation_key = [ Integer, Grammar::Symbol, Array[InadequacyAnnotation::action], Hash[InadequacyAnnotation::action, Bitmap::bitmap?] ] @@ -230,8 +232,8 @@ module Lrama # @rbs () -> lookahead_set def item_lookahead_set: () -> lookahead_set - # @rbs (Array[[State, bool]] pending, bool dependencies_computed) -> void - def compute_item_lookahead_set: (Array[[ State, bool ]] pending, bool dependencies_computed) -> void + # @rbs (Array[State] pending, item_lookahead_dependencies dependencies) -> void + def collect_item_lookahead_dependencies: (Array[State] pending, item_lookahead_dependencies dependencies) -> void # @rbs (lookahead_set k) -> void def item_lookahead_set=: (lookahead_set k) -> void diff --git a/spec/fixtures/ielr/initial_item_lookaheads.y b/spec/fixtures/ielr/initial_item_lookaheads.y new file mode 100644 index 00000000..a71ec24f --- /dev/null +++ b/spec/fixtures/ielr/initial_item_lookaheads.y @@ -0,0 +1,30 @@ +%define lr.type ielr +%define api.pure full +%locations +%code provides { + static int yylex(YYSTYPE *, YYLTYPE *); + static void yyerror(YYLTYPE *, const char *); +} +%% +expr: 'd' | term term 'c' | 'c' item item term; +item: 'a' | term 'b'; +term: item expr | expr; +%% +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/integration_spec.rb b/spec/lrama/integration_spec.rb index 0332f514..83d99fcc 100644 --- a/spec/lrama/integration_spec.rb +++ b/spec/lrama/integration_spec.rb @@ -133,6 +133,19 @@ def generate_object(grammar_file_path, c_path, obj_path, command_args: []) end end + it "propagates initial item lookaheads through recursive IELR rules" do + Dir.mktmpdir do |directory| + c_path = File.join(directory, "initial_lookaheads#{file_extension}") + obj_path = File.join(directory, "initial_lookaheads") + generate_object(fixture_path("ielr/initial_item_lookaheads.y"), c_path, obj_path) + + {"caad" => 0, "cdbad" => 0, "d" => 0, "caa" => 1, "cdba" => 1}.each do |input, expected_status| + _, error, status = Open3.capture3(obj_path, input) + expect(status.exitstatus).to eq(expected_status), "#{input}: #{error}" + end + end + end + it "preserves distinct IELR annotation contributions through nullable recursive rules" do Dir.mktmpdir do |directory| c_path = File.join(directory, "distinct_annotations#{file_extension}") diff --git a/spec/lrama/states_spec.rb b/spec/lrama/states_spec.rb index c341db91..95ac8804 100644 --- a/spec/lrama/states_spec.rb +++ b/spec/lrama/states_spec.rb @@ -214,6 +214,26 @@ end describe 'IELR lookaheads through nested rules' do + it 'propagates lookaheads through every dot position of a recursive rule' do + source = "%%\nlist: %empty | list 'x' list;\n" + grammar = Lrama::Parser.new(source, "recursive.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) + + after_x = states.states.find { |state| state.accessing_symbol.name == "'x'" } + after_list = after_x.nterm_transitions.first.to_state + completed = after_list.kernels.find(&:end_of_rule?) + expected = Lrama::Bitmap.from_array([grammar.eof_symbol.number, after_x.accessing_symbol.number]) + + expect(after_x.item_lookahead_set.fetch(after_x.kernels.first)).to eq(expected) + expect(after_list.item_lookahead_set.fetch(completed)).to eq(expected) + end + ["'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)