Class: Lrama::State
- Inherits:
-
Object
- Object
- Lrama::State
- Defined in:
- lib/lrama/states.rb
Defined Under Namespace
Classes: Conflict, Reduce, ResolvedConflict, Shift
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
-
#initialize(id, accessing_symbol, kernels) ⇒ State
constructor
A new instance of State.
- #non_default_reduces ⇒ Object
-
#nterm_transitions ⇒ Object
Returns array of [nterm, 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 [term, next_state].
-
#transition(sym) ⇒ Object
Move to next state by sym.
Constructor Details
#initialize(id, accessing_symbol, kernels) ⇒ State
Returns a new instance of State.
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/lrama/states.rb', line 80 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.
76 77 78 |
# File 'lib/lrama/states.rb', line 76 def accessing_symbol @accessing_symbol end |
#closure ⇒ Object
Returns the value of attribute closure.
76 77 78 |
# File 'lib/lrama/states.rb', line 76 def closure @closure end |
#conflicts ⇒ Object (readonly)
Returns the value of attribute conflicts.
76 77 78 |
# File 'lib/lrama/states.rb', line 76 def conflicts @conflicts end |
#default_reduction_rule ⇒ Object
Returns the value of attribute default_reduction_rule.
76 77 78 |
# File 'lib/lrama/states.rb', line 76 def default_reduction_rule @default_reduction_rule end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
76 77 78 |
# File 'lib/lrama/states.rb', line 76 def id @id end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
76 77 78 |
# File 'lib/lrama/states.rb', line 76 def items @items end |
#kernels ⇒ Object (readonly)
Returns the value of attribute kernels.
76 77 78 |
# File 'lib/lrama/states.rb', line 76 def kernels @kernels end |
#reduces ⇒ Object
Returns the value of attribute reduces.
78 79 80 |
# File 'lib/lrama/states.rb', line 78 def reduces @reduces end |
#resolved_conflicts ⇒ Object (readonly)
Returns the value of attribute resolved_conflicts.
76 77 78 |
# File 'lib/lrama/states.rb', line 76 def resolved_conflicts @resolved_conflicts end |
#shifts ⇒ Object
Returns the value of attribute shifts.
78 79 80 |
# File 'lib/lrama/states.rb', line 78 def shifts @shifts end |
Instance Method Details
#compute_shifts_reduces ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/lrama/states.rb', line 104 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
198 199 200 201 202 |
# File 'lib/lrama/states.rb', line 198 def find_reduce_by_item!(item) reduces.find do |r| r.item == item end || (raise "reduce is not found. #{item}, #{state}") end |
#non_default_reduces ⇒ Object
98 99 100 101 102 |
# File 'lib/lrama/states.rb', line 98 def non_default_reduces reduces.select do |reduce| reduce.rule != @default_reduction_rule end end |
#nterm_transitions ⇒ Object
Returns array of [nterm, next_state]
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/lrama/states.rb', line 142 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
220 221 222 223 224 |
# File 'lib/lrama/states.rb', line 220 def rr_conflicts @conflicts.select do |conflict| conflict.type == :reduce_reduce end end |
#selected_term_transitions ⇒ Object
171 172 173 174 175 |
# File 'lib/lrama/states.rb', line 171 def selected_term_transitions term_transitions.select do |shift, next_state| !shift.not_selected end end |
#set_items_to_state(items, next_state) ⇒ Object
128 129 130 |
# File 'lib/lrama/states.rb', line 128 def set_items_to_state(items, next_state) @items_to_state[items] = next_state end |
#set_look_ahead(rule, look_ahead) ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/lrama/states.rb', line 133 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
214 215 216 217 218 |
# File 'lib/lrama/states.rb', line 214 def sr_conflicts @conflicts.select do |conflict| conflict.type == :shift_reduce end end |
#term_transitions ⇒ Object
Returns array of [term, next_state]
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/lrama/states.rb', line 157 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
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/lrama/states.rb', line 178 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 |