Sha256: aec019bf72340ab58fe3ff8a67cb0c6ef817850b1eafbdc33ec3bcf718b71b6d

Contents?: true

Size: 1.04 KB

Versions: 7

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}'"
          raise StandardError, msg
      end
      Rley::Parser::Token.new(lexeme, terminal)
    end

    return tokens
  end
end # module

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rley-0.3.11 spec/rley/support/ambiguous_grammar_helper.rb
rley-0.3.10 spec/rley/support/ambiguous_grammar_helper.rb
rley-0.3.09 spec/rley/support/ambiguous_grammar_helper.rb
rley-0.3.08 spec/rley/support/ambiguous_grammar_helper.rb
rley-0.3.07 spec/rley/support/ambiguous_grammar_helper.rb
rley-0.3.06 spec/rley/support/ambiguous_grammar_helper.rb
rley-0.3.05 spec/rley/support/ambiguous_grammar_helper.rb