Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/lrama/states.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 31 additions & 0 deletions spec/fixtures/ielr/resolved_shift_conflicts.y
Original file line number Diff line number Diff line change
@@ -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();
}
11 changes: 11 additions & 0 deletions spec/lrama/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
14 changes: 14 additions & 0 deletions spec/lrama/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
28 changes: 28 additions & 0 deletions spec/lrama/states_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
Loading