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...
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.
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
Kenny McCormack <gazelle@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.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 498 |
Nodes: | 16 (3 / 13) |
Uptime: | 02:12:26 |
Calls: | 9,821 |
Calls today: | 9 |
Files: | 13,757 |
Messages: | 6,190,249 |