#!/usr/bin/ruby DEBUG = false def debug_print(nonterminal, stack) if DEBUG puts "matching #{nonterminal}, stack: #{stack.reverse.join(", ")}" end end def match(expected, stack) if stack.pop != expected raise "reject" end end def a(stack) debug_print('A', stack) match('b', stack) b(stack) end def b(stack) debug_print('B', stack) match('a', stack) c(stack) end def c(stack) debug_print('C', stack) if !stack.empty? match('b', stack) b(stack) end end while line = STDIN.gets begin # use an array as a stack, with the end of the array acting as the top # (beginning of input) stack = line.split.reverse a(stack) if !stack.empty? raise 'reject' end puts 'accept' rescue Exception => e puts e end end