Created
February 23, 2009 23:29
-
-
Save jolts/69260 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 '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