Given a string containing only 0's 1's, 2's and brackets, output the grammar tree of the string.
A 2
requires 2 arguments - one to the left and one to the right
A 1
requires a single argument - to either the left or right
A 0
doesn't require any arguments and is the base case
A pair of brackets counts as one argument and the contents of the brackets are evaluated separately from the rest of the string. Nested brackets are possible
A input string will always be a complete tree with no characters falling off. The string will also only have a single correct solution. Note that the functions are commutative and any arrangement of arguments for 2
will be acceptable. You will not have to handle input that doesn't conform to these requirements.
The output grammar format will be in the form function(arguments)
recursively
Test cases
0 --> 0
01 --> 1(0)
020 --> 2(0,0)
101 --> 1(1(0))
0120 --> 2(1(0),0)
0120210 --> 2(1(0),2(0,1(0)))
01210 --> 2(1(0),1(0))
(020)210 --> 2(2(0,0),1(0))
((020)20)1 --> 1(2(0,2(0,0)))
10201
valid input? \$\endgroup\$0120210
can't also be parsed as2[4](2[2](1[1](0[0]), 0[3]), 1[5](0[6]))
where the bracketed numbers indicate position in the string. \$\endgroup\$101
is also ambiguous. \$\endgroup\$