Class: Rley::Syntax::Production
- Inherits:
-
Object
- Object
- Rley::Syntax::Production
- 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
-
#generative ⇒ Boolean
writeonly
rhs members are generative (that is, they can each generate/derive a non-empty string of terminals).
-
#lhs ⇒ NonTerminal
(also: #head)
readonly
The left-hand side of the rule.
-
#name ⇒ String
The name of the production rule.
-
#rhs ⇒ SymbolSeq
(also: #body)
readonly
The right-hand side (rhs).
Instance Method Summary collapse
-
#as(aName) ⇒ Object
A setter for the production name.
-
#empty? ⇒ Boolean
Is the rhs empty?.
-
#generative? ⇒ Boolean
Return true iff the production is generative.
-
#initialize(aNonTerminal, theSymbols) ⇒ Production
constructor
Create a Production instance.
-
#inspect ⇒ String
Returns a string containing a human-readable representation of the production.
Constructor Details
#initialize(aNonTerminal, theSymbols) ⇒ Production
Create a Production instance.
36 37 38 39 |
# File 'lib/rley/syntax/production.rb', line 36 def initialize(aNonTerminal, theSymbols) @lhs = valid_lhs(aNonTerminal) @rhs = valid_rhs(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).
26 27 28 |
# File 'lib/rley/syntax/production.rb', line 26 def generative=(value) @generative = value end |
#lhs ⇒ NonTerminal (readonly) Also known as: head
Returns The left-hand side of the rule.
17 18 19 |
# File 'lib/rley/syntax/production.rb', line 17 def lhs @lhs end |
#name ⇒ String
Returns The name of the production rule. It must be unique in a grammar.
21 22 23 |
# File 'lib/rley/syntax/production.rb', line 21 def name @name end |
#rhs ⇒ SymbolSeq (readonly) Also known as: body
Returns The right-hand side (rhs).
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
69 70 71 |
# File 'lib/rley/syntax/production.rb', line 69 def as(aName) @name = aName end |
#empty? ⇒ Boolean
Is the rhs empty?
43 44 45 |
# File 'lib/rley/syntax/production.rb', line 43 def empty?() return rhs.empty? end |
#generative? ⇒ Boolean
Return true iff the production is generative
48 49 50 51 52 53 |
# File 'lib/rley/syntax/production.rb', line 48 def generative?() if @generative.nil? end return @generative end |
#inspect ⇒ String
Returns a string containing a human-readable representation of the production.
58 59 60 61 62 63 64 65 |
# File 'lib/rley/syntax/production.rb', line 58 def inspect() result = "#<#{self.class.name}:#{object_id}" result << " @name=\"#{name}\"" result << " @lhs=#{lhs.name}" result << " @rhs=#{rhs.inspect}" result << " @generative=#{@generative}>" return result end |