Class: Sequitur::Production

Inherits:
Object
  • Object
show all
Defined in:
lib/sequitur/production.rb

Overview

In a context-free grammar, a production is a rule in which its left-hand side (LHS) consists solely of a non-terminal symbol and the right-hand side (RHS) consists of a sequence of symbols. The symbols in RHS can be either terminal or non-terminal symbols. The rule stipulates that the LHS is equivalent to the RHS, in other words every occurrence of the LHS can be substituted to corresponding RHS. Implementation note: the object id of the production is taken as its LHS.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProduction

Constructor. Build a production with an empty RHS.



28
29
30
31
32
# File 'lib/sequitur/production.rb', line 28

def initialize()
  @rhs = SymbolSequence.new
  @refcount = 0
  @digrams = []
end

Instance Attribute Details

#digramsObject (readonly)

The sequence of digrams appearing in the RHS



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

def digrams
  @digrams
end

#refcountObject (readonly)

The reference count (= how times other productions reference this one)



21
22
23
# File 'lib/sequitur/production.rb', line 21

def refcount
  @refcount
end

#rhsObject (readonly)

The right-hand side (rhs) consists of a sequence of grammar symbols



18
19
20
# File 'lib/sequitur/production.rb', line 18

def rhs
  @rhs
end

Instance Method Details

#==(other) ⇒ Object

Identity testing.

Parameters:

  • other

    [] another production or production reference.

Returns:

  • true when the receiver and other are the same.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sequitur/production.rb', line 41

def ==(other)
  return true if object_id == other.object_id

  if other.is_a?(ProductionRef)
    result = (other == self)
  else
    result = false
  end

  return result
end

#accept(aVisitor) ⇒ Object

Part of the 'visitee' role in Visitor design pattern.

Parameters:



231
232
233
234
235
236
237
# File 'lib/sequitur/production.rb', line 231

def accept(aVisitor)
  aVisitor.start_visit_production(self)

  rhs.accept(aVisitor)

  aVisitor.end_visit_production(self)
end

#append_symbol(aSymbol) ⇒ Object

Add a (grammar) symbol at the end of the RHS.

Parameters:

  • aSymbol (Object)

    A (grammar) symbol to add.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/sequitur/production.rb', line 139

def append_symbol(aSymbol)
  case aSymbol
    when Production
      new_symb = ProductionRef.new(aSymbol)
    when ProductionRef
      if aSymbol.unbound?
        msg = 'Fail to append reference to nil production in '
        msg << to_string
        fail StandardError, msg
      end
      new_symb = aSymbol.dup
    else
      new_symb = aSymbol
  end

  rhs << new_symb
  digrams << Digram.new(rhs[-2], rhs[-1], self) if rhs.size >= 2
end

#clear_rhsObject

Clear the right-hand side. Any referenced production has its reference counter decremented.



160
161
162
# File 'lib/sequitur/production.rb', line 160

def clear_rhs()
  rhs.clear()
end

#decr_refcountObject

Decrement the reference count by one.



66
67
68
69
# File 'lib/sequitur/production.rb', line 66

def decr_refcount()
  fail StandardError, 'Internal error' if @refcount == 0
  @refcount -= 1
end

#derive_step(another) ⇒ Object

Replace every occurrence of 'another' production in self.rhs by the symbols in the rhs of 'another'.

Given the production p_A : a p_B b p_B c

And the production p_B : x y

Then...

p_A.derive_step(p_B)

Modifies p_A as into: p_A -> a x y b x y c

Examples:

Synopsis


Parameters:



216
217
218
219
220
221
222
223
224
225
226
# File 'lib/sequitur/production.rb', line 216

def derive_step(another)
  (0...rhs.size).to_a.reverse.each do |index|
    next unless rhs[index] == another

    rhs.insert_at(index + 1, another.rhs)
    another.decr_refcount
    rhs.delete_at(index)
  end

  recalc_digrams
end

#empty?Boolean

Is the rhs empty? @ return true if the rhs has no members.

Returns:

  • (Boolean)


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

def empty?
  return rhs.empty?
end

#incr_refcountObject

Increment the reference count by one.



