Class: Ibex::LALR::IELR::Annotator

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

Overview

Finds grammar-relative inadequacies and traces their possible manifestations through predecessor states.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grammar, states, transitions, items, goto_follows, resolver: nil) ⇒ Annotator

Returns a new instance of Annotator.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ibex/lalr/ielr/annotator.rb', line 28

def initialize(grammar, states, transitions, items, goto_follows, resolver: nil)
  @grammar = grammar
  @states = states
  @transitions = transitions
  @items = items
  @goto_follows = goto_follows
  @resolver = resolver || ConflictResolver.new(grammar)
  @kernel_cores = states.map { |state| kernel_items(state) }
  @item_lookaheads = ItemLookaheads.new(
    grammar, goto_follows, @kernel_cores, goto_follows.predecessors,
    start_states: (0...grammar.starts.length).to_a
  )
  @annotation_lists = Array.new(states.length) { [] }
  @keys = Array.new(states.length) { Set.new }
  @inadequacies = Array.new(states.length) { [] }
  @next_id = 0
  @split_stable_discarded = 0
end

Instance Attribute Details

#annotation_listsObject (readonly)

: Array[Array[Annotation]]



21
22
23
# File 'lib/ibex/lalr/ielr/annotator.rb', line 21

def annotation_lists
  @annotation_lists
end

#inadequaciesObject (readonly)

: Array[Array[Inadequacy]]



22
23
24
# File 'lib/ibex/lalr/ielr/annotator.rb', line 22

def inadequacies
  @inadequacies
end

#item_lookaheadsObject (readonly)

: ItemLookaheads



23
24
25
# File 'lib/ibex/lalr/ielr/annotator.rb', line 23

def item_lookaheads
  @item_lookaheads
end

#split_stable_discardedObject (readonly)

: Integer



24
25
26
# File 'lib/ibex/lalr/ielr/annotator.rb', line 24

def split_stable_discarded
  @split_stable_discarded
end

Instance Method Details

#buildObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ibex/lalr/ielr/annotator.rb', line 48

def build
  @states.each_index do |state_id|
    @inadequacies[state_id] = build_inadequacies(state_id)
    @inadequacies.fetch(state_id).each do |inadequacy|
      register?(state_id, annotate_manifestation(state_id, inadequacy))
    end
  end
  worklist = @annotation_lists.each_with_index.flat_map do |annotations, state_id|
    annotations.map { |annotation| [state_id, annotation] }
  end
  cursor = 0
  while cursor < worklist.length
    state_id, annotation = worklist.fetch(cursor)
    cursor += 1
    @goto_follows.predecessors.fetch(state_id).each do |previous|
      derived = annotate_predecessor(previous, state_id, annotation)
      worklist << [previous, derived] if register?(previous, derived)
    end
  end
  @annotation_lists
end