Sha256: 2a8363af2c6fd05e4dd5cec64062dc0799c0b2eb8e8ab50585a387b00408607b
Contents?: true
Size: 1.38 KB
Versions: 1
Compression:
Stored size: 1.38 KB
Contents
require 'strscan' # Load the builder class require_relative '../../../lib/rley/syntax/grammar_builder' require_relative '../../../lib/rley/tokens/token' module GrammarArrIntHelper # Factory method. Creates a grammar builder for a grammar of # array of integers. # (based on the article about Earley's algorithm in Wikipedia) def grammar_arr_int_builder() builder = Rley::Syntax::GrammarBuilder.new do add_terminals('[', ']', ',', 'integer') rule 'P' => 'arr' rule 'arr' => %w( [ sequence ] ) rule 'sequence' => ['list'] rule 'sequence' => [] rule 'list' => %w[list , integer] # Right-recursive rule rule 'list' => 'integer' end builder end # Basic tokenizer for array of integers def arr_int_tokenizer(aText, aGrammar) tokens = [] scanner = StringScanner.new(aText) until scanner.eos? do scanner.skip(/\s+/) lexeme = scanner.scan(/[\[,\]]/) if lexeme terminal = aGrammar.name2symbol[lexeme] tokens << Rley::Tokens::Token.new(lexeme, terminal) next end lexeme = scanner.scan(/^[-+]?\d+/) if lexeme terminal = aGrammar.name2symbol['integer'] tokens << Rley::Tokens::Token.new(lexeme, terminal) else msg = "Unknown input text '#{lexeme}'" raise StandardError, msg end end return tokens end end # module # End of file
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rley-0.5.02 | spec/rley/support/grammar_arr_int_helper.rb |