BEGIN {
print pretty_format("3 19 [ + -2 / ] $")
print pretty_format("[ 1 2 ] [ 3 ]")
print pretty_format("1")
print pretty_format("[ 1")
print pretty_format("1 ]")
}
function pretty_format(str, b, i, q, ql, r, s, t) {
ql = split("[ " str " ]", q)
r = ""
s = ""
b = 0
for(i = 1; i <= ql; i++) {
t = q[i]
if (t == "[") {
r = r s "["
s = ""
b++
}
else if (t == "]") {
if (--b < 0) return "error"
r = r "]"
s = ", "
}
else {
r = r s "'" t "'"
s = ", "
}
}
if (b > 0) return "error"
return r
}
QkVHSU4gewogICAgcHJpbnQgcHJldHR5X2Zvcm1hdCgiMyAxOSBbICsgLTIgLyBdICQiKQoJcHJpbnQgcHJldHR5X2Zvcm1hdCgiWyAxIDIgXSBbIDMgXSIpCglwcmludCBwcmV0dHlfZm9ybWF0KCIxIikKCXByaW50IHByZXR0eV9mb3JtYXQoIlsgMSIpCglwcmludCBwcmV0dHlfZm9ybWF0KCIxIF0iKQp9CgpmdW5jdGlvbiBwcmV0dHlfZm9ybWF0KHN0ciwgIGIsIGksIHEsIHFsLCByLCBzLCB0KSB7CgoJcWwgPSBzcGxpdCgiWyAiIHN0ciAiIF0iLCBxKQoKCXIgPSAiIgoJcyA9ICIiCgliID0gMAoJZm9yKGkgPSAxOyBpIDw9IHFsOyBpKyspIHsKCQl0ID0gcVtpXQoJCWlmICh0ID09ICJbIikgewoJCQlyID0gciBzICJbIgoJCQlzID0gIiIKCQkJYisrCgkJfQoJCWVsc2UgaWYgKHQgPT0gIl0iKSB7CgkJCWlmICgtLWIgPCAwKSByZXR1cm4gImVycm9yIgoJCQlyID0gciAiXSIKCQkJcyA9ICIsICIKCQl9CgkJZWxzZSB7CgkJCXIgPSByIHMgIiciIHQgIiciCgkJCXMgPSAiLCAiCgkJfQoJfQoJaWYgKGIgPiAwKSByZXR1cm4gImVycm9yIgoKCXJldHVybiByCn0K