[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: ambigious ++ expression silently accepted
From: |
Andrew J. Schorr |
Subject: |
Re: ambigious ++ expression silently accepted |
Date: |
Tue, 13 Dec 2005 11:53:34 -0500 |
User-agent: |
Mutt/1.4.1i |
On Tue, Dec 13, 2005 at 12:27:48AM -0800, Paul Eggert wrote:
> Sorry, I don't see how mere precedence resolves the ambiguity here.
> If it were only a matter of precedence, the Awk expression foo++bar
> could be parsed either as ((foo++) bar), or as (foo (++bar)). In both
> parses, the ++ would be unary, and would have higher precedence than
> concatenation.
>
> I looked through the grammar defined by POSIX in
> <http://www.opengroup.org/onlinepubs/000095399/utilities/awk.html>
> and it appears to me that foo++bar is indeed ambiguous, since it can
> be parsed in two different ways:
>
> non_unary_expr non_unary_expr -> lvalue INCR non_unary_expr
> non_unary_expr non_unary_expr -> non_unary_expr INCR lvalue
But that URL contains a table entitled:
"Table: Expressions in Decreasing Precedence in awk". And
that table clearly lists "Pre-increment" as having higher
precedence than "Post-increment". So shouldn't the
expression be parsed as (foo (++bar))? Or am I
interpreting the table improperly?
Note that down below after the YACC grammar, there's a note
that says:
"This grammar has several ambiguities that shall be resolved as follows:
* Operator precedence and associativity shall be as described in Expressions in
Decreasing Precedence in awk."
And in fact, gawk seems to parse as ((foo++) bar).
So is this a bug in gawk?
$ gawk -v foo=1 -v bar=2 'BEGIN {print foo++bar}'
12
I would expect the output to be "13" based on
the precedence table.
I note that Solaris 8 nawk behaves the same way as gawk:
$ nawk -v foo=1 -v bar=2 'BEGIN {print foo++bar}'
12
Regards,
Andy