bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bug in M4


From: Alexandre Duret-Lutz
Subject: Re: Bug in M4
Date: Mon, 03 Jan 2005 23:33:07 +0100
User-agent: Gnus/5.1003 (Gnus v5.10.3) Emacs/21.3.50 (gnu/linux)

>>> "Sam" == Sam Lauber <address@hidden> writes:

 Sam> I was using GNU M4 1.4.2, and typed in the following
 Sam> recursive additive macro:

 Sam> define(`add', `ifelse($1, 0, $2, add(decr($1), incr($2))')

I'm assuming you meant

      define(`add', `ifelse($1, 0, $2, add(decr($1), incr($2)))')

Since unquoted arguments are evaluated before the enclosing macro
is expanded, the above could be translated in C as 

int
add(int a, int b)
{
  int res = add(a - 1, b + 1);
  if (a == 0)
    return b;
  else
    return res;
}

See the problem?

Obviously you want to evaluate the inner add only after (and if) it
has been output by `ifelse', so let's quote it:

  define(`add', `ifelse($1, 0, $2, `add(decr($1), incr($2))')')

 Sam> I then typed `add(1, 1)'. Obviously, I expected it to say `2', but it said

 Sam> `Recursion limit of 250 exceeded; use -L<N> to change'

 Sam> I consider this a bug, because though this _is_ a recursive alogrithm,
 Sam> I don't think it would recurse 250 times over adding 1 + 1.

FWIW, an easy way to understand how M4 processes a small test case is
to run is as `m4 -dtaeq'.
-- 
Alexandre Duret-Lutz





reply via email to

[Prev in Thread] Current Thread [Next in Thread]