Sha256: d376ef4b0c4771aa21ce9abf609affaff59dc8a4ec8f771cbea676dc733d508c
Contents?: true
Size: 1.07 KB
Versions: 6
Compression:
Stored size: 1.07 KB
Contents
# frozen_string_literal: true # Load the builder class require_relative '../../../lib/rley/notation/grammar_builder' require_relative '../../../lib/rley/lexical/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 Rley::Notation::GrammarBuilder.new do add_terminals('+', 'id') rule 'S' => 'E' rule 'E' => 'E + E' rule 'E' => 'id' end 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 pos = Rley::Lexical::Position.new(3, 4) # dummy pos Rley::Lexical::Token.new(lexeme, terminal, pos) end return tokens end end # module
Version data entries
6 entries across 6 versions & 1 rubygems