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
62 changes: 30 additions & 32 deletions lib/lrama/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class State
# type lookahead_set = Hash[Item, Bitmap::bitmap]
# type dominant_contributions = Hash[InadequacyAnnotation, Array[InadequacyAnnotation::action]?]
# type item_pair = [State, Item]
# type item_lookahead_dependencies = Hash[Integer, Array[[State, Item, Array[item_pair]]]]
# type item_origins_cache = Hash[item_pair, Array[item_pair]]
# type annotation_key = [Integer, Grammar::Symbol, Array[InadequacyAnnotation::action], Hash[InadequacyAnnotation::action, Bitmap::bitmap?]]
# type annotation_indexes = Hash[Integer, Set[annotation_key]]
Expand Down Expand Up @@ -542,46 +543,43 @@ def lhs_contributions(sym, token)
def item_lookahead_set
return @item_lookahead_set if @item_lookahead_set

pending = [[self, false]] #: Array[[State, bool]]
while (entry = pending.pop)
state, dependencies_computed = entry
state.compute_item_lookahead_set(pending, dependencies_computed)
pending = [self] #: Array[State]
dependencies = {} #: item_lookahead_dependencies
while (state = pending.pop)
state.collect_item_lookahead_dependencies(pending, dependencies)
end

# Predecessor states can form cycles, but the predecessor item's dot is one position earlier.
dependencies.keys.sort.each do |position|
dependencies.fetch(position).each do |state, item, predecessors|
state.item_lookahead_set[item] = predecessors.reduce(0) do |bits, (predecessor, previous_item)|
bits | predecessor.item_lookahead_set.fetch(previous_item)
end
end
end
@item_lookahead_set
end

# @rbs (Array[[State, bool]] pending, bool dependencies_computed) -> void
def compute_item_lookahead_set(pending, dependencies_computed)
unless dependencies_computed
return if @item_lookahead_set
# @rbs (Array[State] pending, item_lookahead_dependencies dependencies) -> void
def collect_item_lookahead_dependencies(pending, dependencies)
return if @item_lookahead_set

@item_lookahead_set = kernels.map {|k| [k, 0] }.to_h.compare_by_identity
pending << [self, true]
# Depth-first order matters when a predecessor refers to an unfinished state.
kernels.reverse_each do |kernel|
next if kernel.lhs.accept_symbol? || kernel.position <= 1
predecessors_with_item(kernel).reverse_each do |state, _|
pending << [state, false]
@item_lookahead_set = {} #: lookahead_set
@item_lookahead_set.compare_by_identity
kernels.each do |item|
if item.lhs.accept_symbol?
@item_lookahead_set[item] = 0
elsif item.position == 1
@item_lookahead_set[item] = @predecessors.reduce(0) do |lookaheads, predecessor|
goto = predecessor.transitions_by_symbol_number.fetch(item.lhs.number) #: Action::Goto
lookaheads | predecessor.goto_follows.fetch(goto)
end
else
predecessors = predecessors_with_item(item)
(dependencies[item.position] ||= []) << [self, item, predecessors]
predecessors.each { |predecessor, _| pending << predecessor }
end
return
end

@item_lookahead_set = kernels.map {|kernel|
value =
if kernel.lhs.accept_symbol?
0
elsif kernel.position > 1
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
@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
end

# @rbs (lookahead_set k) -> void
Expand Down
6 changes: 4 additions & 2 deletions sig/generated/lrama/state.rbs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions spec/fixtures/ielr/initial_item_lookaheads.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
%define lr.type ielr
%define api.pure full
%locations
%code provides {
static int yylex(YYSTYPE *, YYLTYPE *);
static void yyerror(YYLTYPE *, const char *);
}
%%
expr: 'd' | term term 'c' | 'c' item item term;
item: 'a' | term 'b';
term: item expr | expr;
%%
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();
}
13 changes: 13 additions & 0 deletions spec/lrama/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ def generate_object(grammar_file_path, c_path, obj_path, command_args: [])
end
end

it "propagates initial item lookaheads through recursive IELR rules" do
Dir.mktmpdir do |directory|
c_path = File.join(directory, "initial_lookaheads#{file_extension}")
obj_path = File.join(directory, "initial_lookaheads")
generate_object(fixture_path("ielr/initial_item_lookaheads.y"), c_path, obj_path)

{"caad" => 0, "cdbad" => 0, "d" => 0, "caa" => 1, "cdba" => 1}.each do |input, expected_status|
_, error, status = Open3.capture3(obj_path, input)
expect(status.exitstatus).to eq(expected_status), "#{input}: #{error}"
end
end
end

it "preserves distinct IELR annotation contributions through nullable recursive rules" do
Dir.mktmpdir do |directory|
c_path = File.join(directory, "distinct_annotations#{file_extension}")
Expand Down
20 changes: 20 additions & 0 deletions spec/lrama/states_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,26 @@
end

describe 'IELR lookaheads through nested rules' do
it 'propagates lookaheads through every dot position of a recursive rule' do
source = "%%\nlist: %empty | list 'x' list;\n"
grammar = Lrama::Parser.new(source, "recursive.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)

after_x = states.states.find { |state| state.accessing_symbol.name == "'x'" }
after_list = after_x.nterm_transitions.first.to_state
completed = after_list.kernels.find(&:end_of_rule?)
expected = Lrama::Bitmap.from_array([grammar.eof_symbol.number, after_x.accessing_symbol.number])

expect(after_x.item_lookahead_set.fetch(after_x.kernels.first)).to eq(expected)
expect(after_list.item_lookahead_set.fetch(completed)).to eq(expected)
end

["'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)
Expand Down