PREV INDEX NEXT

Author: Stan Eisenstat
Subject: An amusing test case
Date: Sunday, 18 Oct 2020, 20:25:55


As stated in the specification:

  The expansion of WORD takes place before the substitution so that the search
  for substrings to expand proceeds from left to right and continues at the end
  of the replacement string after each substitution.

Here is an example of why you cannot always search
repeatedly starting at the beginning of the string:

  % echo \$1 | ./mcBash \$1
  (1)$ >> $1

The argument passed to mcBash is "$1".  The string read
is "$1\n".  After replacing "$1" by "$1", the string is
unchanged.

Here is an example of why you cannot always replace all
occurrences:

  $ echo \$1\$A\$1 | A=\$1 ./mcBash \$A
  (1)$ >> $A$1$A
   
The input is "$1$A$1\n".  If you replace all occurrences
of "$A" by its value "$1", giving "$1$1$1\n" and then
replace all occurrences of "$1" by its value "$A", you
get "$A$A$A\n", which is incorrect.  If you replace all
occurrences of "$A" by its value "$1", giving "$1$1$1\n"
and then replace all occurrences of "$1" by its value "$A",
you get "$A$A$A\n", which is still incorrect.

What this suggests is that you may want to continually
remove a prefix of the input and append its replacement
to another, initially empty string.  The regexes for this
process are fairly simple.

The other approaches I know of are much more complicated.

--Stan-
PREV INDEX NEXT