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
7 changes: 4 additions & 3 deletions lib/lrama/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,10 @@ def item_lookahead_set
prev_items = predecessors_with_item(kernel)
prev_items.map {|st, i| st.item_lookahead_set[i] }.reduce(0) {|acc, bitmap| acc | bitmap }
elsif kernel.position == 1
prev_state = @predecessors.find {|p| p.transitions_by_symbol_number.key?(kernel.lhs.number) }
goto = prev_state.transitions_by_symbol_number[kernel.lhs.number] #: Action::Goto?
prev_state.goto_follows[goto]
@predecessors.reduce(0) do |lookaheads, prev_state|
goto = prev_state.transitions_by_symbol_number.fetch(kernel.lhs.number) #: Action::Goto
lookaheads | prev_state.goto_follows.fetch(goto)
end
end
[kernel, value]
}.to_h.compare_by_identity
Expand Down
31 changes: 31 additions & 0 deletions spec/fixtures/ielr/multiple_predecessor_lookaheads.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
%define lr.type ielr
%define api.pure full
%locations
%code provides {
static int yylex(YYSTYPE *, YYLTYPE *);
static void yyerror(YYLTYPE *, const char *);
}
%%
start: 'a' left 'd' | 'a' right 'e'
| 'b' left 'e' | 'b' right 'd';
left: 'x' 'y';
right: 'x' 'y';
%%
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();
}
6 changes: 3 additions & 3 deletions spec/ielr/grammar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
expect(states.rr_conflicts_count).to eq(0)
end

it "preserves always contributions without splitting equivalent states" do
it "builds parser tables for a grammar with always contributions" do
path = fixture_path("ielr/always_contributions.y")
grammar = Lrama::Parser.new(File.read(path), path).parse
grammar.prepare
Expand All @@ -74,8 +74,8 @@

states.compute_ielr

expect(states.states.length).to eq(15)
expect(states.sr_conflicts_count).to eq(4)
expect(states.states.length).to eq(17)
expect(states.sr_conflicts_count).to eq(5)
expect(states.rr_conflicts_count).to eq(1)
expect { Lrama::Context.new(states) }.not_to raise_error
end
Expand Down
17 changes: 17 additions & 0 deletions spec/lrama/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ def generate_object(grammar_file_path, c_path, obj_path, command_args: [])

include IntegrationHelper

it "accepts all paths through shared multi-symbol IELR reductions" do
Dir.mktmpdir do |directory|
c_path = File.join(directory, "multiple_predecessors#{file_extension}")
obj_path = File.join(directory, "multiple_predecessors")
generate_object(fixture_path("ielr/multiple_predecessor_lookaheads.y"), c_path, obj_path)

%w[axyd axye bxyd bxye].each do |input|
_, error, status = Open3.capture3(obj_path, input)
expect(status.success?).to be(true), "#{input}: #{error}"
end
["", "axy", "axyde", "cxyd", "axxd", "xyde"].each do |input|
_, _, status = Open3.capture3(obj_path, input)
expect(status.success?).to be(false), input
end
end
end

it "uses structured tags in declarations and explicit references" do
c_path = Dir.tmpdir + "/structured_tags#{file_extension}"
obj_path = Dir.tmpdir + "/structured_tags"
Expand Down
40 changes: 40 additions & 0 deletions spec/lrama/states_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,46 @@
end
end

describe 'IELR lookaheads from multiple predecessors' do
let(:source) { File.read(fixture_path("ielr/multiple_predecessor_lookaheads.y")) }

[false, true].each do |reverse|
it "unions every predecessor's goto follows with reverse order #{reverse}" do
grammar = Lrama::Parser.new(source, "lookaheads.y").parse
grammar.prepare
states = described_class.new(grammar, Lrama::Tracer.new(Lrama::Logger.new))
states.compute
states.send(:compute_predecessors)
states.send(:compute_follow_kernel_items)
always_follows = states.send(:compute_always_follows)
states.send(:compute_goto_follows, always_follows)
target = states.states.find { |state| state.kernels.any? { |item| item.lhs.name == "left" && item.position == 1 } }
target.predecessors.reverse! if reverse

lookaheads = target.kernels.map do |item|
Lrama::Bitmap.to_array(target.item_lookahead_set.fetch(item)).map { |number| grammar.find_symbol_by_number!(number).name }
end

expect(lookaheads).to eq([["'d'", "'e'"], ["'d'", "'e'"]])
end
end

["'x'", "'x' 'y'", "'x' 'y' 'z'"].each do |rhs|
it "splits reductions with the shared RHS #{rhs} without conflicts" do
grammar = Lrama::Parser.new(source.gsub("'x' 'y'", rhs), "lookaheads.y").parse
grammar.prepare
states = described_class.new(grammar, Lrama::Tracer.new(Lrama::Logger.new))
states.compute

states.compute_ielr

expect(states.sr_conflicts_count).to eq(0)
expect(states.rr_conflicts_count).to eq(0)
expect { Lrama::Context.new(states) }.not_to raise_error
end
end
end

describe '#compute' do
it "basic" do
path = "common/basic.y"
Expand Down
Loading