Start on Item and ItemSet

This commit is contained in:
josh committed 2021-08-28 09:02:19 -04:00
1 parent 6ce94e15af
commit 2e16b0bd6e
5 files changed
+86

No files matched your search

+6
View File
@@ -3,6 +3,12 @@ class Imbecile
class Parser
def initialize(tokens, rules)
start_rules = rules.select {|rule| rule.name == "Start"}
start_items = start_rules.map do |rule|
Item.new(rule, 0)
end
start_item_set = ItemSet.new(start_items)
start_item_set.close!
end
end
+33
View File
@@ -0,0 +1,33 @@
class Imbecile
class Parser
class Item
attr_reader :rule
attr_reader :position
def initialize(rule, position)
@rule = rule
@position = position
end
def next_component
@rule.components[@position]
end
def hash
[@rule, @position].hash
end
def ==(other)
@rule == other.rule && @position == other.position
end
def eql?(other)
self == other
end
end
end
end
+26
View File
@@ -0,0 +1,26 @@
class Imbecile
class Parser
class ItemSet
def initialize(items)
@items = Set.new(items)
end
def close!
eval_items = @items
while eval_items.size > 0
this_eval_items = eval_items
eval_items = Set.new
this_eval_items.each do |item|
if item.next_component.is_a?(Rule)
end
end
@items += eval_items
end
end
end
end
end