class RLTK::CFG::Production
Oddly enough, the Production
class represents a production in a context-free grammar.
Attributes
@return [Integer] ID of this production.
@return [Symbol] Left-hand side of this production.
@return [Array<Symbol>] Right-hand side of this production.
Public Class Methods
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
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
@return [Production] A new copy of this production.
# File lib/rltk/cfg.rb, line 595 def copy Production.new(@id, @lhs, @rhs.clone) end
@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
@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
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