Class: Dendroid::Syntax::Production
- 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
- #body ⇒ Dendroid::Syntax::SymbolSeq readonly
Attributes inherited from Rule
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Equality operator Two production rules are equal when their head and rhs are equal.
-
#choice? ⇒ FalseClass
Predicate method to check whether the rule has alternatives.
-
#empty? ⇒ Boolean
Predicate method to check whether the rule body (its rhs) is empty.
-
#initialize(lhs, rhs) ⇒ Production
constructor
Create a Production instance.
-
#non_productive ⇒ Object
Mark the production rule as non-productive.
-
#productive? ⇒ Boolean, NilClass
Predicate method to check whether the production rule body is productive.
-
#rhs ⇒ Array<Dendroid::Syntax::SymbolSeq>
Returns an array with the symbol sequence of its rhs.
-
#to_s ⇒ String
Return the text representation of the production rule.
Methods inherited from Rule
#nonterminals, #rhs_symbols, #terminals
Constructor Details
#initialize(lhs, rhs) ⇒ Production
Create a Production instance.
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
#body ⇒ Dendroid::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.
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
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.
24 25 26 |
# File 'lib/dendroid/syntax/production.rb', line 24 def empty? body.empty? end |
#non_productive ⇒ Object
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.
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 |
#rhs ⇒ Array<Dendroid::Syntax::SymbolSeq>
Returns an array with the symbol sequence of its rhs
71 72 73 |
# File 'lib/dendroid/syntax/production.rb', line 71 def rhs [body] end |
#to_s ⇒ String
Return the text representation of the production rule
56 57 58 |
# File 'lib/dendroid/syntax/production.rb', line 56 def to_s "#{head} => #{body}" end |