goawk icon indicating copy to clipboard operation
goawk copied to clipboard

Incorrect parsing of complex ++ expressions

Open benhoyt opened this issue 3 years ago • 4 comments

Two cases:

  1. echo 2 3 4 | goawk '{ $$0++; print $0 }' prints 2 4 4 instead of 3. I think this is happening because we parse this as ($($0))++ rather than ($($0++)).
  2. BEGIN { $0="3 4 5 6 7 8 9"; a=3; print $$a++++; print } gives a parsing error instead of printing 7\n3 4 6 6 8 8 9.

There are two commented-out (TODO) tests in interp/interp_test.go for this, as well as a commented-out Gawk test (parse1).

benhoyt avatar Dec 24 '21 04:12 benhoyt

I've started playing with this on the fix-incr-decr branch.

benhoyt avatar Dec 25 '21 08:12 benhoyt

Can you walk through what is supposed to happen in 1? I don't understand it, sorry.

vegarsti avatar Aug 18 '22 21:08 vegarsti

gawk, original-awk, and mawk all print 3 for that example. It's because they parse the expression as $0++ (which returns the current value of $0 but also post-increments $0); then the outer $ does fetches $2 but drops the result.

Whereas GoAWK parses it differently, so executes the inner $0 first (which is 2 3 4), then another $, which fetches the second field 2 (the 3), then the ++ increments that field, creating 2 4 4.

So it's a parsing/precedence issue. Hope that helps!

benhoyt avatar Aug 18 '22 21:08 benhoyt

That helps a lot, thanks!

vegarsti avatar Aug 18 '22 21:08 vegarsti

Was this solved by the newest release? Specifically https://github.com/benhoyt/goawk/pull/172

vegarsti avatar Apr 12 '23 06:04 vegarsti

@vegarsti No, unfortunately not -- this is a different issue.

benhoyt avatar Apr 12 '23 14:04 benhoyt

Okay!

vegarsti avatar Apr 12 '23 16:04 vegarsti

This also produces wrong results:

./goawk 'BEGIN { $0="3 4 5 6 7 8 9"; a=3; print ($($a++)++); print $0; print a }' prints 7\n3 4 7 6 7 8 9\n3, whereas gawk 'BEGIN { $0="3 4 5 6 7 8 9"; a=3; print ($($a++)++); print $0; print a }' prints 7\n3 4 6 6 8 8 9\n3

fioriandrea avatar Oct 09 '23 15:10 fioriandrea