diff --git a/lib/lrama/option_parser.rb b/lib/lrama/option_parser.rb index 2612823fe..f1ba90a13 100644 --- a/lib/lrama/option_parser.rb +++ b/lib/lrama/option_parser.rb @@ -95,6 +95,7 @@ def parse_by_option_parser(argv) o.on_tail ' lookaheads explicitly associate lookahead tokens to items' o.on_tail ' solved describe shift/reduce conflicts solving' o.on_tail ' counterexamples, cex generate conflict counterexamples' + o.on_tail ' ielr show IELR isocores with lookahead diffs and split reasons' o.on_tail ' rules list unused rules' o.on_tail ' terms list unused terminals' o.on_tail ' verbose report detailed internal state and analysis results' @@ -139,7 +140,7 @@ def parse_by_option_parser(argv) end ALIASED_REPORTS = { cex: :counterexamples }.freeze #: Hash[Symbol, Symbol] - VALID_REPORTS = %i[states itemsets lookaheads solved counterexamples rules terms verbose].freeze #: Array[Symbol] + VALID_REPORTS = %i[states itemsets lookaheads solved counterexamples ielr rules terms verbose].freeze #: Array[Symbol] # @rbs (Array[String]) -> Hash[Symbol, bool] def validate_report(report) diff --git a/lib/lrama/reporter.rb b/lib/lrama/reporter.rb index ed25cc7f8..6a452d4b9 100644 --- a/lib/lrama/reporter.rb +++ b/lib/lrama/reporter.rb @@ -3,6 +3,7 @@ require_relative 'reporter/conflicts' require_relative 'reporter/grammar' +require_relative 'reporter/ielr' require_relative 'reporter/precedences' require_relative 'reporter/profile' require_relative 'reporter/rules' @@ -21,6 +22,7 @@ def initialize(**options) @conflicts = Conflicts.new @precedences = Precedences.new @grammar = Grammar.new(**options) + @ielr = Ielr.new(**options) @states = States.new(**options) end @@ -32,6 +34,7 @@ def report(io, states) report_duration(:report_conflicts) { @conflicts.report(io, states) } report_duration(:report_precedences) { @precedences.report(io, states) } report_duration(:report_grammar) { @grammar.report(io, states) } + report_duration(:report_ielr) { @ielr.report(io, states) } report_duration(:report_states) { @states.report(io, states, ielr: states.ielr_defined?) } end end diff --git a/lib/lrama/reporter/ielr.rb b/lib/lrama/reporter/ielr.rb new file mode 100644 index 000000000..c64244cbc --- /dev/null +++ b/lib/lrama/reporter/ielr.rb @@ -0,0 +1,147 @@ +# rbs_inline: enabled +# frozen_string_literal: true + +module Lrama + class Reporter + class Ielr + # @rbs (?ielr: bool, **bool _) -> void + def initialize(ielr: false, **_) + @enabled = ielr + end + + # @rbs (IO io, Lrama::States states) -> void + def report(io, states) + return unless @enabled && states.ielr_defined? + + lalr_isocores = lalr_isocores_with_splits(states) + return if lalr_isocores.empty? + + @incoming_transitions = build_incoming_transitions(states) + token_names = states.terms.map { |term| [term.number, term.display_name] }.to_h + + io << "IELR Isocores\n\n" + + lalr_isocores.each do |lalr_isocore| + report_isocore_set(io, lalr_isocore, token_names) + end + ensure + @incoming_transitions = nil + end + + private + + # @rbs (Lrama::States states) -> Array[Lrama::State] + def lalr_isocores_with_splits(states) + states.states.select do |state| + !state.split_state? && state.ielr_isocores.size > 1 + end + end + + # @rbs (Lrama::States states) -> Hash[Lrama::State, Array[Lrama::State::Action::Shift | Lrama::State::Action::Goto]] + def build_incoming_transitions(states) + incoming_transitions = Hash.new { |index, to_state| index[to_state] = [] } #: Hash[Lrama::State, Array[Lrama::State::Action::Shift | Lrama::State::Action::Goto]] + states.states.each do |state| + state.transitions.each do |transition| + incoming_transitions[transition.to_state] << transition + end + end + incoming_transitions + end + + # @rbs (IO io, Lrama::State lalr_isocore, Hash[Integer, String] token_names) -> void + def report_isocore_set(io, lalr_isocore, token_names) + isocores = lalr_isocore.ielr_isocores.sort_by(&:id) + + io << " Isocore of LALR state #{lalr_isocore.id}: IELR states #{isocores.map(&:id).join(', ')}\n\n" + report_incoming_transitions(io, isocores) + report_item_lookahead_differences(io, isocores, token_names) + report_dominant_contributions(io, isocores) + io << "\n" + end + + # @rbs (IO io, Array[Lrama::State] isocores) -> void + def report_incoming_transitions(io, isocores) + io << " Incoming transitions\n" + + isocores.each do |isocore| + @incoming_transitions[isocore] + .sort_by { |transition| [transition.from_state.id, transition.next_sym.number] } + .each do |transition| + io << " state #{transition.from_state.id} -- #{transition.next_sym.display_name} --> state #{isocore.id} #{isocore_label(isocore)}\n" + end + end + + io << "\n" + end + + # @rbs (IO io, Array[Lrama::State] isocores, Hash[Integer, String] token_names) -> void + def report_item_lookahead_differences(io, isocores, token_names) + differing_kernel_items = isocores.first.kernels.select do |kernel_item| + isocores.map { |isocore| isocore.item_lookahead_set.fetch(kernel_item) }.uniq.size > 1 + end + return if differing_kernel_items.empty? + + io << " Lookahead differences\n" + + differing_kernel_items.each do |kernel_item| + io << " #{kernel_item.display_name}\n" + + isocores.each do |isocore| + tokens = Bitmap.to_array(isocore.item_lookahead_set.fetch(kernel_item)).map { |number| token_names.fetch(number) } + io << " state #{isocore.id} #{isocore_label(isocore)}: [#{tokens.join(', ')}]\n" + end + end + + io << "\n" + end + + # @rbs (IO io, Array[Lrama::State] isocores) -> void + def report_dominant_contributions(io, isocores) + lalr_isocore = isocores.first.lalr_isocore + reported = false + + lalr_isocore.annotation_list.each do |annotation| + dominant_contributions = isocores.map do |isocore| + annotation.dominant_contribution(isocore.item_lookahead_set, isocore.kernels) + end + next if dominant_contributions.compact.uniq.size <= 1 + + io << " Why it split\n" unless reported + reported = true + io << " token #{annotation.token.display_name} (conflict in LALR state #{annotation.state.id})\n" + isocores.zip(dominant_contributions).each do |isocore, dominant_contribution| + io << " state #{isocore.id} #{isocore_label(isocore)}: #{format_dominant_contribution(dominant_contribution)}\n" + end + end + + io << "\n" if reported + end + + # @rbs (Array[Lrama::State::InadequacyAnnotation::action]? dominant_contribution) -> String + def format_dominant_contribution(dominant_contribution) + return "no contribution" if dominant_contribution.nil? + return "syntax error" if dominant_contribution.empty? + + dominant_contribution.map { |action| format_action(action) }.join(", ") + end + + # @rbs (Lrama::State::InadequacyAnnotation::action action) -> String + def format_action(action) + case action + when Lrama::State::Action::Shift + "shift and go to state #{action.to_state.id}" + when Lrama::State::Action::Reduce + rule = action.item.rule + "reduce using rule #{rule.id} (#{rule.lhs.display_name})" + else + raise "Unsupported action #{action.class}" + end + end + + # @rbs (Lrama::State isocore) -> String + def isocore_label(isocore) + isocore.split_state? ? "[IELR split]" : "[LALR core]" + end + end + end +end diff --git a/sig/generated/lrama/reporter/ielr.rbs b/sig/generated/lrama/reporter/ielr.rbs new file mode 100644 index 000000000..609d8bc3c --- /dev/null +++ b/sig/generated/lrama/reporter/ielr.rbs @@ -0,0 +1,42 @@ +# Generated from lib/lrama/reporter/ielr.rb with RBS::Inline + +module Lrama + class Reporter + class Ielr + # @rbs (?ielr: bool, **bool _) -> void + def initialize: (?ielr: bool, **bool _) -> void + + # @rbs (IO io, Lrama::States states) -> void + def report: (IO io, Lrama::States states) -> void + + private + + # @rbs (Lrama::States states) -> Array[Lrama::State] + def lalr_isocores_with_splits: (Lrama::States states) -> Array[Lrama::State] + + # @rbs (Lrama::States states) -> Hash[Lrama::State, Array[Lrama::State::Action::Shift | Lrama::State::Action::Goto]] + def build_incoming_transitions: (Lrama::States states) -> Hash[Lrama::State, Array[Lrama::State::Action::Shift | Lrama::State::Action::Goto]] + + # @rbs (IO io, Lrama::State lalr_isocore, Hash[Integer, String] token_names) -> void + def report_isocore_set: (IO io, Lrama::State lalr_isocore, Hash[Integer, String] token_names) -> void + + # @rbs (IO io, Array[Lrama::State] isocores) -> void + def report_incoming_transitions: (IO io, Array[Lrama::State] isocores) -> void + + # @rbs (IO io, Array[Lrama::State] isocores, Hash[Integer, String] token_names) -> void + def report_item_lookahead_differences: (IO io, Array[Lrama::State] isocores, Hash[Integer, String] token_names) -> void + + # @rbs (IO io, Array[Lrama::State] isocores) -> void + def report_dominant_contributions: (IO io, Array[Lrama::State] isocores) -> void + + # @rbs (Array[Lrama::State::InadequacyAnnotation::action]? dominant_contribution) -> String + def format_dominant_contribution: (Array[Lrama::State::InadequacyAnnotation::action]? dominant_contribution) -> String + + # @rbs (Lrama::State::InadequacyAnnotation::action action) -> String + def format_action: (Lrama::State::InadequacyAnnotation::action action) -> String + + # @rbs (Lrama::State isocore) -> String + def isocore_label: (Lrama::State isocore) -> String + end + end +end diff --git a/spec/lrama/option_parser_spec.rb b/spec/lrama/option_parser_spec.rb index 6404c220c..6d6293744 100644 --- a/spec/lrama/option_parser_spec.rb +++ b/spec/lrama/option_parser_spec.rb @@ -80,6 +80,7 @@ lookaheads explicitly associate lookahead tokens to items solved describe shift/reduce conflicts solving counterexamples, cex generate conflict counterexamples + ielr show IELR isocores with lookahead diffs and split reasons rules list unused rules terms list unused terminals verbose report detailed internal state and analysis results @@ -134,6 +135,7 @@ expect(opts).to eq({ grammar: true, states: true, itemsets: true, lookaheads: true, solved: true, counterexamples: true, + ielr: true, rules: true, terms: true, verbose: true }) end @@ -143,11 +145,19 @@ expect(opts).to eq({ grammar: true, states: true, itemsets: true, lookaheads: true, solved: true, counterexamples: true, + ielr: true, rules: true, terms: true, verbose: true }) end end + context "when ielr is passed" do + it "returns option hash ielr flag enabled" do + opts = option_parser.send(:validate_report, ["ielr"]) + expect(opts).to eq({grammar: true, ielr: true}) + end + end + context "when none is passed" do it "returns empty option hash" do opts = option_parser.send(:validate_report, ["none"]) diff --git a/spec/lrama/reporter/ielr_spec.rb b/spec/lrama/reporter/ielr_spec.rb new file mode 100644 index 000000000..cff8178e2 --- /dev/null +++ b/spec/lrama/reporter/ielr_spec.rb @@ -0,0 +1,118 @@ +# frozen_string_literal: true + +RSpec.describe Lrama::Reporter::Ielr do + def report_for(source, path = "ielr_report.y") + grammar = Lrama::Parser.new(source, path).parse + grammar.prepare + grammar.validate! + states = Lrama::States.new(grammar, Lrama::Tracer.new(Lrama::Logger.new)) + states.compute + states.compute_ielr + + io = StringIO.new + described_class.new(ielr: true).report(io, states) + io.string + end + + describe "#report" do + it "reports LALR vs IELR split diagnostics" do + y = <<~INPUT + %{ + // Prologue + %} + + %define lr.type ielr + + %token a + %token b + %token c + + %% + + S: S2 + ; + + S2: a A1 a + | a A2 b + | b A1 b + | b A2 a + ; + + A1: c + ; + + A2: c + ; + + %% + INPUT + + expect(report_for(y)).to eq(<<~STR) + IELR Isocores + + Isocore of LALR state 5: IELR states 5, 15 + + Incoming transitions + state 1 -- c --> state 5 [LALR core] + state 2 -- c --> state 15 [IELR split] + + Lookahead differences + c • (rule 6) + state 5 [LALR core]: [a] + state 15 [IELR split]: [b] + c • (rule 7) + state 5 [LALR core]: [b] + state 15 [IELR split]: [a] + + Why it split + token a (conflict in LALR state 5) + state 5 [LALR core]: reduce using rule 6 (A1) + state 15 [IELR split]: reduce using rule 7 (A2) + token b (conflict in LALR state 5) + state 5 [LALR core]: reduce using rule 7 (A2) + state 15 [IELR split]: reduce using rule 6 (A1) + + + STR + end + + it "reports dominant contributions at the state where the conflict originated" do + path = fixture_path("integration/ielr.y") + expect(report_for(File.read(path), path)).to include(<<~STR.chomp) + Why it split + token a (conflict in LALR state 14) + state 4 [LALR core]: reduce using rule 9 (E) + state 19 [IELR split]: shift and go to state 17 + STR + end + + it "omits a token when only one isocore contributes to its conflict" do + report = report_for(<<~GRAMMAR) + %define lr.type ielr + %% + s: 'p' x 'a' | 'p' y 'b' | 'q' x 'b' | 'q' y 'a' | 'p' x 'c' | 'p' y 'c'; + x: 'z'; + y: 'z'; + GRAMMAR + expect(report.scan(/token ('\w+')/).flatten).to eq(["'a'", "'b'"]) + end + + it "reports a nonassociative syntax error as a distinct contribution" do + report = report_for(<<~GRAMMAR) + %define lr.type ielr + %nonassoc 'a' + %% + s: 'p' x 'a' | 'q' x 'b' | 'p' z 'b' | 'q' z 'a'; + x: 'z' %prec 'a'; + z: 'z' 'a'; + GRAMMAR + + expect(report).to include(<<~STR.chomp) + Why it split + token 'a' (conflict in LALR state 4) + state 4 [LALR core]: syntax error + state 15 [IELR split]: shift and go to state 10 + STR + end + end +end