Skip to content

Commit

Permalink
Add report_unused_terms private method
Browse files Browse the repository at this point in the history
  • Loading branch information
S-H-GAMELINKS committed Jun 8, 2024
1 parent 5bed7c9 commit 2745b77
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
42 changes: 40 additions & 2 deletions lib/lrama/states_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,53 @@ def report(io, **options)

private

def _report(io, grammar: false, states: false, itemsets: false, lookaheads: false, solved: false, counterexamples: false, verbose: false)
# TODO: Unused terms
def _report(io, term: false, grammar: false, states: false, itemsets: false, lookaheads: false, solved: false, counterexamples: false, verbose: false)
# TODO: Unused rules

report_unused_terms(io) if term
report_conflicts(io)
report_grammar(io) if grammar
report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
end

def report_unused_terms(io)
io << "Unused Terms\n\n"

results = []
terms = []
used_symbols = []

terms = @states.symbols.select(&:term?)

@states.states.select do |state|
state.shifts.map(&:next_sym)
end

@states.states.each do |state|
state.reduces.select do |reduce|
used_symbols << reduce.look_ahead.flatten if !reduce.look_ahead.nil?
end
end

@states.states.each do |state|
used_symbols << state.shifts.map(&:next_sym).select(&:term?).flatten
end

used_symbols = used_symbols.flatten

results = terms.select do |term|
!used_symbols.include?(term)
end

results.each_with_index do |term, index|
io << sprintf("%5d %s\n", index, term.id.s_value)
end

if !results.empty?
io << "\n\n"
end
end

def report_conflicts(io)
has_conflict = false

Expand Down
17 changes: 16 additions & 1 deletion spec/lrama/states_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,24 @@
states.compute

io = StringIO.new
states.reporter.report(io, grammar: true, states: true, itemsets: true, lookaheads: true)
states.reporter.report(io, term: true, grammar: true, states: true, itemsets: true, lookaheads: true)

expect(io.string).to eq(<<~STR)
Unused Terms
0 YYerror
1 YYUNDEF
2 '\\\\'
3 '\\13'
4 keyword_class2
5 tNUMBER
6 tPLUS
7 tMINUS
8 tEQ
9 tEQEQ
10 '>'
State 1 conflicts: 2 shift/reduce, 1 reduce/reduce
Expand Down

0 comments on commit 2745b77

Please sign in to comment.