Skip to content

Commit

Permalink
Merge pull request #439 from S-H-GAMELINKS/add-unused-term-report
Browse files Browse the repository at this point in the history
Add support `--report=terms` option
  • Loading branch information
ydah authored Jun 9, 2024
2 parents 40781a9 + 28b1eb3 commit e087c5a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/lrama/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def parse_by_option_parser(argv)
end

BISON_REPORTS = %w[states itemsets lookaheads solved counterexamples cex all none]
OTHER_REPORTS = %w[verbose]
OTHER_REPORTS = %w[terms 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
2 changes: 1 addition & 1 deletion 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
states itemsets lookaheads solved counterexamples all terms 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 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 e087c5a

Please sign in to comment.