Here's my solution, it's pretty bad as it doesn't fix anything, but I
was looking for an excuse to play with racc and I just did it in the
last 15-20mins or so:
% cat brackets.y
class BracketParser
rule
pack_string: packing
curly: '{' pack_expr '}'
square: '[' pack_expr ']'
round: '(' pack_expr ')'
packing: curly | square | round
bs: 'B' | 'B' bs
packing_list: packing | packing packing_list
pack_expr: bs | packing_list
brackets.tab.rb is pretty massive so I'm omitting it, anyone without
racc who really wants to see it can email me off list
% cat parse_brackets.rb
require 'brackets.tab'
class BracketParser
def parse_str(str)
list_of_tokens = str.split(//).map { |x| [x, x] }
list_of_tokens << [false, false]
yyparse(list_of_tokens, :each)
end
end
parser = BracketParser.new
ARGF.each do |line|
line = line.chomp
begin
parser.parse_str(line)
puts line
rescue Racc::ParseError
exit(1)
end
end