Sha256: 6988984541e38d47d9264cf86849c3408aa7b97897f28a8cc1c69f5f6bafa2d0

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

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
    builder.add_terminals('+', 'id')
    builder.add_production('S' => 'E')
    builder.add_production('E' => %w(E + E))
    builder.add_production('E' => 'id')
    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}'"
          fail 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.3.04 spec/rley/support/ambiguous_grammar_helper.rb
rley-0.3.01 spec/rley/support/ambiguous_grammar_helper.rb
rley-0.3.00 spec/rley/support/ambiguous_grammar_helper.rb