I'll just give you the basic idea, as opposed to writing the code for you.
Build a tree (well, sort of a tree) of every part of the string inside the outer-most {}
's (a {b} c {d}
would have 2 trees - one for {b}
and one for {d}
). Each element following on another must be a child of that element. Whenever there are multiple options, each of them must be a child of the previous node and each of them must have the next node as a child.
So for, for example, {a {b|c} d e | f}
, you'd have:
-> b -
/ \
-> a - -> d -> e
/ \ /
/ -> c -
\
\
-> f
Then generate all paths from the root, using depth-first search for example.
The paths are -> a -> b -> d -> e
and -> a -> c -> d -> e
and -> f
,
so we'd have {a b d e | a c d e | f}
.
Implementation hints:
A stack might be a good idea for keeping track of the nodes.
It might be wise to have blank intermediate nodes before and after {}
's (trust me, this would make it a lot easier to implement).
So {{a|b} {c|d}}
would look like:
-> a - -> c -
/ \ / \
-> . - -> . - -> .
\ / \ /
-> b - -> d -