Class: Dendroid::Syntax::Choice

Inherits:
Rule
  • Object
show all
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

#head

Instance Method Summary collapse

Methods inherited from Rule

#nonterminals, #rhs_symbols, #terminals

Constructor Details

#initialize(lhs, alt) ⇒ Choice

Create a Choice instance.

Parameters:



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

#alternativesArray<Dendroid::Syntax::SymbolSeq> (readonly)

Returns:



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.

Returns:

  • (Boolean)


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

Returns:

  • (TrueClass)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

#rhsArray<Dendroid::Syntax::SymbolSeq>

Returns an array with the symbol sequence of its alternatives

Returns:



52
53
54
# File 'lib/dendroid/syntax/choice.rb', line 52

def rhs
  alternatives
end

#to_sString

Return the text representation of the choice

Returns:

  • (String)


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

def to_s
  "#{head} => #{alternatives.join(' | ')}"
end