Skip to content

Commit

Permalink
Merge pull request #441 from ydah/support-rules-option
Browse files Browse the repository at this point in the history
Add support `--report=rules` option
  • Loading branch information
ydah authored Jun 10, 2024
2 parents 13140bc + db5b291 commit 9a75458
Show file tree
Hide file tree
Showing 7 changed files with 54 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 @@ -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[terms verbose]
OTHER_REPORTS = %w[rules terms verbose]
NOT_SUPPORTED_REPORTS = %w[cex none]
VALID_REPORTS = BISON_REPORTS + OTHER_REPORTS - NOT_SUPPORTED_REPORTS

Expand Down
21 changes: 18 additions & 3 deletions lib/lrama/states_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ def report(io, **options)

private

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

def _report(io, grammar: false, rules: false, terms: false, states: false, itemsets: false, lookaheads: false, solved: false, counterexamples: false, verbose: false)
report_unused_rules(io) if rules
report_unused_terms(io) if terms
report_conflicts(io)
report_grammar(io) if grammar
Expand Down Expand Up @@ -49,6 +48,22 @@ def report_unused_terms(io)
end
end

def report_unused_rules(io)
used_rules = @states.rules.flat_map(&:rhs)

unused_rules = @states.rules.map(&:lhs).select do |rule|
!used_rules.include?(rule) && rule.token_id != 0
end

unless unused_rules.empty?
io << "#{unused_rules.count} Unused Rules\n\n"
unused_rules.each_with_index do |rule, index|
io << sprintf("%5d %s\n", index, rule.display_name)
end
io << "\n\n"
end
end

def report_conflicts(io)
has_conflict = false

Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/common/basic.y
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ string_2: string '+'
string: tSTRING
;

unused: tNUMBER
;

%%

// Epilogue
4 changes: 4 additions & 0 deletions spec/lrama/lexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@
expect(lexer.next_token).to eq([':', ':'])
expect(lexer.next_token).to eq([:IDENTIFIER, token_class::Ident.new(s_value: 'tSTRING')])
expect(lexer.next_token).to eq([';', ';'])
expect(lexer.next_token).to eq([:IDENT_COLON, token_class::Ident.new(s_value: 'unused')])
expect(lexer.next_token).to eq([':', ':'])
expect(lexer.next_token).to eq([:IDENTIFIER, token_class::Ident.new(s_value: 'tNUMBER')])
expect(lexer.next_token).to eq([';', ';'])
expect(lexer.next_token).to eq(['%%', '%%'])
expect(lexer.next_token).to eq(nil)
end
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 terms verbose
states itemsets lookaheads solved counterexamples all rules 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
19 changes: 19 additions & 0 deletions spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
Sym.new(id: T::Ident.new(s_value: "string_1"), alias_name: nil, number: 28, tag: nil, term: false, token_id: 9, nullable: false, precedence: nil, printer: nil),
Sym.new(id: T::Ident.new(s_value: "string_2"), alias_name: nil, number: 29, tag: nil, term: false, token_id: 10, nullable: false, precedence: nil, printer: nil),
Sym.new(id: T::Ident.new(s_value: "string"), alias_name: nil, number: 30, tag: nil, term: false, token_id: 11, nullable: false, precedence: nil, printer: nil),
Sym.new(id: T::Ident.new(s_value: "unused"), alias_name: nil, number: 31, tag: nil, term: false, token_id: 12, nullable: false, precedence: nil, printer: nil),
])
expect(grammar.types).to eq([Type.new(id: T::Ident.new(s_value: "class"), tag: T::Tag.new(s_value: "<i>"))])
_rules = grammar.rule_builders.map {|b| [b.lhs, (b.rhs + [b.precedence_sym, b.user_code]).compact, b.line] }
Expand Down Expand Up @@ -214,6 +215,13 @@
],
81,
],
[
T::Ident.new(s_value: "unused"),
[
T::Ident.new(s_value: "tNUMBER")
],
84,
],
])
expect(grammar.rules).to eq([
Rule.new(
Expand Down Expand Up @@ -415,6 +423,17 @@
precedence_sym: grammar.find_symbol_by_s_value!("tSTRING"),
lineno: 81,
),
Rule.new(
id: 17,
lhs: grammar.find_symbol_by_s_value!("unused"),
rhs: [
grammar.find_symbol_by_s_value!("tNUMBER"),
],
token_code: nil,
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("tNUMBER"),
lineno: 84,
),
])
end

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

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

expect(io.string).to eq(<<~STR)
1 Unused Rules
0 unused
11 Unused Terms
0 YYerror
Expand Down Expand Up @@ -69,6 +74,8 @@
16 string: tSTRING
17 unused: tNUMBER
State 0
Expand Down

0 comments on commit 9a75458

Please sign in to comment.