class RLTK::CFG::Production

Oddly enough, the Production class represents a production in a context-free grammar.

Attributes

id[R]

@return [Integer] ID of this production.

lhs[R]

@return [Symbol] Left-hand side of this production.

rhs[R]

@return [Array<Symbol>] Right-hand side of this production.

Public Class Methods

new(id, lhs, rhs) click to toggle source

Instantiates a new Production object with the specified ID, and left- and right-hand sides.

@param [Integer] id ID number of this production. @param [Symbol] lhs Left-hand side of the production. @param [Array<Symbol>] rhs Right-hand side of the production.

# File lib/rltk/cfg.rb, line 578
def initialize(id, lhs, rhs)
        @id = id
        @lhs        = lhs
        @rhs        = rhs
end

Public Instance Methods

==(other) click to toggle source

Comparese on production to another. Returns true only if the left- and right- hand sides match.

@param [Production] other Another production to compare to.

@return [Boolean]

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

@return [Production] A new copy of this production.

# File lib/rltk/cfg.rb, line 595
def copy
        Production.new(@id, @lhs, @rhs.clone)
end
last_terminal() click to toggle source

@return [Symbol] The last terminal in the right-hand side of the production.

# File lib/rltk/cfg.rb, line 600
def last_terminal
        @rhs.inject(nil) { |m, sym| if CFG::is_terminal?(sym) then sym else m end }
end
to_item() click to toggle source

@return [Item] An Item based on this production.

# File lib/rltk/cfg.rb, line 605
def to_item
        Item.new(0, @id, @lhs, @rhs)
end
to_s(padding = 0) click to toggle source

Returns a string representation of this production.

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

@return [String]

# File lib/rltk/cfg.rb, line 614
def to_s(padding = 0)
        "#{format("%-#{padding}s", @lhs)} -> #{@rhs.empty? ? 'ɛ' : @rhs.map { |s| s.to_s }.join(' ')}"
end