Sha256: 4b08b17f25cfe3ef3c5f90abaf86171908dd7ab33204d9db92846613c24cd94d

Contents?: true

Size: 1.64 KB

Versions: 14

Compression:

Stored size: 1.64 KB

Contents

require_relative 'symbol_seq'

module Rley # This module is used as a namespace
  module Syntax # This module is used as a namespace
    # 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.
    class Production
      # The right-hand side (rhs) consists of a sequence of grammar symbols
      attr_reader(:rhs)

      # The left-hand side of the rule. It must be a non-terminal symbol
      attr_reader(:lhs)

      # Provide common alternate names to lhs and rhs accessors

      alias body rhs
      alias head lhs

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

      # Is the rhs empty?
      # @ return true if the rhs has no members.
      def empty?()
        return rhs.empty?
      end

      private

      # Validation method. Return the validated input argument or
      # raise an exception.
      def valid_lhs(aNonTerminal)
        unless aNonTerminal.kind_of?(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
    end # class
  end # module
end # module

# End of file

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rley-0.4.05 lib/rley/syntax/production.rb
rley-0.4.04 lib/rley/syntax/production.rb
rley-0.4.03 lib/rley/syntax/production.rb
rley-0.4.02 lib/rley/syntax/production.rb
rley-0.4.01 lib/rley/syntax/production.rb
rley-0.4.00 lib/rley/syntax/production.rb
rley-0.3.12 lib/rley/syntax/production.rb
rley-0.3.11 lib/rley/syntax/production.rb
rley-0.3.10 lib/rley/syntax/production.rb
rley-0.3.09 lib/rley/syntax/production.rb
rley-0.3.08 lib/rley/syntax/production.rb
rley-0.3.07 lib/rley/syntax/production.rb
rley-0.3.06 lib/rley/syntax/production.rb
rley-0.3.05 lib/rley/syntax/production.rb