61
62
63
# File 'lib/sequitur/production.rb', line 61

def incr_refcount()
  @refcount += 1
end

#last_digramDigram

Retrieve the last digram appearing in the RHS (if any).

Returns:

  • (Digram)

    last digram in the rhs otherwise nil.



122
123
124
125
# File 'lib/sequitur/production.rb', line 122

def last_digram()
  result = digrams.empty? ? nil : digrams.last
  return result
end

#positions_of(symb1, symb2) ⇒ Array

Find all the positions where the digram occurs in the rhs

Examples:

# Given the production p : a b c a b a b d
#Then ...
p.positions_of(a, b) # => [0, 3, 5]
# Caution: "overlapping" digrams shouldn't be counted
# Given the production p : a a b a a a c d
# Then ... 
p.positions_of(a, a) # => [0, 3]

Parameters:

  • symb1 (Object)

    first symbol of the digram

  • symb2 (Object)

    second symbol of the digram

Returns:

  • (Array)

    the list of indices where the digram occurs in rhs.



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/sequitur/production.rb', line 176

def positions_of(symb1, symb2)

  # Find the positions where the digram occur in rhs

  indices = [ -2 ] # Dummy index!

  (0...rhs.size).each do |i|
    next if i == indices.last + 1
    indices << i if (rhs[i] == symb1) && (rhs[i + 1] == symb2)
  end

  indices.shift

  return indices
end

#recalc_digramsArray

Enumerate the digrams appearing in the right-hand side (rhs)

Returns:

  • (Array)

    the list of digrams found in rhs of this production.



89
90
91
92
93
94
95
# File 'lib/sequitur/production.rb', line 89

def recalc_digrams()
  return [] if rhs.size < 2

  result = []
  rhs.symbols.each_cons(2) { |couple| result << Digram.new(*couple, self) }
  @digrams = result
end

#reduce_step(another) ⇒ Object

Given that the production P passed as argument has exactly 2 symbols in its rhs s1 s2, substitute in the rhs of self all occurrences of s1 s2 by a reference to P.

Parameters:



196
197
198
199
200
201
202
203
204
# File 'lib/sequitur/production.rb', line 196

def reduce_step(another)
  (symb1, symb2) = another.rhs.symbols
  pos = positions_of(symb1, symb2).reverse

  # Replace the two symbol sequence by the production

  pos.each { |index| rhs.reduce_step(index, another) }

  recalc_digrams
end

#referencesArray of ProductionRef

Select the references to production appearing in the rhs.

Returns:



74
75
76
# File 'lib/sequitur/production.rb', line 74

def references()
  return rhs.references
end

#references_of(aProduction) ⇒ Array

Look in the rhs all the references to a production passed a argument. aProduction [aProduction or ProductionRef] The production to search for.

Returns:

  • (Array)

    the array of ProductionRef to the passed production



81
82
83
84
# File 'lib/sequitur/production.rb', line 81

def references_of(aProduction)
  refs = references
  return refs.select { |a_ref| a_ref == aProduction }
end

#repeated_digram?true/false

Detect whether the last digram occurs twice Assumption: when a digram occurs twice in a production then it must occur at the end of the rhs

Returns:

  • (true/false)

    true when the digram occurs twice in rhs.



110
111
112
113
114
115
116
117
118
# File 'lib/sequitur/production.rb', line 110

def repeated_digram?()
  return false if rhs.size < 3

  my_digrams = digrams
  all_keys = my_digrams.map(&:key)
  last_key = all_keys.pop
  same_key_found = all_keys.index(last_key)
  return !same_key_found.nil?
end

#single_digram?true/false

Does the rhs have exactly one digram only (= 2 symbols)?

Returns:

  • (true/false)

    true when the rhs contains exactly two symbols.



101
102
103
# File 'lib/sequitur/production.rb', line 101

def single_digram?
  return rhs.size == 2
end

#to_stringString

Emit a text representation of the production rule. Text is of the form: object id of production : rhs as space-separated sequence of symbols.

Returns:

  • (String)


133
134
135
# File 'lib/sequitur/production.rb', line 133

def to_string()
  return "#{object_id} : #{rhs.to_string}."
end