Skip to content

Commit

Permalink
Merge pull request #354 from ydah/refactor
Browse files Browse the repository at this point in the history
Refactor to simplify the code
  • Loading branch information
yui-knk authored Jan 25, 2024
2 parents c7c9725 + 7c2e580 commit d62cebb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/lrama/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def yysymbol_kind_t
def yyfinal
@states.states.find do |state|
state.items.find do |item|
item.rule.lhs.accept_symbol? && item.end_of_rule?
item.lhs.accept_symbol? && item.end_of_rule?
end
end.id
end
Expand Down
2 changes: 1 addition & 1 deletion lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def nterms
def compute_nullable
@rules.each do |rule|
case
when rule.rhs.empty?
when rule.empty_rule?
rule.nullable = true
when rule.rhs.any?(&:term)
rule.nullable = false
Expand Down
4 changes: 2 additions & 2 deletions lib/lrama/grammar/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ def ==(other)
# TODO: Change this to display_name
def to_s
l = lhs.id.s_value
r = rhs.empty? ? "ε" : rhs.map {|r| r.id.s_value }.join(", ")
r = empty_rule? ? "ε" : rhs.map {|r| r.id.s_value }.join(", ")

"#{l} -> #{r}"
end

# Used by #user_actions
def as_comment
l = lhs.id.s_value
r = rhs.empty? ? "%empty" : rhs.map(&:display_name).join(" ")
r = empty_rule? ? "%empty" : rhs.map(&:display_name).join(" ")

"#{l}: #{r}"
end
Expand Down
30 changes: 17 additions & 13 deletions lib/lrama/states/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class States
class Item < Struct.new(:rule, :position, keyword_init: true)
# Optimization for States#setup_state
def hash
[rule.id, position].hash
[rule_id, position].hash
end

def rule_id
Expand All @@ -17,62 +17,66 @@ def empty_rule?
end

def number_of_rest_symbols
rule.rhs.count - position
rhs.count - position
end

def lhs
rule.lhs
end

def rhs
rule.rhs
end

def next_sym
rule.rhs[position]
rhs[position]
end

def next_next_sym
rule.rhs[position + 1]
rhs[position + 1]
end

def previous_sym
rule.rhs[position - 1]
rhs[position - 1]
end

def end_of_rule?
rule.rhs.count == position
rhs.count == position
end

def beginning_of_rule?
position == 0
end

def start_item?
rule.id == 0 && position == 0
rule.initial_rule? && beginning_of_rule?
end

def new_by_next_position
Item.new(rule: rule, position: position + 1)
end

def symbols_before_dot
rule.rhs[0...position]
rhs[0...position]
end

def symbols_after_dot
rule.rhs[position..-1]
rhs[position..-1]
end

def to_s
"#{lhs.id.s_value}: #{display_name}"
end

def display_name
r = rule.rhs.map(&:display_name).insert(position, "•").join(" ")
"#{r} (rule #{rule.id})"
r = rhs.map(&:display_name).insert(position, "•").join(" ")
"#{r} (rule #{rule_id})"
end

# Right after position
def display_rest
r = rule.rhs[position..-1].map(&:display_name).join(" ")
". #{r} (rule #{rule.id})"
r = rhs[position..-1].map(&:display_name).join(" ")
". #{r} (rule #{rule_id})"
end
end
end
Expand Down
18 changes: 8 additions & 10 deletions lib/lrama/states_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def report_grammar(io)
last_lhs = nil

@states.rules.each do |rule|
if rule.rhs.empty?
if rule.empty_rule?
r = "ε"
else
r = rule.rhs.map(&:display_name).join(" ")
Expand Down Expand Up @@ -84,17 +84,15 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
last_lhs = nil
list = itemsets ? state.items : state.kernels
list.sort_by {|i| [i.rule_id, i.position] }.each do |item|
rule = item.rule
position = item.position
if rule.rhs.empty?
if item.empty_rule?
r = "ε •"
else
r = rule.rhs.map(&:display_name).insert(position, "•").join(" ")
r = item.rhs.map(&:display_name).insert(item.position, "•").join(" ")
end
if rule.lhs == last_lhs
l = " " * rule.lhs.id.s_value.length + "|"
if item.lhs == last_lhs
l = " " * item.lhs.id.s_value.length + "|"
else
l = rule.lhs.id.s_value + ":"
l = item.lhs.id.s_value + ":"
end
la = ""
if lookaheads && item.end_of_rule?
Expand All @@ -104,9 +102,9 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
la = " [#{look_ahead.map(&:display_name).join(", ")}]"
end
end
last_lhs = rule.lhs
last_lhs = item.lhs

io << sprintf("%5i %s %s%s\n", rule.id, l, r, la)
io << sprintf("%5i %s %s%s\n", item.rule_id, l, r, la)
end
io << "\n"

Expand Down

0 comments on commit d62cebb

Please sign in to comment.