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
19 changes: 12 additions & 7 deletions lib/lrama/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -454,14 +454,19 @@ def annotate_predecessor(predecessor)
cs = 0
predecessor.lane_items[self].each {|pred_kernel, kernel|
next unless contributions[kernel_indices.fetch(kernel)] == 1
pred_index = predecessor.kernel_indices[pred_kernel]
next unless pred_index
c =
(kernel.position > 1 && predecessor.item_lookahead_set[pred_kernel][annotation.token.number] == 1) ||
(kernel.position == 1 && predecessor.lhs_contributions(kernel.lhs, annotation.token)[pred_kernel])
cs |= (1 << pred_index) if c
propagates = true if c

if kernel.position == 1
# Enclosing rules can contribute through kernels outside this transition lane.
predecessor.lhs_contributions(kernel.lhs, annotation.token).each do |contributing_kernel, contributes|
cs |= (1 << predecessor.kernel_indices.fetch(contributing_kernel)) if contributes
end
else
pred_index = predecessor.kernel_indices[pred_kernel]
next unless pred_index
cs |= (1 << pred_index) if predecessor.item_lookahead_set[pred_kernel][annotation.token.number] == 1
end
}
propagates = true if cs != 0
contribution_matrix[action] = cs
end
}
Expand Down
33 changes: 33 additions & 0 deletions spec/fixtures/ielr/nested_rule_contributions.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
%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' left_tail;
right: 'x' right_tail;
left_tail: 'y';
right_tail: '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();
}
2 changes: 1 addition & 1 deletion spec/ielr/grammar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
states.compute_ielr

expect(states.states.length).to eq(17)
expect(states.sr_conflicts_count).to eq(5)
expect(states.sr_conflicts_count).to eq(4)
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 @@ -105,6 +105,23 @@ def generate_object(grammar_file_path, c_path, obj_path, command_args: [])
end
end

it "accepts all paths through nested IELR rules" do
Dir.mktmpdir do |directory|
c_path = File.join(directory, "nested#{file_extension}")
obj_path = File.join(directory, "nested")
generate_object(fixture_path("ielr/nested_rule_contributions.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 "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
11 changes: 11 additions & 0 deletions spec/lrama/state_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,17 @@ def annotation(contributions, from: origin, on: token, with: actions)
expect(predecessor.annotation_list.map(&:state)).to eq([origin, state])
end

it "propagates first-position contributions through kernels outside the transition lane" do
predecessor = described_class.new(0, token, [last_item, next_item])
predecessor.lane_items[state] = [[item, next_item]]
allow(predecessor).to receive(:lhs_contributions).with(rule.lhs, token).and_return({ last_item => true, next_item => true })
state.annotation_list << annotation(0b01)

expect(state.annotate_predecessor(predecessor)).to be(true)

expect(predecessor.annotation_list.first.contribution_matrix).to eq({ actions.first => 0b11 })
end

it "shares the unconditional contribution check across actions" do
state.annotation_list << Lrama::State::InadequacyAnnotation.new(origin, token, actions + [other_action], {
actions.first => 0b01, other_action => 0b01,
Expand Down
18 changes: 18 additions & 0 deletions spec/lrama/states_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,24 @@
end
end

describe 'IELR lookaheads through nested rules' do
["'y'", "'y' 'z'", "'y' 'z' 'w'"].each do |rhs|
it "splits nested reductions with the shared tail #{rhs} without conflicts" do
source = File.read(fixture_path("ielr/nested_rule_contributions.y")).gsub("'y'", rhs)
grammar = Lrama::Parser.new(source, "nested.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