Sha256: b89d44b88b9938ab7e4c8e45d5d4e4f9722396ade3eb9fc52f8bc7ec659d0e66

Contents?: true

Size: 1.65 KB

Versions: 40

Compression:

Stored size: 1.65 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_method :body, :rhs
      alias_method :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."
          fail StandardError, msg_prefix + msg_suffix
        end

        return aNonTerminal
      end
    end # class
  end # module
end # module

# End of file

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
rley-0.3.04 lib/rley/syntax/production.rb
rley-0.3.01 lib/rley/syntax/production.rb
rley-0.3.00 lib/rley/syntax/production.rb
rley-0.2.15 lib/rley/syntax/production.rb
rley-0.2.14 lib/rley/syntax/production.rb
rley-0.2.12 lib/rley/syntax/production.rb
rley-0.2.11 lib/rley/syntax/production.rb
rley-0.2.10 lib/rley/syntax/production.rb
rley-0.2.09 lib/rley/syntax/production.rb
rley-0.2.08 lib/rley/syntax/production.rb
rley-0.2.06 lib/rley/syntax/production.rb
rley-0.2.05 lib/rley/syntax/production.rb
rley-0.2.04 lib/rley/syntax/production.rb
rley-0.2.03 lib/rley/syntax/production.rb
rley-0.2.02 lib/rley/syntax/production.rb
rley-0.2.01 lib/rley/syntax/production.rb
rley-0.2.00 lib/rley/syntax/production.rb
rley-0.1.12 lib/rley/syntax/production.rb
rley-0.1.11 lib/rley/syntax/production.rb
rley-0.1.10 lib/rley/syntax/production.rb