Class: Dhaka::ShiftReduceConflict

Inherits:
Conflict
  • Object
show all
Defined in:
lib/dhaka/parser/conflict.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods inherited from Conflict

#build_conflict_message, #initialize

Constructor Details

This class inherits a constructor from Dhaka::Conflict

Instance Method Details

#resolveObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dhaka/parser/conflict.rb', line 26

def resolve
  lines = [build_conflict_message]
  shift_precedence  = @lookahead.precedence
  reduce_precedence = @new_action.production.precedence
  if shift_precedence && reduce_precedence
    if shift_precedence > reduce_precedence
      lines << "Resolving with precedence. Choosing shift over reduce."
    elsif shift_precedence < reduce_precedence
      lines << "Resolving with precedence. Choosing reduce over shift."
      @state.actions[@lookahead.name] = @new_action
    else
      case shift_precedence.associativity
        when :left
          lines << "Resolving with left associativity. Choosing reduce over shift."
          @state.actions[@lookahead.name] = @new_action
        when :right
          lines << "Resolving with right associativity. Choosing shift over reduce."
        when :nonassoc
          lines << "Resolving with non-associativity. Eliminating action."
          @state.actions.delete(@lookahead.name)
      end
    end
  else
    lines << "No precedence rule. Choosing shift over reduce."
  end
  lines.join("\n")
end