Description
- A parenthesis-enclosed list contains one or more members separated by commas.
- A member of a list can be another list.
- There may be more than one list in a line.
- Each parenthesis is assigned a `depth' according to the following rules:
- If a opening parenthesis is not enclosed by another,
its depth is one.
- The depth of a opening parenthesis is one plus the depth of
the nearest opening one enclosing it.
- Since parentheses are paired, the depth of a closing parenthesis
is the same as the corresponding opening one.
- We want to remove all parentheses whose depth is not maximum.
|
| Raw Input
| A1 (B2,(C3,(D4,E5),(F6))) G6 (H7,(I7,(J8,K9)),L0)
|
|
| Desired Output
| A1 B2,C3,(D4,E5),(F6) G6 H7,I7,(J8,K9),L0
|
|
Script and Comments
Script1 [ 1] :loop0
[ 2] s/\(.*/\n&\n/
[ 3] /\n/!b
[ 4] :loop1
[ 5] /\n\(/s/$/#/
[ 6] /\n[()].*\n#$/s/\n/\n\n/
[ 7] /\n\)/s/#$//
[ 8] s/\n([()][^()\n]*)/\1\n/
[ 9] /^([^\n]*\n){3}/s/\n[()]//
[10] /\n\n/!b loop1
[11] s/\n\n//
[12] /^([^()]*\([^()]*\))*[^()]*$/!b loop0
| |
|