Skip to content

Commit

Permalink
Rewind the version
Browse files Browse the repository at this point in the history
  • Loading branch information
junk0612 committed Sep 25, 2024
1 parent 0e60548 commit b3078e1
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions lib/lrama/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
}
}
Expand All @@ -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

Expand All @@ -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?
[]
Expand All @@ -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)
Expand All @@ -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!
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b3078e1

Please sign in to comment.