class Rley::Syntax::Production
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.
Attributes
@return [SymbolSeq] The right-hand side (rhs).
@return [Array<Syntax::MatchClosest>] A list of constraints between rhs members
@return [Boolean] A production is generative when all of its rhs members are generative (that is, they can each generate/derive a non-empty string of terminals).
@return [NonTerminal] The left-hand side of the rule.
@return [NonTerminal] The left-hand side of the rule.
@return [String]
The name of the production rule. It must be unique in a grammar.
@return [Boolean] A production is nullable when all of its
rhs members are nullable.
@return [SymbolSeq] The right-hand side (rhs).
Public Class Methods
Source
# File lib/rley/syntax/production.rb, line 45 def initialize(aNonTerminal, theSymbols) @lhs = valid_lhs(aNonTerminal) @rhs = valid_rhs(theSymbols) @constraints = [] end
Create a Production
instance. @param aNonTerminal [NonTerminal] The left-hand side of the rule. @param theSymbols [list<Terminal | NonTerminal>] symbols of rhs.
Public Instance Methods
Source
# File lib/rley/syntax/production.rb, line 84 def as(aName) @name = aName end
A setter for the production name @param aName [String] the name of the production
Source
# File lib/rley/syntax/production.rb, line 53 def empty? rhs.empty? end
Is the rhs empty? @return [Boolean] true if the rhs has no members.
Source
# File lib/rley/syntax/production.rb, line 58 def generative? # if @generative.nil? # end @generative end
Return true iff the production is generative
Source
# 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
Returns a string containing a human-readable representation of the production. @return [String]
Source
# File lib/rley/syntax/production.rb, line 66 def nullable? @nullable end
@return [Boolen] true iff the production is nullable
Private Instance Methods
Source
# File lib/rley/syntax/production.rb, line 94 def valid_lhs(aNonTerminal) unless aNonTerminal.is_a?(NonTerminal) msg_prefix = 'Left side of production must be a non-terminal symbol' msg_suffix = ", found a #{aNonTerminal.class} instead." raise StandardError, msg_prefix + msg_suffix end return aNonTerminal end
Validation method. Return the validated input argument or raise an exception.
Source
# File lib/rley/syntax/production.rb, line 104 def valid_rhs(theSymbols) if theSymbols.nil? msg_prefix = 'Right side of a production of the kind ' msg_suffix = "'#{lhs.name}' => ... is nil." raise StandardError, msg_prefix + msg_suffix end return SymbolSeq.new(theSymbols) end