It's certainly possible, but it's not easy, and I'm not going to write the code for you...
You might like to tackle an easier problem first: start with
<open-paren level="0"/>
<open-paren level="1"/>
<bar num="3" oper="*"/>
<bar num="2" oper="-"/>
<close-paren level="1">
<bar num="2"/>
<close-paren level="0">
You can tackle that with a recursive approach along the lines:
process-sequence($S):
* take the first element in S
* if it's not an open-paren, copy it, and apply the same rules to the sequence $S[position() != 1]
* if it is an open-paren, output a <paren> element, whose content is generated by calling process-sequence applied to all the elements up to (and excluding) the matching close-paren.
Once you've got that working, think about how to convert the input from the form you gave into the form above - I suspect that part is relatively easy.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference