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
2 changes: 1 addition & 1 deletion lib/lrama/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def compute_yydefact
if state.reduces.map(&:selected_look_ahead).any? {|la| !la.empty? }
# 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|
reduce.selected_look_ahead.each do |term|
actions[term.number] = rule_id_to_action_number(reduce.rule.id)
end
end
Expand Down
32 changes: 32 additions & 0 deletions spec/fixtures/ielr/selected_reduction_lookaheads.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
%define api.pure full
%locations
%code provides {
static int yylex(YYSTYPE *, YYLTYPE *);
static void yyerror(YYLTYPE *, const char *);
}
%precedence LOW
%precedence 'b'
%precedence HIGH
%%
start: 'a' empty 'b' 'x' | direct 'b' 'y' | 'a' 'b' 'z' | 'a' empty 'c';
empty: %empty %prec LOW;
direct: 'a' %prec HIGH;
%%
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();
}
22 changes: 22 additions & 0 deletions spec/lrama/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,28 @@

describe "compute_yydefact" do
[:lalr, :ielr].each do |lr_type|
it "omits #{lr_type} reductions already discarded by precedence" do
source = "%define lr.type #{lr_type}\n" + File.read(fixture_path("ielr/selected_reduction_lookaheads.y"))
grammar = Lrama::Parser.new(source, "precedence.y").parse
grammar.prepare
states = Lrama::States.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 }
empty = state.reduces.find { |reduce| reduce.rule.rhs.empty? }
direct = state.reduces.find { |reduce| !reduce.rule.rhs.empty? }
token = grammar.find_symbol_by_s_value!("'b'")

expect(empty.look_ahead).to include(token)
expect(empty.selected_look_ahead).not_to include(token)
expect(empty.selected_look_ahead).to include(grammar.find_symbol_by_s_value!("'c'"))
expect(direct.selected_look_ahead).to include(token)
context = Lrama::Context.new(states)
index = context.yypact[state.id] + token.number
expect(context.yycheck[index]).to eq(token.number)
expect(context.yytable[index]).to eq(-(direct.rule.id + 1))
end

["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
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 "honors discarded reduction lookaheads in #{lr_type} parsers" do
Dir.mktmpdir do |directory|
c_path = File.join(directory, "selected_lookaheads#{file_extension}")
obj_path = File.join(directory, "selected_lookaheads")
generate_object(fixture_path("ielr/selected_reduction_lookaheads.y"), c_path, obj_path,
command_args: ["-Dlr.type=#{lr_type}"])

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

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}")
Expand Down
Loading