[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: A bug in parsing of conditional ops?
From: |
arnold |
Subject: |
Re: A bug in parsing of conditional ops? |
Date: |
Tue, 09 Jul 2024 08:04:34 -0600 |
User-agent: |
Heirloom mailx 12.5 7/5/10 |
Hi.
Denys Vlasenko <dvlasenk@redhat.com> wrote:
> Good day.
>
> GNU Awk 5.3.0, API 4.0, PMA Avon 8-g1, (GNU MPFR 4.1.0-p13, GNU MP 6.2.1)
> here.
>
> Initially I thought that I found an error in docs
> which state that the precedence table is:
> ...................
> < > <= >= == !=
> ~ !~
> in
> &&
> ||
> ?:
> = += -= *= /= %= ^=
> since this unexpectedly works:
>
> $ awk 'BEGIN { print 3==v=3, v}'
> 1 3
>
> According to the table, it should not, (3==v)=3 is not a valid assignment.
It's parsed (as you noted) as 3 == (v = 3)
> I thought that maybe it's just an error in docs,
> == and = in fact have the same precedence and are both right-associative,
> this would explain why the above works (it is treated as "3==(v=3)"),
> and this also works:
>
> $ awk 'BEGIN { print v=3==3, v}'
> 1 1
This is parsed as v = (3 == 3). C parses it the same way.
> But there is more: more than one comparison op fails to parse:
>
> $ awk 'BEGIN { print 3==3==3}'
> awk: cmd. line:1: BEGIN { print 3==3==3}
> awk: cmd. line:1: ^ syntax error
>
> $ awk 'BEGIN { print 3==3!=3}'
> awk: cmd. line:1: BEGIN { print 3==3!=3}
> awk: cmd. line:1: ^ syntax error
Brian Kernighan's awk treats it as a syntax error. Mawk
actually prints 0 for the first one and 1 for the second.
It's probably more correct than gawk is here.
> This suggests that parser is just broken somehow,
> and "3==v=3" unexpectedly working is just a glitch
> due to that breakage.
Well, like C, awk is an expression language. But I won't disagree
that it might be a bug.
The precedence is done by use of various productions in the grammar that
define expressions.
The grammars for awk implementations are notoriously difficult, and in
fact your cases get different results on different versions.
> What do you think?
Although it's possible that things could be better here, I'm not about
to mess with a grammar that's been largely unchanged for more than 30
years; it is what it is.
Thanks,
Arnold