Skip to content

Commit 3c82f25

Browse files
committed
Render inadequacy annotation of each state on output file
1 parent 1cd2e53 commit 3c82f25

File tree

5 files changed

+67
-6
lines changed

5 files changed

+67
-6
lines changed

lib/lrama/reporter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def report(io, states)
2929
@terms.report(io, states)
3030
@conflicts.report(io, states)
3131
@grammar.report(io, states)
32-
@states.report(io, states)
32+
@states.report(io, states, ielr: states.ielr_defined?)
3333
end
3434
end
3535
end

lib/lrama/reporter/states.rb

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def initialize(itemsets: false, lookaheads: false, solved: false, counterexample
1313
@verbose = verbose
1414
end
1515

16-
# @rbs (IO io, Lrama::States states) -> void
17-
def report(io, states)
16+
# @rbs (IO io, Lrama::States states, ielr: bool) -> void
17+
def report(io, states, ielr: false)
1818
if @counterexamples
1919
cex = Counterexamples.new(states)
2020
end
@@ -179,6 +179,43 @@ def report(io, states)
179179
end
180180
end
181181

182+
if ielr && !state.annotation_list.empty?
183+
state.annotation_list.each do |annotation|
184+
# binding.irb
185+
io << " inadequacy annotation manifesting state #{annotation.state.id}, token #{annotation.token.id.s_value}\n"
186+
187+
annotation.contribution_matrix.each do |action, contributions|
188+
case action
189+
when State::Shift
190+
io << " state #{state.id} always contibutes to shift by #{action.next_sym.id.s_value}\n"
191+
when State::Reduce
192+
reduce_rule = action.item.rule
193+
reduce_rule_text = "#{reduce_rule.lhs.display_name}: #{reduce_rule.rhs.map(&:display_name).join(" ")}"
194+
195+
if contributions
196+
contributions.each do |item, contribution|
197+
if item.empty_rule?
198+
r = "ε •"
199+
else
200+
r = item.rhs.map(&:display_name).insert(item.position, "•").join(" ")
201+
end
202+
203+
if contribution
204+
io << " '#{r}' potentially contibutes to reduce by '#{reduce_rule_text}'\n"
205+
else
206+
io << " '#{r}' never contibutes to reduce by '#{reduce_rule_text}'\n"
207+
end
208+
end
209+
else
210+
io << " state #{state.id} always contibutes to reduce by '#{reduce_rule_text}'\n"
211+
end
212+
end
213+
end
214+
end
215+
216+
io << "\n"
217+
end
218+
182219
if @verbose
183220
# Report direct_read_sets
184221
io << " [Direct Read sets]\n"

lib/lrama/states.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class States
1414
include Lrama::Tracer::Duration
1515

1616
def_delegators "@grammar", :symbols, :terms, :nterms, :rules,
17-
:accept_symbol, :eof_symbol, :undef_symbol, :find_symbol_by_s_value!
17+
:accept_symbol, :eof_symbol, :undef_symbol, :find_symbol_by_s_value!, :ielr_defined?
1818

1919
attr_reader :states, :reads_relation, :includes_relation, :lookback_relation
2020

sig/generated/lrama/reporter/states.rbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module Lrama
66
# @rbs (itemsets: bool, lookaheads: bool, solved: bool, counterexamples: bool, verbose: bool, **untyped _) -> void
77
def initialize: (itemsets: bool, lookaheads: bool, solved: bool, counterexamples: bool, verbose: bool, **untyped _) -> void
88

9-
# @rbs (IO io, Lrama::States states) -> void
10-
def report: (IO io, Lrama::States states) -> void
9+
# @rbs (IO io, Lrama::States states, ielr: bool) -> void
10+
def report: (IO io, Lrama::States states, ielr: bool) -> void
1111
end
1212
end
1313
end

spec/lrama/states_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,10 @@ class go to state 5
18241824
18251825
S go to state 3
18261826
1827+
inadequacy annotation manifesting state 14, token a
1828+
state 0 always contibutes to shift by a
1829+
state 0 always contibutes to reduce by 'E: '
1830+
18271831
18281832
State 1
18291833
@@ -1833,6 +1837,10 @@ class go to state 5
18331837
18341838
A go to state 5
18351839
1840+
inadequacy annotation manifesting state 14, token a
1841+
state 1 always contibutes to shift by a
1842+
state 1 always contibutes to reduce by 'E: '
1843+
18361844
18371845
State 2
18381846
@@ -1842,6 +1850,10 @@ class go to state 5
18421850
18431851
A go to state 6
18441852
1853+
inadequacy annotation manifesting state 14, token a
1854+
state 2 always contibutes to shift by a
1855+
'b • A B b' never contibutes to reduce by 'E: '
1856+
18451857
18461858
State 3
18471859
@@ -1859,6 +1871,10 @@ class go to state 5
18591871
C go to state 9
18601872
D go to state 10
18611873
1874+
inadequacy annotation manifesting state 14, token a
1875+
state 4 always contibutes to shift by a
1876+
'a • C D E' potentially contibutes to reduce by 'E: '
1877+
18621878
18631879
State 5
18641880
@@ -1904,6 +1920,10 @@ class go to state 5
19041920
19051921
D go to state 14
19061922
1923+
inadequacy annotation manifesting state 14, token a
1924+
state 9 always contibutes to shift by a
1925+
'a C • D E' potentially contibutes to reduce by 'E: '
1926+
19071927
19081928
State 10
19091929
@@ -1941,6 +1961,10 @@ class go to state 5
19411961
19421962
E go to state 18
19431963
1964+
inadequacy annotation manifesting state 14, token a
1965+
state 14 always contibutes to shift by a
1966+
'a C D • E' potentially contibutes to reduce by 'E: '
1967+
19441968
19451969
State 15
19461970

0 commit comments

Comments
 (0)