Skip to content

Instantly share code, notes, and snippets.

@jolts
Created February 23, 2009 23:19
Show Gist options
  • Save jolts/69257 to your computer and use it in GitHub Desktop.
Save jolts/69257 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'parse_tree'
require 'sexp_processor'
require 'logger'
class ParseError < RuntimeError; end
class Parser
def initialize
@logger = Logger.new(STDOUT)
$rules = Hash.new
@start = nil
@tokens = ["+", "-", "*", "/"]
@input = Array.new
@output = Array.new
end
def tokenize(string)
@input = string.clone
@input.strip.to_a
until string.empty?
@input.each do |lex|
raise ParseError, "unable to lex #{string}" unless lex.match(/\w+/)
end
@output << string[/\+|\-|\/|\*/]
string.gsub!(/\+|\-|\/|\*/, '')
@output << string[/\d+/]
string.gsub!(/\d+/, '')
end
puts @output
end
end
parser = Parser.new
parser.tokenize('1 + 2')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment