diff --git a/lib/lrama/states.rb b/lib/lrama/states.rb index b7d42e0a..238abc08 100644 --- a/lib/lrama/states.rb +++ b/lib/lrama/states.rb @@ -580,6 +580,10 @@ def compute_shift_reduce_conflicts(lr_type) end end end + + state.conflicts.reject! do |conflict| + conflict.is_a?(State::ShiftReduceConflict) && conflict.shift.not_selected + end end end diff --git a/spec/fixtures/ielr/resolved_shift_conflicts.y b/spec/fixtures/ielr/resolved_shift_conflicts.y new file mode 100644 index 00000000..9d532aee --- /dev/null +++ b/spec/fixtures/ielr/resolved_shift_conflicts.y @@ -0,0 +1,31 @@ +%define api.pure full +%expect 0 +%locations +%code provides { + static int yylex(YYSTYPE *, YYLTYPE *); + static void yyerror(YYLTYPE *, const char *); +} +%nonassoc 'b' +%% +start: 'a' empty 'b' 'x' | direct 'b' 'y' | 'a' 'b' 'z' | 'a' empty 'c'; +empty: %empty; +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/command_spec.rb b/spec/lrama/command_spec.rb index 49ebbdde..b568b30e 100644 --- a/spec/lrama/command_spec.rb +++ b/spec/lrama/command_spec.rb @@ -128,6 +128,17 @@ end [:lalr, :ielr].each do |lr_type| + it "honors %expect 0 after resolving a previously recorded #{lr_type} shift conflict" do + Dir.mktmpdir do |dir| + outfile = File.join(dir, "parse.c") + command = Lrama::Command.new(["-o", outfile, "-Dlr.type=#{lr_type}", + fixture_path("ielr/resolved_shift_conflicts.y")]) + + expect { command.run }.not_to raise_error + expect(File).to exist(outfile) + end + end + it "honors %expect 0 after discarding #{lr_type} reduction lookaheads" do Dir.mktmpdir do |dir| outfile = File.join(dir, "parse.c") diff --git a/spec/lrama/integration_spec.rb b/spec/lrama/integration_spec.rb index fb12c273..f74c5da3 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 "preserves nonassociative errors after removing obsolete #{lr_type} S/R conflicts" do + Dir.mktmpdir do |directory| + c_path = File.join(directory, "resolved_shift#{file_extension}") + obj_path = File.join(directory, "resolved_shift") + generate_object(fixture_path("ielr/resolved_shift_conflicts.y"), c_path, obj_path, + command_args: ["-Dlr.type=#{lr_type}"]) + + {"ac" => true, "abx" => false, "aby" => false, "abz" => false, "" => false}.each do |input, accepted| + _, error, status = Open3.capture3(obj_path, input) + expect(status.success?).to be(accepted), "#{input}: #{error}" + end + end + end + 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}") diff --git a/spec/lrama/states_spec.rb b/spec/lrama/states_spec.rb index a197dffe..cddf92f6 100644 --- a/spec/lrama/states_spec.rb +++ b/spec/lrama/states_spec.rb @@ -1405,6 +1405,34 @@ class go to state 5 describe '#compute_conflicts' do [:lalr, :ielr].each do |lr_type| + [ + ["higher precedence", "%precedence 'b'\n%precedence HIGH", "%prec 'b'", "%prec HIGH", ["'c'"], 1], + ["left associativity", "%left 'b'", "", "%prec 'b'", ["'c'"], 1], + ["nonassociativity", "%nonassoc 'b'", "", "%prec 'b'", ["'c'"], 0], + ["equal precedence", "%precedence 'b'", "%prec 'b'", "%prec 'b'", ["'b'", "'b'", "'c'"], 1], + ].each do |description, precedences, empty_prec, direct_prec, unresolved, rr_count| + it "reports only remaining #{lr_type} S/R conflicts after #{description}" do + grammar = Lrama::Parser.new(<<~GRAMMAR, "resolved_shift.y").parse + %define lr.type #{lr_type} + #{precedences} + %% + start: 'a' empty 'b' 'x' | direct 'b' 'y' | 'a' 'b' 'z' + | 'a' empty 'c' | 'a' 'c' 'w'; + empty: %empty #{empty_prec}; + direct: 'a' #{direct_prec}; + 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 } + + expect(state.sr_conflicts.flat_map(&:symbols).map(&:name)).to eq(unresolved) + expect(states.sr_conflicts_count).to eq(unresolved.length) + expect(state.rr_conflicts.count).to eq(rr_count) + end + end + [ ["higher precedence", "%nonassoc 'b'\n%precedence HIGH", ["empty: %empty %prec HIGH;", "direct: 'a' %prec 'b';"], :reduce],