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
36 changes: 17 additions & 19 deletions lib/lrama/state.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# rbs_inline: enabled
# frozen_string_literal: true

require "set"
require_relative "state/action"
require_relative "state/inadequacy_annotation"
require_relative "state/item"
Expand All @@ -21,6 +22,8 @@ class State
# type dominant_contributions = Hash[InadequacyAnnotation, Array[InadequacyAnnotation::action]?]
# type item_pair = [State, Item]
# 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]]
#
# @id: Integer
# @accessing_symbol: Grammar::Symbol
Expand Down Expand Up @@ -432,8 +435,8 @@ def annotate_manifestation

# Definition 3.32 (annotate_predecessor)
#
# @rbs (State predecessor, ?Array[InadequacyAnnotation] annotations) -> bool
def annotate_predecessor(predecessor, annotations = annotation_list)
# @rbs (State predecessor, ?Array[InadequacyAnnotation] annotations, ?annotation_indexes indexes) -> bool
def annotate_predecessor(predecessor, annotations = annotation_list, indexes = {})
always_contributions = {} #: Hash[Grammar::Symbol, Hash[Item, bool]]
propagating_list = []
annotations.each {|annotation|
Expand Down Expand Up @@ -480,27 +483,22 @@ def annotate_predecessor(predecessor, annotations = annotation_list)

propagating_list << InadequacyAnnotation.new(annotation.state, annotation.token, annotation.actions, contribution_matrix)
}
predecessor.append_annotation_list(propagating_list)
predecessor.append_annotation_list(propagating_list, indexes)
end

# @rbs (Array[InadequacyAnnotation] propagating_list) -> bool
def append_annotation_list(propagating_list)
changed = false
propagating_by_key = propagating_list.group_by {|annotation|
[annotation.state.id, annotation.token, annotation.actions]
}
# @rbs (Array[InadequacyAnnotation] propagating_list, ?annotation_indexes indexes) -> bool
def append_annotation_list(propagating_list, indexes = {})
return false if propagating_list.empty?

annotation_list.each do |annotation|
merging_list = propagating_by_key.delete([annotation.state.id, annotation.token, annotation.actions])
changed = annotation.merge_matrix(merging_list.map(&:contribution_matrix)) || changed if merging_list
# Unioning distinct paths can hide differences in their dominant actions.
keys = indexes[id] ||= Set.new(annotation_list.map do |annotation|
[annotation.state.id, annotation.token, annotation.actions, annotation.contribution_matrix] #: annotation_key
end)
appending = propagating_list.select do |annotation|
keys.add?([annotation.state.id, annotation.token, annotation.actions, annotation.contribution_matrix])
end

# Preserve propagation order even when unmatched keys are interleaved.
appending = propagating_list.select {|annotation|
propagating_by_key.key?([annotation.state.id, annotation.token, annotation.actions])
}
@annotation_list += appending
changed || !appending.empty?
@annotation_list.concat(appending)
!appending.empty?
end

# Definition 3.31 (compute_lhs_contributions)
Expand Down
26 changes: 0 additions & 26 deletions lib/lrama/state/inadequacy_annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,6 @@ def contributed?(kernel_index)
@contribution_matrix.any? {|action, contributions| !contributions.nil? && contributions[kernel_index] == 1 }
end

# @rbs (Array[Hash[action, Bitmap::bitmap?]] another_matrixes) -> bool
def merge_matrix(another_matrixes)
changed = false
another_matrixes.each do |another_matrix|
another_matrix.each do |action, another_contributions|
unless @contribution_matrix.key?(action)
@contribution_matrix[action] = another_contributions
changed = true
next
end
contributions = @contribution_matrix[action]
next if contributions.nil?

if another_contributions.nil?
@contribution_matrix[action] = nil
changed = true
else
merged = contributions | another_contributions
changed = true if merged != contributions
@contribution_matrix[action] = merged
end
end
end
changed
end

# Definition 3.42 (dominant_contribution)
#
# @rbs (State::lookahead_set lookaheads, Array[Item] kernels) -> Array[action]?
Expand Down
4 changes: 3 additions & 1 deletion lib/lrama/states.rb
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,8 @@ def compute_inadequacy_annotations
queue = @states.reject {|state| state.annotation_list.empty? }
queued = Set.new(queue.map(&:id))
propagated = {} #: Hash[State::InadequacyAnnotation, Hash[State::InadequacyAnnotation::action, Bitmap::bitmap?]]
# Scope insertion indexes to this pass, while contribution matrices are fixed.
indexes = {} #: State::annotation_indexes

while (curr = queue.shift) do
queued.delete(curr.id)
Expand All @@ -771,7 +773,7 @@ def compute_inadequacy_annotations
true
end
curr.predecessors.each do |pred|
changed = curr.annotate_predecessor(pred, annotations)
changed = curr.annotate_predecessor(pred, annotations, indexes)
queue << pred if changed && queued.add?(pred.id)
end
end
Expand Down
12 changes: 8 additions & 4 deletions sig/generated/lrama/state.rbs

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

3 changes: 0 additions & 3 deletions sig/generated/lrama/state/inadequacy_annotation.rbs

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

36 changes: 36 additions & 0 deletions spec/fixtures/ielr/distinct_annotation_contributions.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
%define lr.type ielr
%right 'b'
%right HIGH
%define api.pure full
%locations
%code provides {
static int yylex(YYSTYPE *, YYLTYPE *);
static void yyerror(YYLTYPE *, const char *);
}
%%
start: expr;
empty: %empty;
expr: expr tail 'a' tail | %empty | nested 'c';
tail: 'b' | %empty %prec HIGH | 'b' nested empty;
prefix: %empty;
suffix: %empty;
nested: suffix prefix | expr tail 'b' tail;
%%
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 "preserves distinct IELR annotation contributions through nullable recursive rules" do
Dir.mktmpdir do |directory|
c_path = File.join(directory, "distinct_annotations#{file_extension}")
obj_path = File.join(directory, "distinct_annotations")
generate_object(fixture_path("ielr/distinct_annotation_contributions.y"), c_path, obj_path)

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

it "accepts all paths through nested IELR rules" do
Dir.mktmpdir do |directory|
c_path = File.join(directory, "nested#{file_extension}")
Expand Down
Loading
Loading