Class: RLTK::CFG::Production
- Inherits:
-
Object
- Object
- RLTK::CFG::Production
- Defined in:
- lib/rltk/cfg.rb
Overview
Oddly enough, the Production class represents a production in a context-free grammar.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#id ⇒ Integer
readonly
ID of this production.
-
#lhs ⇒ Symbol
readonly
Left-hand side of this production.
-
#rhs ⇒ Array<Symbol>
readonly
Right-hand side of this production.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Comparese on production to another.
-
#copy ⇒ Production
A new copy of this production.
-
#initialize(id, lhs, rhs) ⇒ Production
constructor
Instantiates a new Production object with the specified ID, and left- and right-hand sides.
-
#last_terminal ⇒ Symbol
The last terminal in the right-hand side of the production.
-
#to_item ⇒ Item
An Item based on this production.
-
#to_s(padding = 0) ⇒ String
Returns a string representation of this production.
Constructor Details
#initialize(id, lhs, rhs) ⇒ Production
Instantiates a new Production object with the specified ID, and left- and right-hand sides.
578 579 580 581 582 |
# File 'lib/rltk/cfg.rb', line 578 def initialize(id, lhs, rhs) @id = id @lhs = lhs @rhs = rhs end |
Instance Attribute Details
#id ⇒ Integer (readonly)
Returns ID of this production.
564 565 566 |
# File 'lib/rltk/cfg.rb', line 564 def id @id end |
#lhs ⇒ Symbol (readonly)
Returns Left-hand side of this production.
567 568 569 |
# File 'lib/rltk/cfg.rb', line 567 def lhs @lhs end |
#rhs ⇒ Array<Symbol> (readonly)
Returns Right-hand side of this production.
570 571 572 |
# File 'lib/rltk/cfg.rb', line 570 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.
590 591 592 |
# File 'lib/rltk/cfg.rb', line 590 def ==(other) self.lhs == other.lhs and self.rhs == other.rhs end |
#copy ⇒ Production
Returns A new copy of this production.
595 596 597 |
# File 'lib/rltk/cfg.rb', line 595 def copy Production.new(@id, @lhs, @rhs.clone) end |
#last_terminal ⇒ Symbol
Returns The last terminal in the right-hand side of the production.
600 601 602 |
# 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 ⇒ Item
Returns An Item based on this production.
605 606 607 |
# File 'lib/rltk/cfg.rb', line 605 def to_item Item.new(0, @id, @lhs, @rhs) end |
#to_s(padding = 0) ⇒ String
Returns a string representation of this production.
614 615 616 |
# 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 |