Class: RLTK::CFG::Production

Inherits:
Object
  • Object
show all
Defined in:
lib/rltk/cfg.rb

Overview

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

Direct Known Subclasses

Item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, lhs, rhs) ⇒ Production

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

Parameters:

  • id (Integer)

    ID number of this production.

  • lhs (Symbol)

    Left-hand side of the production.

  • rhs (Array<Symbol>)

    Right-hand side of the production.



524
525
526
527
528
# File 'lib/rltk/cfg.rb', line 524

def initialize(id, lhs, rhs)
	@id	= id
	@lhs	= lhs
	@rhs	= rhs
end

Instance Attribute Details

#idInteger (readonly)

Returns ID of this production.

Returns:

  • (Integer)

    ID of this production.



510
511
512
# File 'lib/rltk/cfg.rb', line 510

def id
  @id
end

#lhsSymbol (readonly)

Returns Left-hand side of this production.

Returns:

  • (Symbol)

    Left-hand side of this production.



513
514
515
# File 'lib/rltk/cfg.rb', line 513

def lhs
  @lhs
end

#rhsArray<Symbol> (readonly)

Returns Right-hand side of this production.

Returns:

  • (Array<Symbol>)

    Right-hand side of this production.



516
517
518
# File 'lib/rltk/cfg.rb', line 516

def rhs
  @rhs
end

Instance Method Details

#==(other) ⇒ Boolean

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

Parameters:

  • other (Production)

    Another production to compare to.

Returns:

  • (Boolean)


536
537
538
# File 'lib/rltk/cfg.rb', line 536

def ==(other)
	self.lhs == other.lhs and self.rhs == other.rhs
end

#copyProduction

Returns A new copy of this production.

Returns:



541
542
543
# File 'lib/rltk/cfg.rb', line 541

def copy
	Production.new(@id, @lhs, @rhs.clone)
end

#last_terminalSymbol

Returns The last terminal in the right-hand side of the production.

Returns:

  • (Symbol)

    The last terminal in the right-hand side of the production.



546
547
548
# File 'lib/rltk/cfg.rb', line 546

def last_terminal
	@rhs.inject(nil) { |m, sym| if CFG::is_terminal?(sym) then sym else m end }
end

#to_itemItem

Returns An Item based on this production.

Returns:

  • (Item)

    An Item based on this production.



551
552
553
# File 'lib/rltk/cfg.rb', line 551

def to_item
	Item.new(0, @id, @lhs, @rhs)
end

#to_s(padding = 0) ⇒ String

Returns a string representation of this production.

Parameters:

  • padding (Integer) (defaults to: 0)

    The ammount of padding spaces to add to the beginning of the string.

Returns:

  • (String)


560
561
562
# File 'lib/rltk/cfg.rb', line 560

def to_s(padding = 0)
	"#{format("%-#{padding}s", @lhs)} -> #{@rhs.empty? ? 'ɛ' : @rhs.map { |s| s.to_s }.join(' ')}"
end