Class: Ibex::LALR::IELR::ItemLookaheads

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

Overview

Lazily derives the lookahead of a kernel item from predecessor states and goto-follow sets. Memoisation is important: predecessor paths can share exponentially many suffixes in a real grammar.

Instance Method Summary collapse

Constructor Details

#initialize(grammar, goto_follows, kernel_cores, predecessors, start_states: nil) ⇒ ItemLookaheads

Returns a new instance of ItemLookaheads.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ibex/lalr/ielr/item_lookaheads.rb', line 19

def initialize(grammar, goto_follows, kernel_cores, predecessors, start_states: nil)
  @grammar = grammar
  @goto_follows = goto_follows
  @kernel_cores = kernel_cores
  @predecessors = predecessors
  @start_states = start_states || []
  @index = {}
  kernel_cores.each_with_index do |cores, state_id|
    cores.each_with_index { |core, kernel_index| @index[[state_id, core]] = kernel_index }
  end
  @memo = {}
end

Instance Method Details

#fetch(state_id, kernel_index) ⇒ Object



33
34
35
36
37
38
# File 'lib/ibex/lalr/ielr/item_lookaheads.rb', line 33

def fetch(state_id, kernel_index)
  key = [state_id, kernel_index]
  return @memo.fetch(key) if @memo.key?(key)

  @memo[key] = compute(state_id, kernel_index)
end

#sizeObject



41
42
43
# File 'lib/ibex/lalr/ielr/item_lookaheads.rb', line 41

def size
  @memo.length
end