From 5f962f2b385acc7fb4b6fbf5eba0d4df85631420 Mon Sep 17 00:00:00 2001 From: ydah Date: Mon, 10 Jun 2024 00:20:41 +0900 Subject: [PATCH 1/3] Improve `--report=terms` command Before: ``` Unused Terms 0 YYerror 2 YYUNDEF 3 '\\\\' 3 '\\13' 4 keyword_class2 5 tNUMBER 6 tPLUS 7 tMINUS 8 tEQ 9 tEQEQ 10 '>' ``` After: ``` 11 Unused Terms 0 YYerror 1 YYUNDEF 2 '\\\\' 3 '\\13' 4 keyword_class2 5 tNUMBER 6 tPLUS 7 tMINUS 8 tEQ 9 tEQEQ 10 '>' ``` --- lib/lrama/states_reporter.rb | 6 ++++-- spec/lrama/states_spec.rb | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/lrama/states_reporter.rb b/lib/lrama/states_reporter.rb index d05c48c9..a549246b 100644 --- a/lib/lrama/states_reporter.rb +++ b/lib/lrama/states_reporter.rb @@ -26,8 +26,6 @@ def _report(io, grammar: false, terms: false, states: false, itemsets: false, lo end def report_unused_terms(io) - io << "Unused Terms\n\n" - results = [] terms = [] used_symbols = [] @@ -54,6 +52,10 @@ def report_unused_terms(io) !used_symbols.include?(term) end + if !results.empty? + io << "#{results.count} Unused Terms\n\n" + end + results.each_with_index do |term, index| io << sprintf("%5d %s\n", index, term.id.s_value) end diff --git a/spec/lrama/states_spec.rb b/spec/lrama/states_spec.rb index 6b924e69..e848f529 100644 --- a/spec/lrama/states_spec.rb +++ b/spec/lrama/states_spec.rb @@ -18,7 +18,7 @@ states.reporter.report(io, grammar: true, terms: true, states: true, itemsets: true, lookaheads: true) expect(io.string).to eq(<<~STR) - Unused Terms + 11 Unused Terms 0 YYerror 1 YYUNDEF From e39e86e07edc908370d05d37e3426a8222fb65cf Mon Sep 17 00:00:00 2001 From: ydah Date: Mon, 10 Jun 2024 00:20:45 +0900 Subject: [PATCH 2/3] Remove unnecessary process --- lib/lrama/states_reporter.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/lrama/states_reporter.rb b/lib/lrama/states_reporter.rb index a549246b..c6022737 100644 --- a/lib/lrama/states_reporter.rb +++ b/lib/lrama/states_reporter.rb @@ -32,10 +32,6 @@ def report_unused_terms(io) 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? From fec1f158eac3f2562ff871129d3d845d9ffc33f4 Mon Sep 17 00:00:00 2001 From: ydah Date: Mon, 10 Jun 2024 00:21:33 +0900 Subject: [PATCH 3/3] Refactor `report_unused_terms` --- lib/lrama/states_reporter.rb | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/lib/lrama/states_reporter.rb b/lib/lrama/states_reporter.rb index c6022737..c23932d6 100644 --- a/lib/lrama/states_reporter.rb +++ b/lib/lrama/states_reporter.rb @@ -26,37 +26,25 @@ def _report(io, grammar: false, terms: false, states: false, itemsets: false, lo end def report_unused_terms(io) - results = [] - terms = [] - used_symbols = [] - - terms = @states.symbols.select(&:term?) - - @states.states.each do |state| - state.reduces.select do |reduce| - used_symbols << reduce.look_ahead.flatten if !reduce.look_ahead.nil? + look_aheads = @states.states.each do |state| + state.reduces.flat_map do |reduce| + reduce.look_ahead unless 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) + next_terms = @states.states.flat_map do |state| + state.shifts.map(&:next_sym).select(&:term?) end - if !results.empty? - io << "#{results.count} Unused Terms\n\n" + unused_symbols = @states.terms.select do |term| + !(look_aheads + next_terms).include?(term) end - results.each_with_index do |term, index| - io << sprintf("%5d %s\n", index, term.id.s_value) - end - - if !results.empty? + unless unused_symbols.empty? + io << "#{unused_symbols.count} Unused Terms\n\n" + unused_symbols.each_with_index do |term, index| + io << sprintf("%5d %s\n", index, term.id.s_value) + end io << "\n\n" end end