Class: Lrama::State
- Inherits:
-
Object
- Object
- Lrama::State
- Defined in:
- lib/lrama/state.rb,
lib/lrama/state/shift.rb,
lib/lrama/state/reduce.rb,
lib/lrama/state/resolved_conflict.rb,
lib/lrama/state/shift_reduce_conflict.rb,
lib/lrama/state/reduce_reduce_conflict.rb
Defined Under Namespace
Classes: Reduce, ReduceReduceConflict, ResolvedConflict, Shift, ShiftReduceConflict
Instance Attribute Summary collapse
-
#accessing_symbol ⇒ Object
readonly
Returns the value of attribute accessing_symbol.
-
#closure ⇒ Object
Returns the value of attribute closure.
-
#conflicts ⇒ Object
readonly
Returns the value of attribute conflicts.
-
#default_reduction_rule ⇒ Object
Returns the value of attribute default_reduction_rule.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#kernels ⇒ Object
readonly
Returns the value of attribute kernels.
-
#reduces ⇒ Object
Returns the value of attribute reduces.
-
#resolved_conflicts ⇒ Object
readonly
Returns the value of attribute resolved_conflicts.
-
#shifts ⇒ Object
Returns the value of attribute shifts.
Instance Method Summary collapse
- #compute_shifts_reduces ⇒ Object
- #find_reduce_by_item!(item) ⇒ Object
- #has_conflicts? ⇒ Boolean
-
#initialize(id, accessing_symbol, kernels) ⇒ State
constructor
A new instance of State.
- #non_default_reduces ⇒ Object
-
#nterm_transitions ⇒ Object
Returns array of [Shift, next_state].
- #rr_conflicts ⇒ Object
- #selected_term_transitions ⇒ Object
- #set_items_to_state(items, next_state) ⇒ Object
- #set_look_ahead(rule, look_ahead) ⇒ Object
- #sr_conflicts ⇒ Object
-
#term_transitions ⇒ Object
Returns array of [Shift, next_state].
-
#transition(sym) ⇒ Object
Move to next state by sym.
- #transitions ⇒ Object
Constructor Details
#initialize(id, accessing_symbol, kernels) ⇒ State
Returns a new instance of State.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/lrama/state.rb', line 13 def initialize(id, accessing_symbol, kernels) @id = id @accessing_symbol = accessing_symbol @kernels = kernels.freeze @items = @kernels # Manage relationships between items to state # to resolve next state @items_to_state = {} @conflicts = [] @resolved_conflicts = [] @default_reduction_rule = nil end |
Instance Attribute Details
#accessing_symbol ⇒ Object (readonly)
Returns the value of attribute accessing_symbol.
9 10 11 |
# File 'lib/lrama/state.rb', line 9 def accessing_symbol @accessing_symbol end |
#closure ⇒ Object
Returns the value of attribute closure.
9 10 11 |
# File 'lib/lrama/state.rb', line 9 def closure @closure end |
#conflicts ⇒ Object (readonly)
Returns the value of attribute conflicts.
9 10 11 |
# File 'lib/lrama/state.rb', line 9 def conflicts @conflicts end |
#default_reduction_rule ⇒ Object
Returns the value of attribute default_reduction_rule.
9 10 11 |
# File 'lib/lrama/state.rb', line 9 def default_reduction_rule @default_reduction_rule end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
9 10 11 |
# File 'lib/lrama/state.rb', line 9 def id @id end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
9 10 11 |
# File 'lib/lrama/state.rb', line 9 def items @items end |
#kernels ⇒ Object (readonly)
Returns the value of attribute kernels.
9 10 11 |
# File 'lib/lrama/state.rb', line 9 def kernels @kernels end |
#reduces ⇒ Object
Returns the value of attribute reduces.
11 12 13 |
# File 'lib/lrama/state.rb', line 11 def reduces @reduces end |
#resolved_conflicts ⇒ Object (readonly)
Returns the value of attribute resolved_conflicts.
9 10 11 |
# File 'lib/lrama/state.rb', line 9 def resolved_conflicts @resolved_conflicts end |
#shifts ⇒ Object
Returns the value of attribute shifts.
11 12 13 |
# File 'lib/lrama/state.rb', line 11 def shifts @shifts end |
Instance Method Details
#compute_shifts_reduces ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/lrama/state.rb', line 37 def compute_shifts_reduces _shifts = {} reduces = [] items.each do |item| # TODO: Consider what should be pushed if item.end_of_rule? reduces << Reduce.new(item) else key = item.next_sym _shifts[key] ||= [] _shifts[key] << item.new_by_next_position end end # It seems Bison 3.8.2 iterates transitions order by symbol number shifts = _shifts.sort_by do |next_sym, new_items| next_sym.number end.map do |next_sym, new_items| Shift.new(next_sym, new_items.flatten) end self.shifts = shifts.freeze self.reduces = reduces.freeze end |
#find_reduce_by_item!(item) ⇒ Object
134 135 136 137 138 |
# File 'lib/lrama/state.rb', line 134 def find_reduce_by_item!(item) reduces.find do |r| r.item == item end || (raise "reduce is not found. #{item}") end |
#has_conflicts? ⇒ Boolean
150 151 152 |
# File 'lib/lrama/state.rb', line 150 def has_conflicts? !@conflicts.empty? end |
#non_default_reduces ⇒ Object
31 32 33 34 35 |
# File 'lib/lrama/state.rb', line 31 def non_default_reduces reduces.select do |reduce| reduce.rule != @default_reduction_rule end end |
#nterm_transitions ⇒ Object
Returns array of [Shift, next_state]
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/lrama/state.rb', line 74 def nterm_transitions return @nterm_transitions if @nterm_transitions @nterm_transitions = [] shifts.each do |shift| next if shift.next_sym.term? @nterm_transitions << [shift, @items_to_state[shift.next_items]] end @nterm_transitions end |
#rr_conflicts ⇒ Object
160 161 162 163 164 |
# File 'lib/lrama/state.rb', line 160 def rr_conflicts @conflicts.select do |conflict| conflict.type == :reduce_reduce end end |
#selected_term_transitions ⇒ Object
107 108 109 110 111 |
# File 'lib/lrama/state.rb', line 107 def selected_term_transitions term_transitions.select do |shift, next_state| !shift.not_selected end end |
#set_items_to_state(items, next_state) ⇒ Object
61 62 63 |
# File 'lib/lrama/state.rb', line 61 def set_items_to_state(items, next_state) @items_to_state[items] = next_state end |
#set_look_ahead(rule, look_ahead) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/lrama/state.rb', line 65 def set_look_ahead(rule, look_ahead) reduce = reduces.find do |r| r.rule == rule end reduce.look_ahead = look_ahead end |
#sr_conflicts ⇒ Object
154 155 156 157 158 |
# File 'lib/lrama/state.rb', line 154 def sr_conflicts @conflicts.select do |conflict| conflict.type == :shift_reduce end end |
#term_transitions ⇒ Object
Returns array of [Shift, next_state]
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/lrama/state.rb', line 89 def term_transitions return @term_transitions if @term_transitions @term_transitions = [] shifts.each do |shift| next if shift.next_sym.nterm? @term_transitions << [shift, @items_to_state[shift.next_items]] end @term_transitions end |
#transition(sym) ⇒ Object
Move to next state by sym
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/lrama/state.rb', line 114 def transition(sym) result = nil if sym.term? term_transitions.each do |shift, next_state| term = shift.next_sym result = next_state if term == sym end else nterm_transitions.each do |shift, next_state| nterm = shift.next_sym result = next_state if nterm == sym end end raise "Can not transit by #{sym} #{self}" if result.nil? result end |
#transitions ⇒ Object
103 104 105 |
# File 'lib/lrama/state.rb', line 103 def transitions term_transitions + nterm_transitions end |