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: 2 additions & 2 deletions lib/lrama/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ def compute_yydefact
actions = Array.new(@states.terms.count, 0)

if state.reduces.map(&:selected_look_ahead).any? {|la| !la.empty? }
# Iterate reduces with reverse order so that first rule is used.
state.reduces.reverse_each do |reduce|
# Visit later rules first so earlier rules win reduce/reduce conflicts.
state.reduces.sort_by {|reduce| reduce.rule.id }.reverse_each do |reduce|
reduce.look_ahead.each do |term|
actions[term.number] = rule_id_to_action_number(reduce.rule.id)
end
Expand Down
29 changes: 29 additions & 0 deletions spec/fixtures/ielr/reduce_reduce_rule_order.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
%define api.pure full
%locations
%code provides {
static int yylex(YYSTYPE *, YYLTYPE *);
static void yyerror(YYLTYPE *, const char *);
}
%%
start: 'a' empty 'b' 'x' | direct 'b' 'y';
empty: %empty;
direct: 'a';
%%
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();
}
23 changes: 23 additions & 0 deletions spec/lrama/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,29 @@

describe "compute_yydefact" do
[:lalr, :ielr].each do |lr_type|
["empty: %empty;\ndirect: 'a';", "direct: 'a';\nempty: %empty;"].each do |rules|
it "resolves #{lr_type} R/R conflicts by rule order with #{rules.lines.first.strip} first" do
grammar = Lrama::Parser.new(<<~GRAMMAR, "reduce_order.y").parse
%define lr.type #{lr_type}
%%
start: 'a' empty 'b' 'x' | direct 'b' 'y';
#{rules}
GRAMMAR
grammar.prepare
states = Lrama::States.new(grammar, Lrama::Tracer.new(Lrama::Logger.new))
states.compute
states.compute_ielr if lr_type == :ielr
context = Lrama::Context.new(states)

expect(states.rr_conflicts_count).to eq(1)
state = states.states.find { |s| s.reduces.length == 2 }
token = grammar.find_symbol_by_s_value!("'b'")
index = context.yypact[state.id] + token.number
expect(context.yycheck[index]).to eq(token.number)
expect(context.yytable[index]).to eq(-(grammar.rules[3].id + 1))
end
end

["'a' | 'a'", "|"].each do |alternatives|
it "builds #{lr_type} tables for duplicate alternatives #{alternatives.inspect}" do
grammar = Lrama::Parser.new(<<~GRAMMAR, "duplicate.y").parse
Expand Down
16 changes: 16 additions & 0 deletions spec/lrama/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ def generate_object(grammar_file_path, c_path, obj_path, command_args: [])

include IntegrationHelper

[:lalr, :ielr].each do |lr_type|
it "prefers the earlier empty rule in #{lr_type} R/R conflicts" do
Dir.mktmpdir do |directory|
c_path = File.join(directory, "reduce_order#{file_extension}")
obj_path = File.join(directory, "reduce_order")
generate_object(fixture_path("ielr/reduce_reduce_rule_order.y"), c_path, obj_path,
command_args: ["-Dlr.type=#{lr_type}"])

{"abx" => true, "aby" => false}.each do |input, accepted|
_, error, status = Open3.capture3(obj_path, input)
expect(status.success?).to be(accepted), "#{input}: #{error}"
end
end
end
end

it "accepts all paths through shared multi-symbol IELR reductions" do
Dir.mktmpdir do |directory|
c_path = File.join(directory, "multiple_predecessors#{file_extension}")
Expand Down
Loading