summaryrefslogtreecommitdiff
path: root/tool/lrama/lib/lrama/context.rb
diff options
context:
space:
mode:
Diffstat (limited to 'tool/lrama/lib/lrama/context.rb')
-rw-r--r--tool/lrama/lib/lrama/context.rb60
1 files changed, 23 insertions, 37 deletions
diff --git a/tool/lrama/lib/lrama/context.rb b/tool/lrama/lib/lrama/context.rb
index 9086470011..32017e65fc 100644
--- a/tool/lrama/lib/lrama/context.rb
+++ b/tool/lrama/lib/lrama/context.rb
@@ -1,4 +1,4 @@
-require "lrama/report"
+require "lrama/report/duration"
module Lrama
# This is passed to a template
@@ -9,7 +9,7 @@ module Lrama
BaseMin = -Float::INFINITY
# TODO: It might be better to pass `states` to Output directly?
- attr_reader :states
+ attr_reader :states, :yylast, :yypact_ninf, :yytable_ninf, :yydefact, :yydefgoto
def initialize(states)
@states = states
@@ -41,15 +41,11 @@ module Lrama
def yyfinal
@states.states.find do |state|
state.items.find do |item|
- item.rule.lhs.id.s_value == "$accept" && item.end_of_rule?
+ item.lhs.accept_symbol? && item.end_of_rule?
end
end.id
end
- def yylast
- @yylast
- end
-
# Number of terms
def yyntokens
@states.terms.count
@@ -89,6 +85,16 @@ module Lrama
return a
end
+ def yytranslate_inverted
+ a = Array.new(@states.symbols.count, @states.undef_symbol.token_id)
+
+ @states.terms.each do |term|
+ a[term.number] = term.token_id
+ end
+
+ return a
+ end
+
# Mapping from rule number to line number of the rule is defined.
# Dummy rule is appended as the first element whose value is 0
# because 0 means error in yydefact.
@@ -109,30 +115,14 @@ module Lrama
end
end
- def yypact_ninf
- @yypact_ninf
- end
-
- def yytable_ninf
- @yytable_ninf
- end
-
def yypact
@base[0...yynstates]
end
- def yydefact
- @yydefact
- end
-
def yypgoto
@base[yynstates..-1]
end
- def yydefgoto
- @yydefgoto
- end
-
def yytable
@table
end
@@ -160,7 +150,7 @@ module Lrama
return a
end
- # Mapping from rule number to lenght of RHS.
+ # Mapping from rule number to length of RHS.
# Dummy rule is appended as the first element whose value is 0
# because 0 means error in yydefact.
def yyr2
@@ -204,7 +194,7 @@ module Lrama
(rule_id + 1) * -1
end
- # Symbol number is assinged to term first then nterm.
+ # Symbol number is assigned to term first then nterm.
# This method calculates sequence_number for nterm.
def nterm_number_to_sequence_number(nterm_number)
nterm_number - @states.terms.count
@@ -231,7 +221,7 @@ module Lrama
if state.reduces.map(&:selected_look_ahead).any? {|la| !la.empty? }
# Iterate reduces with reverse order so that first rule is used.
- state.reduces.reverse.each do |reduce|
+ state.reduces.reverse_each do |reduce|
reduce.look_ahead.each do |term|
actions[term.number] = rule_id_to_action_number(reduce.rule.id)
end
@@ -249,7 +239,7 @@ module Lrama
actions[conflict.symbol.number] = ErrorActionNumber
end
- # If default_reduction_rule, replase default_reduction_rule in
+ # If default_reduction_rule, replace default_reduction_rule in
# actions with zero.
if state.default_reduction_rule
actions.map! do |e|
@@ -262,7 +252,7 @@ module Lrama
end
# If no default_reduction_rule, default behavior is an
- # error then replase ErrorActionNumber with zero.
+ # error then replace ErrorActionNumber with zero.
if !state.default_reduction_rule
actions.map! do |e|
if e == ErrorActionNumber
@@ -275,9 +265,9 @@ module Lrama
s = actions.each_with_index.map do |n, i|
[i, n]
- end.select do |i, n|
+ end.reject do |i, n|
# Remove default_reduction_rule entries
- n != 0
+ n == 0
end
if s.count != 0
@@ -299,10 +289,8 @@ module Lrama
# Index is sequence number of nterm, value is state id
# of a default nterm transition destination.
@yydefgoto = Array.new(@states.nterms.count, 0)
- h = {}
# Mapping from nterm to next_states
nterm_to_next_states = {}
- terms_count = @states.terms.count
@states.states.each do |state|
state.nterm_transitions.each do |shift, next_state|
@@ -359,7 +347,7 @@ module Lrama
end
j = @sorted_actions.count - 1
- state_id, froms_and_tos, count, width = action
+ _state_id, _froms_and_tos, count, width = action
while (j >= 0) do
case
@@ -391,7 +379,6 @@ module Lrama
end
print sprintf("]\n\n")
-
print sprintf("width [\n")
vectors_count.times do |i|
print sprintf("%d, ", ary[i] ? ary[i][3] : 0)
@@ -399,7 +386,6 @@ module Lrama
end
print sprintf("]\n\n")
-
print sprintf("tally [\n")
vectors_count.times do |i|
print sprintf("%d, ", ary[i] ? ary[i][2] : 0)
@@ -476,7 +462,7 @@ module Lrama
@yylast = high
# replace_ninf
- @yypact_ninf = (@base.select {|i| i != BaseMin } + [0]).min - 1
+ @yypact_ninf = (@base.reject {|i| i == BaseMin } + [0]).min - 1
@base.map! do |i|
case i
when BaseMin
@@ -486,7 +472,7 @@ module Lrama
end
end
- @yytable_ninf = (@table.compact.select {|i| i != ErrorActionNumber } + [0]).min - 1
+ @yytable_ninf = (@table.compact.reject {|i| i == ErrorActionNumber } + [0]).min - 1
@table.map! do |i|
case i
when nil