For example, the CFG can produce: 6+(4-(5)+3) OR (7+7+(1-2)+9) OR -6+(-3+7+(9-1))
I have the following rules:
S→T|SS|(S)|e
T→exp•op•exp
nonzero→1|2|3|4|5|6|7|8|9
digit→0|1|2|3|4|5|6|7|8|9
num→nonzero•digit*
op→+|-
exp→num|(num)|op•num|(op•num)
However, the above rules can produce empty parenthesis (e.g. (()()) ) and it cannot produce the aforementioned examples.
I ended up with a quasi-solution:
S → S | SS | op(S) | exps
digit → 0|1|2|3|4|5|6|7|8|9
nonzero → 1|2|3|4|5|6|7|8|9
num → nonzero•digit*
exp → num | (num)
exps → op•exp•[op•exp]*
op → - | +
( → (
) → )
The solution above assumes operators must always precede numbers and opening parenthesis. This may force some unnecessary operators but it will always produce a valid mathematical expression.