A niggling bug in the GAWK parser

32 views
Skip to first unread message

Kenny McCormack

unread,
Oct 5, 2021, 12:27:57 PM10/5/21
to
I got bit by this today:

print SomeVar > stime ? "Error!" : "OK"

Perfectly reasonable, right? But GAWK flags this as a syntax error.
(Literally, as "syntax error - and see below)

And I know why. I also know the fix (which is to parenthesize the
conditional expression). So, I don't need anyone writing in to tell me why
it happens or what the fix is.

The point is, it just seems to me that the error message ought to be
clearer. I mean, this one has been around a long time, and it seems like
it could be caught better. In fact, it took me more than a few moments to
figure out that it wasn't a syntax error (in the conventional sense).
I.e., there wasn't really anything wrong with my code - I hadn't misspelled
anything or anything like that. Rather, it is just a (well-known) bugaboo.

Note: Tested with GAWK 5.0.1 - it is still there and still flagged as
"syntax error".

Anyway, just something to consider. Note that C compilers these days are
getting really explicit about telling you exactly what they think is wrong
with your code - and how to fix it. It seems GAWK might take a cue from
that...

--
To be evangelical is to spend every waking moment hovering around
two emotional states: fear and rage. Evangelicals are seriously the
angriest and most vicious bunch of self-pitying, constantly-moaning
whinybutts I've ever encountered.

Mack The Knife

unread,
Oct 6, 2021, 8:05:11 AM10/6/21
to
From https://www.gnu.org/software/gawk/manual/html_node/Usenet.html:

| Please do not try to report bugs in gawk by posting to the Usenet/Internet
| newsgroup comp.lang.awk. Although some of the gawk developers occasionally
| read this news group, the primary gawk maintainer no longer does. Thus
| it’s virtually guaranteed that he will not see your posting.

Also, you might think about the kind of work involved in such changes
to the parser, and in particular about the size and nature (paid)
of the teams doing GCC development vs. the size and nature (volunteer)
of the team doing gawk development.

'nuff said.

In article <sjhuea$2v60b$1...@news.xmission.com>,

Kaz Kylheku

unread,
Oct 6, 2021, 11:21:27 AM10/6/21
to
On 2021-10-05, Kenny McCormack <gaz...@shell.xmission.com> wrote:
> I got bit by this today:
>
> print SomeVar > stime ? "Error!" : "OK"
>
> Perfectly reasonable, right? But GAWK flags this as a syntax error.
> (Literally, as "syntax error - and see below)
>
> And I know why. I also know the fix (which is to parenthesize the
> conditional expression). So, I don't need anyone writing in to tell me why
> it happens or what the fix is.
>
> The point is, it just seems to me that the error message ought to be
> clearer. I mean, this one has been around a long time, and it seems like

I happen have a git directory with gawk 4.x sources from way back when,
all set up to build.

I tried a tiny patch, just for discussion:

$ ./gawk 'BEGIN { print SomeVar > stime ? "Error!" : "OK" }'
gawk: cmd. line:1: BEGIN { print SomeVar > stime ? "Error!" : "OK" }
gawk: cmd. line:1: ^ syntax error
gawk: cmd. line:1: BEGIN { print SomeVar > stime ? "Error!" : "OK" }
gawk: cmd. line:1: ^ I/O redirection possibly confused with relational operator
gawk: cmd. line:1: BEGIN { print SomeVar > stime ? "Error!" : "OK" }
gawk: cmd. line:1: ^ syntax error


It is:

$ git diff awkgram.y
diff --git a/awkgram.y b/awkgram.y
index 0b7e29f3..41952248 100644
--- a/awkgram.y
+++ b/awkgram.y
@@ -1312,6 +1312,12 @@ output_redir
yyerror(_("multistage two-way pipelines don't work"));
$$ = list_prepend($3, $1);
}
+ | IO_OUT common_exp error
+ {
+ yyerror(_("I/O redirection possibly confused with relational operator"));
+ yyerrok;
+ $$ = NULL;
+ }
;

if_statement

See what you make of it.

Kenny McCormack

unread,
Oct 6, 2021, 12:14:23 PM10/6/21
to
In article <202110060...@kylheku.com>,
Kaz Kylheku <480-99...@kylheku.com> wrote:
...
Very interesting. Thanks.

--
No puppet.
No puppet.
You're a puppet.

Ed Morton

unread,
Oct 6, 2021, 12:53:00 PM10/6/21
to
On 10/6/2021 7:05 AM, Mack The Knife wrote:
<snip>
> Kenny McCormack <gaz...@shell.xmission.com> wrote:
>> I got bit by this today:
>>
>> print SomeVar > stime ? "Error!" : "OK"
>>
>> Perfectly reasonable, right?

Wrong

> But GAWK flags this as a syntax error.

Good. Any unparenthesized expression on the right side of input or
output redirection is undefined behavior per POSIX and a syntax error is
a good response to that code.

Ed.
Reply all
Reply to author
Forward
0 new messages