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 d2b1d5c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/lrama/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def parse_by_option_parser(argv)
end
end

BISON_REPORTS = %w[states itemsets lookaheads solved counterexamples cex all none]
BISON_REPORTS = %w[terms states itemsets lookaheads solved counterexamples cex all none]
OTHER_REPORTS = %w[verbose]
NOT_SUPPORTED_REPORTS = %w[cex none]
VALID_REPORTS = BISON_REPORTS + OTHER_REPORTS - NOT_SUPPORTED_REPORTS
Expand Down
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, grammar: false, terms: false, states: false, itemsets: false, lookaheads: false, solved: false, counterexamples: false, verbose: false)
# TODO: Unused rules

report_unused_terms(io) if terms
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
4 changes: 2 additions & 2 deletions spec/lrama/option_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
-h, --help display this help and exit
Valid Reports:
states itemsets lookaheads solved counterexamples all verbose
terms states itemsets lookaheads solved counterexamples all verbose
Valid Traces:
none locations scan parse automaton bitsets closure grammar rules actions resource sets muscles tools m4-early m4 skeleton time ielr cex all
Expand All @@ -91,7 +91,7 @@
it "returns option hash all flags enabled" do
opts = option_parser.send(:validate_report, ["all"])
expect(opts).to eq({
grammar: true, states: true, itemsets: true,
grammar: true, terms: true, states: true, itemsets: true,
lookaheads: true, solved: true, counterexamples: true,
})
end
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, grammar: true, terms: 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 d2b1d5c

Please sign in to comment.