Created
January 21, 2009 05:49
-
-
Save JoergWMittag/49866 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
#!/usr/bin/env ruby | |
require 'fiber' | |
# This state machine corresponds to the regular expression /ab*(c|d)/ | |
@start = Fiber.new do | |
loop do | |
case (@string.next rescue nil) | |
when 'a' then @state1.transfer | |
else @errorstate.transfer | |
end | |
end | |
end | |
@state1 = Fiber.new do | |
loop do | |
case (@string.next rescue nil) | |
when 'b' then @state1.transfer | |
when 'c', 'd' then @endstate.transfer | |
else @errorstate.transfer | |
end | |
end | |
end | |
@endstate = Fiber.new do | |
puts "It's a match!" | |
end | |
@errorstate = Fiber.new do | |
raise ArgumentError, 'String does not match!' | |
end | |
@string = 'abbbbb'.chars | |
@start.transfer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment