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 the unique rule for the non-terminal symbol at its left-hand side (lhs).

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.

Parameters:



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

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

Instance Attribute Details

#bodyDendroid::Syntax::SymbolSeq (readonly)



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

def body
  @body
end

Instance Method Details

#==(other) ⇒ Boolean

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

Returns:

  • (Boolean)


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

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

Returns:

  • (FalseClass)


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

def choice?
  false
end

#empty?Boolean

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

Returns:

  • (Boolean)


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

def empty?
  body.empty?
end

#non_productiveObject

Mark the production rule as non-productive.



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

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.

Returns:

  • (Boolean, NilClass)


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

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

Returns:



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

def rhs
  [body]
end

#to_sString

Return the text representation of the production rule

Returns:

  • (String)


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

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