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
5 changes: 4 additions & 1 deletion lib/lrama/states.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions spec/fixtures/ielr/discarded_shift_conflicts.y
Original file line number Diff line number Diff line change
@@ -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();
}
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 "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}")
Expand Down
37 changes: 37 additions & 0 deletions spec/lrama/states_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading