Class: Ibex::LALR::ConflictResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/lalr/conflict.rb

Overview

Applies yacc-compatible precedence and ordering rules to action candidates.

Instance Method Summary collapse

Constructor Details

#initialize(grammar) ⇒ ConflictResolver

Returns a new instance of ConflictResolver.



8
9
10
# File 'lib/ibex/lalr/conflict.rb', line 8

def initialize(grammar)
  @grammar = grammar
end

Instance Method Details

#resolve(token_id, candidates) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ibex/lalr/conflict.rb', line 13

def resolve(token_id, candidates)
  actions = candidates.uniq
  return [actions.first, Array.new(0)] if actions.length <= 1

  accept = actions.find { |action| action[:type] == :accept }
  return [accept, Array.new(0)] if accept

  shift = actions.find { |action| action[:type] == :shift } #: IR::shift_action?
  reductions = reduction_actions(actions)
  chosen_reduce, conflicts = resolve_reductions(token_id, reductions)
  all_conflicts = widen_conflicts(conflicts)
  return [chosen_reduce, all_conflicts] unless shift
  return [shift, all_conflicts] unless chosen_reduce

  chosen, conflict = resolve_shift_reduce(token_id, shift, chosen_reduce)
  [chosen, all_conflicts << conflict]
end