Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GAWK: Parenthesezation needed?

56 views
Skip to first unread message

Kenny McCormack

unread,
Dec 19, 2014, 11:48:09 AM12/19/14
to
Observe:

$ gawk '$2 == $3 == $4'
gawk: $2 == $3 == $4
gawk: ^ syntax error
$

This works if you put parens around either sub-expression [*].

Note that this works as written (with the appropriate mods to satisfy the
compiler) in C [*]. Shouldn't gawk have pretty much the same rules as C?

Note that in general, there are a few situations in GAWK where parens are
needed in order to make it work as expected, but I don't think I've hit any
where it needs parens to avoid being a syntax error.

[*] But doesn't do what you might expect...

--
Shikata ga nai...

Janis Papanagnou

unread,
Dec 19, 2014, 12:10:46 PM12/19/14
to
On 19.12.2014 17:48, Kenny McCormack wrote:
> Observe:
>
> $ gawk '$2 == $3 == $4'
> gawk: $2 == $3 == $4
> gawk: ^ syntax error
> $
>
> This works if you put parens around either sub-expression [*].
>
> Note that this works as written (with the appropriate mods to satisfy the
> compiler) in C [*]. Shouldn't gawk have pretty much the same rules as C?

Well, not necessarily as in C. But anyway, the POSIX Awk syntax says

non_unary_expr : ...
| non_unary_expr EQ expr
...

So, AFAICT, your above expression should be covered by the POSIX awk syntax
and not be a syntax error.

Janis

Kenny McCormack

unread,
Dec 19, 2014, 3:20:21 PM12/19/14
to
In article <m71m6l$bch$1...@news.m-online.net>,
Janis Papanagnou <janis_pa...@hotmail.com> wrote:
>On 19.12.2014 17:48, Kenny McCormack wrote:
>> Observe:
>>
>> $ gawk '$2 == $3 == $4'
>> gawk: $2 == $3 == $4
>> gawk: ^ syntax error
>> $
>>
>> This works if you put parens around either sub-expression [*].
>>
>> Note that this works as written (with the appropriate mods to satisfy the
>> compiler) in C [*]. Shouldn't gawk have pretty much the same rules as C?
>
>Well, not necessarily as in C. But anyway, the POSIX Awk syntax says
>
>non_unary_expr : ...
> | non_unary_expr EQ expr
> ...
>
>So, AFAICT, your above expression should be covered by the POSIX awk syntax
>and not be a syntax error.

Interesting. Thanks for researching this for me.

--
Shikata ga nai...

Andrew Schorr

unread,
Dec 24, 2014, 2:55:36 PM12/24/14
to
On Friday, December 19, 2014 11:48:09 AM UTC-5, Kenny McCormack wrote:
> Observe:
>
> $ gawk '$2 == $3 == $4'
> gawk: $2 == $3 == $4
> gawk: ^ syntax error
> $
>
> This works if you put parens around either sub-expression [*].

This is a very odd expression, and even if it weren't a syntax error, it might not behave the way one might naively expect. The current gawk grammar does not support this expression, and neither does BWK awk. I think this is not going to be changed. The benefit of forcing the user to add parentheses is that he/she might then understand what this expression actually means.

Regards,
Andy

Janis Papanagnou

unread,
Dec 24, 2014, 3:18:54 PM12/24/14
to
I understand your safety concern. But if that's reported as _syntax_ error
it's (as far as I see it) non-conforming to the POSIX standard. A warning
would be more appropriate, IMO.

Janis

>
> Regards, Andy
>

Janis Papanagnou

unread,
Dec 27, 2014, 4:54:39 AM12/27/14
to
On 19.12.2014 17:48, Kenny McCormack wrote:
> Observe:
>
> $ gawk '$2 == $3 == $4'
> gawk: $2 == $3 == $4
> gawk: ^ syntax error
> $
>
> This works if you put parens around either sub-expression [*].
>
> Note that this works as written (with the appropriate mods to satisfy the
> compiler) in C [*]. Shouldn't gawk have pretty much the same rules as C?

I've been informed about an interesting observation; the "Precedence" table
in POSIX Awk says about the associativity of '==': "N/A". So while by syntax
rules alone it would be possible to build chains of equality operations it's
ruled out implicitly by this "N/A" statement. And this is different from C;
in the K&R operator precedence table there's left associativity defined.

Janis

Kenny McCormack

unread,
Dec 27, 2014, 7:55:22 AM12/27/14
to
In article <m7lvku$top$1...@news.m-online.net>,
Janis Papanagnou <janis_pa...@hotmail.com> wrote:
...
>I've been informed about an interesting observation; the "Precedence" table
>in POSIX Awk says about the associativity of '==': "N/A". So while by syntax
>rules alone it would be possible to build chains of equality operations it's
>ruled out implicitly by this "N/A" statement. And this is different from C;
>in the K&R operator precedence table there's left associativity defined.

Very interesting.

--
"Women should not be enlightened or educated in any way. They should be
segregated because they are the cause of unholy erections in holy men.

-- Saint Augustine (354-430) --

0 new messages