Sha256: bd7eb0f448852f718a1a3a8e663ed0b616fd396ab416d7cc234b709af23800f5

Contents?: true

Size: 1.04 KB

Versions: 9

Compression:

Stored size: 1.04 KB

Contents

# Purpose: define a grammar with right-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 right recursive rule.
  rule 'r_dots' => []
  rule 'r_dots' => %w[DOT r_dots]  
end

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

# Highly simplified tokenizer implementation.
def tokenizer(aText, aGrammar)
  tokens = aText.scan(/\./).map do |dot|
    terminal = aGrammar.name2symbol['DOT']
    Rley::Lexical::Token.new(dot, terminal)
  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) # Takes about 20 seconds on my computer!!!!

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rley-0.6.09 examples/general/right.rb
rley-0.6.08 examples/general/right.rb
rley-0.6.07 examples/general/right.rb
rley-0.6.06 examples/general/right.rb
rley-0.6.05 examples/general/right.rb
rley-0.6.04 examples/general/right.rb
rley-0.6.03 examples/general/right.rb
rley-0.6.02 examples/general/right.rb
rley-0.6.01 examples/general/right.rb