From b3078e1a1f0833564b25d6d15bb6ad8fe0fc3ac7 Mon Sep 17 00:00:00 2001 From: Junichi Kobayashi Date: Thu, 26 Sep 2024 02:36:21 +0900 Subject: [PATCH] Rewind the version --- lib/lrama/state.rb | 56 +++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/lib/lrama/state.rb b/lib/lrama/state.rb index a14de3eb..ab0ca2d0 100644 --- a/lib/lrama/state.rb +++ b/lib/lrama/state.rb @@ -159,7 +159,7 @@ def rr_conflicts end def propagate_lookaheads(next_state) - next_state.kernels.to_h {|item| + next_state.kernels.map {|item| lookahead_sets = if item.position == 1 goto_follow_set(item.lhs) @@ -169,7 +169,7 @@ def propagate_lookaheads(next_state) end [item, lookahead_sets & next_state.lookahead_set_filters[item]] - } + }.to_h end def lookaheads_recomputed @@ -186,21 +186,21 @@ def compatible_lookahead?(filtered_lookahead) end def lookahead_set_filters - kernels.to_h {|kernel| + kernels.map {|kernel| [kernel, - @lalr_isocore.annotation_list.filter_map {|token, actions| - filter = token.term? && actions.any? {|action, contributions| + @lalr_isocore.annotation_list.select {|token, actions| + token.term? && actions.any? {|action, contributions| !contributions.nil? && contributions.key?(kernel) && contributions[kernel] } - filter && token - }] - } + }.map {|token, _| token } + ] + }.to_h end def dominant_contribution(token, actions, lookaheads) - a = actions.filter_map {|action, contributions| - action if contributions.nil? || contributions.any? {|item, contributed| contributed && lookaheads[item].include?(token) } - } + a = actions.select {|action, contributions| + contributions.nil? || contributions.any? {|item, contributed| contributed && lookaheads[item].include?(token) } + }.map {|action, _| action } return nil if a.empty? a.reject {|action| if action.is_a?(State::Shift) @@ -214,18 +214,18 @@ def dominant_contribution(token, actions, lookaheads) def inadequacy_list return @inadequacy_list if @inadequacy_list - shift_contributions = shifts.to_h {|shift| + shift_contributions = shifts.map {|shift| [shift.next_sym, [shift]] - } + }.to_h reduce_contributions = reduces.map {|reduce| - (reduce.look_ahead || []).to_h {|sym| + (reduce.look_ahead || []).map {|sym| [sym, [reduce]] - } + }.to_h }.reduce(Hash.new([])) {|hash, cont| - hash.merge(cont) {|_, a, b| a.union(b) } + hash.merge(cont) {|_, a, b| a | b } } - list = shift_contributions.merge(reduce_contributions) {|_, a, b| a.union(b) } + list = shift_contributions.merge(reduce_contributions) {|_, a, b| a | b } @inadequacy_list = list.select {|token, actions| token.term? && actions.size > 1 } end @@ -249,18 +249,18 @@ def annotation_list def annotate_manifestation inadequacy_list.transform_values {|actions| - actions.to_h {|action| + actions.map {|action| if action.is_a?(Shift) [action, nil] elsif action.is_a?(Reduce) if action.rule.empty_rule? [action, lhs_contributions(action.rule.lhs, inadequacy_list.key(actions))] else - contributions = kernels.to_h {|kernel| [kernel, kernel.rule == action.rule && kernel.end_of_rule?] } + contributions = kernels.map {|kernel| [kernel, kernel.rule == action.rule && kernel.end_of_rule?] }.to_h [action, contributions] end end - } + }.to_h } end @@ -275,14 +275,14 @@ def annotate_predecessor(predecessor) if lhs_adequacy next nil else - predecessor.kernels.to_h {|pred_k| + predecessor.kernels.map {|pred_k| [pred_k, kernels.any? {|k| inadequacy[k] && ( pred_k.predecessor_item_of?(k) && predecessor.item_lookahead_set[pred_k].include?(token) || k.position == 1 && predecessor.lhs_contributions(k.lhs, token)[pred_k] ) }] - } + }.to_h end } } @@ -293,7 +293,7 @@ def lhs_contributions(sym, token) if always_follows(shift, next_state).include?(token) nil else - kernels.to_h {|kernel| [kernel, follow_kernel_items(shift, next_state, kernel) && item_lookahead_set[kernel].include?(token)] } + kernels.map {|kernel| [kernel, follow_kernel_items(shift, next_state, kernel) && item_lookahead_set[kernel].include?(token)] }.to_h end end @@ -310,7 +310,7 @@ def follow_kernel_items(shift, next_state, kernel) def item_lookahead_set return @item_lookahead_set if @item_lookahead_set - kernels.to_h {|item| + kernels.map {|item| value = if item.lhs.accept_symbol? [] @@ -323,7 +323,7 @@ def item_lookahead_set prev_state.goto_follows(shift, next_state) end [item, value] - } + }.to_h end def item_lookahead_set=(k) @@ -340,10 +340,6 @@ def predecessors_with_item(item) result end - def next_terms - shifts.filter_map {|shift| shift.next_sym.term? && shift.next_sym } - end - def append_predecessor(prev_state) @predecessors << prev_state @predecessors.uniq! @@ -398,7 +394,7 @@ def successor_dependencies(shift, next_state) def predecessor_dependencies(shift, next_state) state_items = [] - @kernels.filter {|kernel| + @kernels.select {|kernel| kernel.next_sym == shift.next_sym && kernel.symbols_after_transition.all?(&:nullable) }.each do |item| queue = predecessors_with_item(item)