class RLTK::CFG::Item

The Item class represents a CFG production with dot in it.

Attributes

dot[R]

@return [Integer] Index of the next symbol in this item.

Public Class Methods

new(dot, *args) click to toggle source

Instantiates a new Item object with a dot located before the symbol at index dot of the right-hand side. The remaining arguments (args) should be as specified by {Production#initialize}.

@param [Integer] dot Location of the dot in this Item. @param [Array<Object>] args (see {Production#initialize})

Calls superclass method RLTK::CFG::Production::new
# File lib/rltk/cfg.rb, line 631
def initialize(dot, *args)
        super(*args)

        # The Dot indicates the NEXT symbol to be read.
        @dot = dot
end

Public Instance Methods

==(other) click to toggle source

Compares two items.

@param [Item] other Another item to compare to.

@return [Boolean]

# File lib/rltk/cfg.rb, line 643
def ==(other)
        self.dot == other.dot and self.lhs == other.lhs and self.rhs == other.rhs
end
advance() click to toggle source

Moves the items dot forward by one if the end of the right-hand side hasn’t already been reached.

@return [Integer, nil]

# File lib/rltk/cfg.rb, line 651
def advance
        if @dot < @rhs.length
                @dot += 1
        end
end
at_end?() click to toggle source

Tests to see if the dot is at the end of the right-hand side.

@return [Boolean]

# File lib/rltk/cfg.rb, line 660
def at_end?
        @dot == @rhs.length
end
copy() click to toggle source

@return [Item] A new copy of this item.

# File lib/rltk/cfg.rb, line 665
def copy
        Item.new(@dot, @id, @lhs, @rhs.clone)
end
next_symbol() click to toggle source

Returns the symbol located after the dot.

@return [Symbol] Symbol located after the dot (at the index indicated by the {#dot} attribute).

# File lib/rltk/cfg.rb, line 672
def next_symbol
        @rhs[@dot]
end
to_s(padding = 0) click to toggle source

Returns a string representation of this item.

@param [Integer] padding The ammount of padding spaces to add to the beginning of the string.

@return [String]

# File lib/rltk/cfg.rb, line 681
def to_s(padding = 0)
        "#{format("%-#{padding}s", @lhs)} -> #{@rhs.map { |s| s.to_s }.insert(@dot, '·').join(' ') }"
end