Skip to content

Instantly share code, notes, and snippets.

@bmaland
Created April 13, 2009 13:23
Show Gist options
  • Save bmaland/94442 to your computer and use it in GitHub Desktop.
Save bmaland/94442 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "rton"
include Automaton
pda = Automaton.setup(:pda) do
initial_state :q0 do
transition ['a', EMPTY], [:q0, 'A']
transition ['b', 'A'], [:q1, EMPTY]
end
state :q1 do
transition ['b', 'A'], [:q1, EMPTY]
end
final_states :q0, :q1
end
loop do
print "Input: "
input = gets
break if input.nil?
input = input.chomp.strip.split("") # simple tokenization
if pda.recognize?(input)
puts "The input was recognized by the automaton"
else
puts "The input was rejected by the automaton"
end
end
# $ ruby rton_cli.rb
# Input: aabb
# The input was recognized by the automaton
# Input: aaaabbbb
# The input was recognized by the automaton
# Input: abb
# The input was rejected by the automaton
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment