Class: Dendroid::Syntax::Production

Inherits:
Rule
  • Object
show all
Defined in:
lib/dendroid/syntax/production.rb

Overview

A specialization of the Rule class. A production is a rule with a single rhs

Instance Attribute Summary collapse

Attributes inherited from Rule

#head

Instance Method Summary collapse

Methods inherited from Rule

#nonterminals, #rhs_symbols, #terminals

Constructor Details

#initialize(lhs, rhs) ⇒ Production

Create a Production instance.



16
17
18
19
# File 'lib/dendroid/syntax/production.rb', line 16

def initialize(lhs, rhs)
  super(lhs)
  @body = valid_body(rhs)
end

Instance Attribute Details

#bodyDendroid::Syntax::SymbolSeq (readonly)



11
12
13
# File 'lib/dendroid/syntax/production.rb', line 11

def body
  @body
end

Instance Method Details

#==(other) ⇒ Boolean

Equality operator Two production rules are equal when their head and rhs are equal.



62
63
64
65
66
# File 'lib/dendroid/syntax/production.rb', line 62

def ==(other)
  return true if equal?(other)

  (head == other.head) && (body == other.body)
end

#choice?FalseClass

Predicate method to check whether the rule has alternatives



29
30
31
# File 'lib/dendroid/syntax/production.rb', line 29

def choice?
  false
end

#empty?Boolean

Predicate method to check whether the rule body (its rhs) is empty.



23
24
25
# File 'lib/dendroid/syntax/production.rb', line 23

def empty?
  body.empty?
end

#non_productiveObject

Mark the production rule as non-productive.



49
50
51
# File 'lib/dendroid/syntax/production.rb', line 49

def non_productive
  self.productive = false
end

#productive?Boolean, NilClass

Predicate method to check whether the production rule body is productive. It is productive when it is empty or all of its rhs members are productive too.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dendroid/syntax/production.rb', line 36

def productive?
  if @productive.nil?
    if body.productive?
      self.productive = true
    else
      nil
    end
  else
    @productive
  end
end

#rhsArray<Dendroid::Syntax::SymbolSeq>

Returns an array with the symbol sequence of its rhs



70
71
72
# File 'lib/dendroid/syntax/production.rb', line 70

def rhs
  [body]
end

#to_sString

Return the text representation of the production rule



55
56
57
# File 'lib/dendroid/syntax/production.rb', line 55

def to_s
  "#{head} => #{body}"
end