Class: Dendroid::Syntax::Choice
- Defined in:
- lib/dendroid/syntax/choice.rb
Overview
A specialization of the Rule class. A choice is a rule with multiple rhs
Instance Attribute Summary collapse
Attributes inherited from Rule
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Equality operator Two production rules are equal when their head and alternatives are equal.
-
#choice? ⇒ TrueClass
Predicate method to check whether the rule has alternatives.
-
#empty? ⇒ Boolean
Predicate method to check whether the rule has at least one empty alternative.
-
#initialize(lhs, alt) ⇒ Choice
constructor
Create a Choice instance.
-
#productive? ⇒ Boolean
Predicate method to check whether the choice rule body is productive.
-
#rhs ⇒ Array<Dendroid::Syntax::SymbolSeq>
Returns an array with the symbol sequence of its alternatives.
-
#to_s ⇒ String
Return the text representation of the choice.
Methods inherited from Rule
#nonterminals, #rhs_symbols, #terminals
Constructor Details
#initialize(lhs, alt) ⇒ Choice
Create a Choice instance.
16 17 18 19 |
# File 'lib/dendroid/syntax/choice.rb', line 16 def initialize(lhs, alt) super(lhs) @alternatives = valid_alternatives(alt) end |
Instance Attribute Details
#alternatives ⇒ Array<Dendroid::Syntax::SymbolSeq> (readonly)
11 12 13 |
# File 'lib/dendroid/syntax/choice.rb', line 11 def alternatives @alternatives end |
Instance Method Details
#==(other) ⇒ Boolean
Equality operator Two production rules are equal when their head and alternatives are equal.
59 60 61 62 63 64 |
# File 'lib/dendroid/syntax/choice.rb', line 59 def ==(other) return true if equal?(other) return false if other.is_a?(Production) (head == other.head) && (alternatives == other.alternatives) end |
#choice? ⇒ TrueClass
Predicate method to check whether the rule has alternatives
23 24 25 |
# File 'lib/dendroid/syntax/choice.rb', line 23 def choice? true end |
#empty? ⇒ Boolean
Predicate method to check whether the rule has at least one empty alternative.
46 47 48 |
# File 'lib/dendroid/syntax/choice.rb', line 46 def empty? alternatives.any?(&:empty?) end |
#productive? ⇒ Boolean
Predicate method to check whether the choice rule body is productive. It is productive when at least of its alternative is productive.
36 37 38 39 40 41 42 |
# File 'lib/dendroid/syntax/choice.rb', line 36 def productive? productive_alts = alternatives.select(&:productive?) return false if productive_alts.empty? @productive = Set.new(productive_alts) head.productive = true end |
#rhs ⇒ Array<Dendroid::Syntax::SymbolSeq>
Returns an array with the symbol sequence of its alternatives
52 53 54 |
# File 'lib/dendroid/syntax/choice.rb', line 52 def rhs alternatives end |
#to_s ⇒ String
Return the text representation of the choice
29 30 31 |
# File 'lib/dendroid/syntax/choice.rb', line 29 def to_s "#{head} => #{alternatives.join(' | ')}" end |