Class: Lrama::Reporter::Terms

Inherits:
Object
  • Object
show all
Defined in:
lib/lrama/reporter/terms.rb

Instance Method Summary collapse

Constructor Details

#initialize(terms: false, **_) ⇒ Terms

Returns a new instance of Terms.



8
9
10
# File 'lib/lrama/reporter/terms.rb', line 8

def initialize(terms: false, **_)
  @terms = terms
end

Instance Method Details

#report(io, states) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lrama/reporter/terms.rb', line 13

def report(io, states)
  return unless @terms

  look_aheads = states.states.each do |state|
    state.reduces.flat_map do |reduce|
      reduce.look_ahead unless reduce.look_ahead.nil?
    end
  end

  next_terms = states.states.flat_map do |state|
    state.term_transitions.map {|shift| shift.next_sym }
  end

  unused_symbols = states.terms.reject do |term|
    (look_aheads + next_terms).include?(term)
  end

  io << states.terms.count << " Terms\n\n"

  io << states.nterms.count << " Non-Terminals\n\n"

  unless unused_symbols.empty?
    io << "#{unused_symbols.count} Unused Terms\n\n"
    unused_symbols.each_with_index do |term, index|
      io << sprintf("%5d %s", index, term.id.s_value) << "\n"
    end
    io << "\n\n"
  end
end