8 elif [[ ! $line ]]; then
11 regex
='([A-Z][a-z]*) => +([A-Za-z ]*)'
12 [[ $line =~
$regex ]] ||
{ echo "failed to parse"; exit 1; }
13 elements
[${BASH_REMATCH[1]}]=${BASH_REMATCH[1]}
14 set ${BASH_REMATCH[2]}
15 [[ $longest -gt $# ]] || longest
=$#
16 eval "${BASH_REMATCH[1]}+=('"$
*"')"
19 # cheat: after pre-reading the input, all elements start with capital
20 # except 'e', but 'e' does not occur in target or any substitution.
21 # Break input into per-element chunks
22 done < <(sed 's/e/E/g; s/\([A-Z]\)/ \1/g')
23 echo "read $count rules for ${elements[*]}, longest expansion $longest"
26 [[ ! ${DEBUG+set} ]] && return
29 # contract n list... # output all contractions of the nth element of list
35 debug
" prefix: $prefix"
38 for el
in ${elements[*]}; do
39 [[ $el == E
&& ( $prefix ||
$# -gt 2) ]] && continue # trivial prune
41 for i
in ${!arr[*]}; do
44 debug
" matched $el[$i] (${arr[i]})"
45 echo $prefix $el ${tail#${arr[i]}}
53 while case "$nl$list$nl" in *"${nl}E$nl"*) false
;; *) : ;; esac; do
54 echo " generation $gen, list has $(echo "$list" | wc -l) lines"
58 while [[ $i -lt $# && $i -lt $longest ]]; do
62 done <<< "$list" > tmp
63 list
=$
(sed '/^ /d' < tmp |
sort -u)
64 debug
"list is:"$
'\n'"$list"
68 echo "found target in $gen steps"