[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: ambigious ++ expression silently accepted
From: |
Paul Eggert |
Subject: |
Re: ambigious ++ expression silently accepted |
Date: |
Tue, 13 Dec 2005 00:27:48 -0800 |
User-agent: |
Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux) |
>> Some of my students wrote "{ foo++bar }" which is ambigious;
>> Either: post-increment foo and concatenate with bar
>> or: concatenate foo with pre-increment bar.
>
> It's not ambiguous. The unary operators have higher precedence
> than the binary ones, and concatenation is a binary operator.
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
If I understand things correctly, this is a defect in the POSIX spec
for Awk, one that could be corrected by adding an extra disambiguation
rule or by complicating the grammar somewhat.