Sha256: e369e3f2e851add384ee33f82c251aadb577e0eb1c7e8e765737108791850971

Contents?: true

Size: 1018 Bytes

Versions: 3

Compression:

Stored size: 1018 Bytes

Contents

# Load the builder class
require_relative '../../../lib/rley/syntax/grammar_builder'
require_relative '../../../lib/rley/parser/token'


module AmbiguousGrammarHelper
  # Factory method. Creates a grammar builder for a basic ambiguous
  # expression grammar.
  # (based on an example from Fisher and LeBlanc: "Crafting a Compiler")
  def grammar_builder()
    builder = Rley::Syntax::GrammarBuilder.new do
      add_terminals('+', 'id')
      rule 'S' => 'E'
      rule 'E' => %w(E + E)
      rule 'E' => 'id'
    end
    builder
  end

  # Basic tokenizing method
  def tokenize(aText, aGrammar)
    tokens = aText.scan(/\S+/).map do |lexeme|
      case lexeme
        when '+'
          terminal = aGrammar.name2symbol[lexeme]
        when /^[_a-zA-Z][_a-zA-Z0-9]*$/
          terminal = aGrammar.name2symbol['id']
        else
          msg = "Unknown input text '#{lexeme}'"
          raise StandardError, msg
      end
      Rley::Parser::Token.new(lexeme, terminal)
    end

    return tokens
  end
end # module

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rley-0.4.01 spec/rley/support/ambiguous_grammar_helper.rb
rley-0.4.00 spec/rley/support/ambiguous_grammar_helper.rb
rley-0.3.12 spec/rley/support/ambiguous_grammar_helper.rb