Skip to content

Instantly share code, notes, and snippets.

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