Skip to content

Instantly share code, notes, and snippets.

@jolts
Created February 23, 2009 23:10
Show Gist options
  • Save jolts/69250 to your computer and use it in GitHub Desktop.
Save jolts/69250 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
tokens = Array.new
@input.strip.to_a
puts @input
until string.empty?
@input.each do |lex|
raise ParseError, "unable to lex #{string}" unless lex.match(/\w+/)
@output << string[/\+|\-|\/|\*/]
string.gsub(/\+|\-|\/|\*/, '')
end
string = ""
end
puts tokens
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