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.

Parameters:



45
46
47
48
49
# File 'lib/rley/syntax/production.rb', line 45

def initialize(aNonTerminal, theSymbols)
  @lhs = valid_lhs(aNonTerminal)
  @rhs = valid_rhs(theSymbols)
  @constraints = []
end

Instance Attribute Details

#constraintsArray<Syntax::MatchClosest>

Returns A list of constraints between rhs members.

Returns:



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

def constraints
  @constraints
end

#generative=(value) ⇒ Boolean (writeonly)

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

Returns:

  • (Boolean)

    A production is generative when all of its



28
29
30
# File 'lib/rley/syntax/production.rb', line 28

def generative=(value)
  @generative = value
end

#lhsNonTerminal (readonly) Also known as: head

Returns The left-hand side of the rule.

Returns:



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

def lhs
  @lhs
end

#nameString

Returns The name of the production rule. It must be unique in a grammar.

Returns:

  • (String)

    The name of the production rule. It must be unique in a grammar.



23
24
25
# File 'lib/rley/syntax/production.rb', line 23

def name
  @name
end

#nullable=(value) ⇒ Boolean (writeonly)

Returns A production is nullable when all of its rhs members are nullable.

Returns:

  • (Boolean)

    A production is nullable when all of its rhs members are nullable.



32
33
34
# File 'lib/rley/syntax/production.rb', line 32

def nullable=(value)
  @nullable = value
end

#rhsSymbolSeq (readonly) Also known as: body

Returns The right-hand side (rhs).

Returns:



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

def rhs
  @rhs
end

Instance Method Details

#as(aName) ⇒ Object Also known as: tag

A setter for the production name

Parameters:

  • aName (String)

    the name of the production



84
85
86
# File 'lib/rley/syntax/production.rb', line 84

def as(aName)
  @name = aName
end

#empty?Boolean

Is the rhs empty?

Returns:

  • (Boolean)

    true if the rhs has no members.



53
54
55
# File 'lib/rley/syntax/production.rb', line 53

def empty?
  rhs.empty?
end

#generative?Boolean

Return true iff the production is generative

Returns:

  • (Boolean)


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

def generative?
  # if @generative.nil?
  # end

  @generative
end

#inspectString

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

Returns:

  • (String)


73
74
75
76
77
78
79
80
# File 'lib/rley/syntax/production.rb', line 73

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

#nullable?Boolen

Returns true iff the production is nullable.

Returns:

  • (Boolen)

    true iff the production is nullable



66
67
68
# File 'lib/rley/syntax/production.rb', line 66

def nullable?
  @nullable
end