Sha256: 2f46adff86d6f59a3f4b454af430a491b1244786801f53521b18496243f55238

Contents?: true

Size: 1.26 KB

Versions: 10

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

# Load the builder class
require_relative '../../../lib/rley/syntax/base_grammar_builder'
require_relative '../../../lib/rley/lexical/token'


module GrammarBExprHelper
  # Factory method. Creates a grammar builder for a basic arithmetic
  # expression grammar.
  # (based on the article about Earley's algorithm in Wikipedia)
  def grammar_expr_builder
    Rley::Syntax::BaseGrammarBuilder.new do
      add_terminals('+', '*', 'integer')
      rule 'P' => 'S'
      rule 'S' => 'S + M'
      rule 'S' => 'M'
      rule 'M' => 'M * T'
      rule 'M' => 'T'
      rule 'T' => 'integer'
    end
  end

  # Basic expression tokenizer
  def expr_tokenizer(aText)
    scanner = StringScanner.new(aText)
    tokens = []

    loop do
      scanner.skip(/\s+/)
      curr_pos = scanner.pos
      lexeme = scanner.scan(/\S+/)
      break unless lexeme

      case lexeme
        when '+', '*'
          terminal = lexeme
        when /^[-+]?\d+$/
          terminal = 'integer'
        else
          msg = "Unknown input text '#{lexeme}'"
          raise StandardError, msg
      end

      pos = Rley::Lexical::Position.new(1, curr_pos + 1)
      tokens << Rley::Lexical::Token.new(lexeme, terminal, pos)
    end

    return tokens
  end
end # module
# End of file

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rley-0.8.11 spec/rley/support/grammar_b_expr_helper.rb
rley-0.8.10 spec/rley/support/grammar_b_expr_helper.rb
rley-0.8.09 spec/rley/support/grammar_b_expr_helper.rb
rley-0.8.08 spec/rley/support/grammar_b_expr_helper.rb
rley-0.8.06 spec/rley/support/grammar_b_expr_helper.rb
rley-0.8.05 spec/rley/support/grammar_b_expr_helper.rb
rley-0.8.03 spec/rley/support/grammar_b_expr_helper.rb
rley-0.8.02 spec/rley/support/grammar_b_expr_helper.rb
rley-0.8.01 spec/rley/support/grammar_b_expr_helper.rb
rley-0.8.00 spec/rley/support/grammar_b_expr_helper.rb