Class: Rley::Parser::StateSet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rley/parser/state_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStateSet

Returns a new instance of StateSet.



14
15
16
# File 'lib/rley/parser/state_set.rb', line 14

def initialize()
  @states = []
end

Instance Attribute Details

#statesObject (readonly)

The set of parse states



12
13
14
# File 'lib/rley/parser/state_set.rb', line 12

def states
  @states
end

Instance Method Details

#ambiguitiesObject

Return an Array of Arrays of ambiguous parse states.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rley/parser/state_set.rb', line 74

def ambiguities()
  complete_states = states.select(&:complete?)
  return [] if complete_states.size <= 1

  # Group parse state by lhs symbol and origin
  groupings = complete_states.group_by do |st|
    st.dotted_rule.lhs.object_id.to_s
  end

  # Retain the groups having more than one element.
  ambiguous_groups = []
  groupings.each_value do |a_group|
    ambiguous_groups << a_group if a_group.size > 1
  end

  return ambiguous_groups
end

#expected_terminalsObject

The list of distinct expected terminal symbols. An expected symbol is on the left of a dot in a parse state of the parse set.



64
65
66
67
68
69
70
71
# File 'lib/rley/parser/state_set.rb', line 64

def expected_terminals()
  expecting_terminals = states.select do |s|
    s.dotted_rule.next_symbol.kind_of?(Rley::Syntax::Terminal)
  end

  terminals = expecting_terminals.map { |s| s.dotted_rule.next_symbol }
  return terminals.uniq
end

#predecessor_state(aPState) ⇒ Object

Retrieve the parse state that is the predecessor of the given one.

Raises:

  • (StandardError)


54
55
56
57
58
59
60
# File 'lib/rley/parser/state_set.rb', line 54

def predecessor_state(aPState)
  dotted_rule = aPState.dotted_rule
  raise StandardError, aPState.to_s unless dotted_rule.prev_position

  candidate = states.find { |s| s.precedes?(aPState) }
  return candidate
end

#push_state(aState) ⇒ TrueClass/FalseClass

Append the given state (if it isn't yet in the set) to the list of states

Parameters:

Returns:

  • (TrueClass/FalseClass)

    true when the state is really added



22
23
24
25
26
27
28
29
30
31
# File 'lib/rley/parser/state_set.rb', line 22

def push_state(aState)
  if include?(aState)
    result = false
  else
    @states << aState
    result = true
  end

  return result
end

#states_expecting(aSymbol) ⇒ Object

The list of ParseState that expect the given symbol.

Parameters:

  • aSymbol (GrmSymbol)

    the expected symbol (=on the right of the dot)



36
37
38
# File 'lib/rley/parser/state_set.rb', line 36

def states_expecting(aSymbol)
  return states.select { |s| s.dotted_rule.next_symbol == aSymbol }
end

#states_for(aProduction) ⇒ Object

The list of ParseState that involve the given production



49
50
51
# File 'lib/rley/parser/state_set.rb', line 49

def states_for(aProduction)
  return states.select { |s| s.dotted_rule.production == aProduction }
end

#states_rewriting(aNonTerm) ⇒ Object

The list of complete ParseState that have the given non-terminal symbol as the lhs of their production.



42
43
44
45
46
# File 'lib/rley/parser/state_set.rb', line 42

def states_rewriting(aNonTerm)
  return states.select do |s|
    (s.dotted_rule.production.lhs == aNonTerm) && s.complete?
  end
end