Sha256: 74d33636039e57a5885d650bbf3095f01d75618e86aa99f5df2b4b90d69059fd

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

# Purpose: define a grammar with left-recursive rule
require 'rley' # Load Rley library

# Instantiate a builder object that will build the grammar for us.
builder = Rley::Syntax::GrammarBuilder.new do
  # The grammar defines a language that consists in a sequence
  # of 0 or more dots...
  add_terminals('DOT')

  # Grammar with left recursive rule.
  rule 'l_dots' => []
  rule 'l_dots' => 'l_dots DOT'
end

# And now, let's build the grammar...
grammar = builder.grammar

# Highly simplified tokenizer implementation.
def tokenizer(aText, aGrammar)
  index = 0
  tokens = aText.scan(/\./).map do |dot|
    terminal = aGrammar.name2symbol['DOT']
    index += 1
    pos = Rley::Lexical::Position.new(1, index)
    Rley::Lexical::Token.new(dot, terminal, pos)
  end

  return tokens
end

input_to_parse = '.' * 500 # Input = 500 consecutive dots

parser = Rley::Parser::GFGEarleyParser.new(grammar)
tokens = tokenizer(input_to_parse, grammar)
result = parser.parse(tokens)

puts "Parsing successful? #{result.success?}"
unless result.success?
  puts result.failure_reason.message
  exit(1)
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rley-0.8.06 examples/general/left.rb
rley-0.8.05 examples/general/left.rb
rley-0.8.03 examples/general/left.rb
rley-0.8.02 examples/general/left.rb
rley-0.8.01 examples/general/left.rb
rley-0.8.00 examples/general/left.rb