Class: Rley::Syntax::Production

Inherits:
Object
  • Object
show all
Defined in:
lib/rley/syntax/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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aNonTerminal, theSymbols) ⇒ Production

Create a Production instance.



35
36
37
38
# File 'lib/rley/syntax/production.rb', line 35

def initialize(aNonTerminal, theSymbols)
  @lhs = valid_lhs(aNonTerminal)
  @rhs = SymbolSeq.new(theSymbols)       
end

Instance Attribute Details

#generative=(value) ⇒ Boolean (writeonly)

rhs members are generative (that is, they can each generate/derive a non-empty string of terminals).



25
26
27
# File 'lib/rley/syntax/production.rb', line 25

def generative=(value)
  @generative = value
end

#lhsNonTerminal (readonly) Also known as: head



17
18
19
# File 'lib/rley/syntax/production.rb', line 17

def lhs
  @lhs
end

#nameString



20
21
22
# File 'lib/rley/syntax/production.rb', line 20

def name
  @name
end

#rhsSymbolSeq (readonly) Also known as: body



14
15
16
# File 'lib/rley/syntax/production.rb', line 14

def rhs
  @rhs
end

Instance Method Details

#as(aName) ⇒ Object

A setter for the production name



68
69
70
# File 'lib/rley/syntax/production.rb', line 68

def as(aName)
  @name = aName
end

#empty?Boolean

Is the rhs empty?



42
43
44
# File 'lib/rley/syntax/production.rb', line 42

def empty?()
  return rhs.empty?
end

#generative?Boolean

Return true iff the production is generative



47
48
49
50
51
52
# File 'lib/rley/syntax/production.rb', line 47

def generative?()
  if @generative.nil?
  end
  
  return @generative
end

#inspectString

Returns a string containing a human-readable representation of the production.



57
58
59
60
61
62
63
64
# File 'lib/rley/syntax/production.rb', line 57

def inspect()
  result = "#<#{self.class.name}:#{self.object_id}"
  result << " @name=\"#{self.name}\""
  result << " @lhs=#{lhs.name}"
  result << " @rhs=#{rhs.inspect}"
  result << " @generative=#{@generative}>"
  return result
end