diff --git a/lib/lrama/states.rb b/lib/lrama/states.rb index cebc6170..20edcb2d 100644 --- a/lib/lrama/states.rb +++ b/lib/lrama/states.rb @@ -501,8 +501,11 @@ def compute_conflicts(lr_type) # @rbs () -> void def compute_shift_reduce_conflicts(lr_type) states.each do |state| + reduces = state.reduces.sort_by { |reduce| reduce.rule.id } state.term_transitions.each do |shift| - state.reduces.each do |reduce| + reduces.each do |reduce| + break if shift.not_selected + sym = shift.next_sym next unless reduce.look_ahead diff --git a/spec/fixtures/ielr/discarded_shift_conflicts.y b/spec/fixtures/ielr/discarded_shift_conflicts.y new file mode 100644 index 00000000..442dcb1d --- /dev/null +++ b/spec/fixtures/ielr/discarded_shift_conflicts.y @@ -0,0 +1,31 @@ +%define api.pure full +%locations +%code provides { + static int yylex(YYSTYPE *, YYLTYPE *); + static void yyerror(YYLTYPE *, const char *); +} +%nonassoc 'b' +%precedence HIGH +%% +start: 'a' empty 'b' 'x' | direct 'b' 'y' | 'a' 'b' 'z'; +empty: %empty %prec HIGH; +direct: 'a' %prec 'b'; +%% +static const char *input; + +static int yylex(YYSTYPE *value, YYLTYPE *location) { + (void)value; + (void)location; + return *input ? *input++ : 0; +} + +static void yyerror(YYLTYPE *location, const char *message) { + (void)location; + (void)message; +} + +int main(int argc, char **argv) { + if (argc != 2) return 2; + input = argv[1]; + return yyparse(); +} diff --git a/spec/lrama/integration_spec.rb b/spec/lrama/integration_spec.rb index 01c75e44..fb12c273 100644 --- a/spec/lrama/integration_spec.rb +++ b/spec/lrama/integration_spec.rb @@ -76,6 +76,20 @@ def generate_object(grammar_file_path, c_path, obj_path, command_args: []) include IntegrationHelper [:lalr, :ielr].each do |lr_type| + it "does not create errors from discarded shifts in #{lr_type} parsers" do + Dir.mktmpdir do |directory| + c_path = File.join(directory, "discarded_shift#{file_extension}") + obj_path = File.join(directory, "discarded_shift") + generate_object(fixture_path("ielr/discarded_shift_conflicts.y"), c_path, obj_path, + command_args: ["-Dlr.type=#{lr_type}"]) + + {"abx" => true, "aby" => false, "abz" => false, "ab" => false}.each do |input, accepted| + _, error, status = Open3.capture3(obj_path, input) + expect(status.success?).to be(accepted), "#{input}: #{error}" + end + end + end + it "honors discarded reduction lookaheads in #{lr_type} parsers" do Dir.mktmpdir do |directory| c_path = File.join(directory, "selected_lookaheads#{file_extension}") diff --git a/spec/lrama/states_spec.rb b/spec/lrama/states_spec.rb index 60296e9c..b9b8841f 100644 --- a/spec/lrama/states_spec.rb +++ b/spec/lrama/states_spec.rb @@ -1404,6 +1404,43 @@ class go to state 5 end describe '#compute_conflicts' do + [:lalr, :ielr].each do |lr_type| + [ + ["higher precedence", "%nonassoc 'b'\n%precedence HIGH", + ["empty: %empty %prec HIGH;", "direct: 'a' %prec 'b';"], :reduce], + ["nonassociativity before higher precedence", "%nonassoc 'b'\n%precedence HIGH", + ["direct: 'a' %prec 'b';", "empty: %empty %prec HIGH;"], :error], + ["left associativity", "%precedence LOW\n%left 'b'", + ["empty: %empty %prec 'b';", "direct: 'a' %prec LOW;"], :reduce], + ["nonassociativity", "%precedence LOW\n%nonassoc 'b'", + ["empty: %empty %prec 'b';", "direct: 'a' %prec LOW;"], :error], + ].each do |description, precedences, rules, resolution| + it "stops comparing discarded shifts after #{description} in #{lr_type}" do + grammar = Lrama::Parser.new(<<~GRAMMAR, "discarded_shift.y").parse + %define lr.type #{lr_type} + #{precedences} + %% + start: 'a' empty 'b' 'x' | direct 'b' 'y' | 'a' 'b' 'z'; + #{rules.join("\n")} + GRAMMAR + grammar.prepare + states = described_class.new(grammar, Lrama::Tracer.new(Lrama::Logger.new)) + states.compute + states.compute_ielr if lr_type == :ielr + state = states.states.find { |s| s.reduces.length == 2 } + token = grammar.find_symbol_by_s_value!("'b'") + reduces = state.reduces.sort_by { |reduce| reduce.rule.id } + + expect(state.resolved_conflicts.map { |conflict| [conflict.reduce.rule.id, conflict.which] }).to eq([ + [reduces.first.rule.id, resolution], + ]) + expect(state.term_transitions.find { |shift| shift.next_sym == token }.not_selected).to be(true) + expect(reduces.first.selected_look_ahead.include?(token)).to be(resolution == :reduce) + expect(reduces.last.selected_look_ahead).to include(token) + end + end + end + describe "rules have no explicit precedence" do it "inherits precedence from the last term in RHS" do y = <<~INPUT