L
Lawrence Kirby
Furthermore, the table is wrong (at least if it's the same table as the
one I'm used to). The expression
sizeof (int) - 1
is parsed as
(sizeof (int)) - 1
not as
sizeof ((int) (- 1))
as the table implies.
The problem here is not that the precedence table mispredicts the binding,
it is that it cannot predict it at all, the precedence rules allow both of
these. A precedence table assumes that you already know what operators are
being used or have a grammar without such ambiguities, and it will then
determine how operands are bound to those operators. In this case there is
an ambiguity over which operators the original exprrssion contains. In the
first parse there is a sizeof operator and a binary -, in the second parse
there is a sizeof, a cast and a unary -. This ambiguity cannot be solved
by pure precedence, there have to be other non-precedence based rules to
resolve this. E.g. a rule that states that sizeof followed by a type in
parentheses is parsed as a single sizeof(TYPE) operation rather than
sizeof (CAST). I don't have K&R2 with me here, but I though it did mention
sizeof in the accompanying text for the precedence table.
This demonstrates that the grammar form used to specify C's syntax is more
powerful than precedence rules since it can express this directly.
Lawrence