Created
February 23, 2009 23:19
-
-
Save jolts/69257 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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