• GAWK mystery parses

    From gazelle@gazelle@shell.xmission.com (Kenny McCormack) to comp.lang.awk on Fri Oct 3 04:36:17 2025
    From Newsgroup: comp.lang.awk

    The topic for today is: What can we get away with for clause 3 of a "for" statement.

    Observe (notice that the print statement is *inside* the for statement):

    First, we use a print statement as clause 3:
    % gawk4 'BEGIN { for (i=1; i<=5; print "i =",i++); }'
    i = 1
    i = 2
    i = 3
    i = 4
    i = 5

    But here we show that print really is a statement and not an expression:
    % gawk4 'BEGIN { print (print "help") }'
    gawk4: cmd. line:1: BEGIN { print (print "help") }
    gawk4: cmd. line:1: ^ syntax error
    %
    (You get the same error with or without the parens)

    Now, the point here is that I always thought that clause 3 in a for
    statement had to be an expression (like clauses 1 and 2). But it seems we
    can get away with having a statement there - if it is simple enough.

    Note that in C, there's no real distinction between a simple statement and
    an expression; for example, printf() is usually used as if it was a
    statement, but it is in fact a function that returns a value, i.e., an expression. So, in C, clause 3 of the for statement is usually thought of
    as a statement, but is, in fact, an expression.

    Next, we press our luck:

    This works:
    % gawk4 'BEGIN { for (i=1; i<=5;) if (i++ == 3) print "three" }'
    three
    %

    But this doesn't:
    % gawk4 'BEGIN { for (i=1; i<=5; if (i++ == 3) print "three"); }'
    gawk4: cmd. line:1: BEGIN { for (i=1; i<=5; if (i++ == 3) print "three"); } gawk4: cmd. line:1: ^ syntax error
    gawk4: cmd. line:1: BEGIN { for (i=1; i<=5; if (i++ == 3) print "three"); } gawk4: cmd. line:1: ^ syntax error
    %

    Why not?
    --
    You are again heaping damnation upon your own head by your statements.

    - Rick C Hodgin -

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Kaz Kylheku@643-408-1753@kylheku.com to comp.lang.awk on Fri Oct 3 06:20:34 2025
    From Newsgroup: comp.lang.awk

    On 2025-10-03, Kenny McCormack <gazelle@shell.xmission.com> wrote:
    The topic for today is: What can we get away with for clause 3 of a "for" statement.

    Observe (notice that the print statement is *inside* the for statement):

    First, we use a print statement as clause 3:
    % gawk4 'BEGIN { for (i=1; i<=5; print "i =",i++); }'
    i = 1
    i = 2
    i = 3
    i = 4
    i = 5

    That's interesting, and useless; yet, stupidly, you cannot have comma expressions like for (i = 0, j = 0; ...

    What you found is not portable:

    $ mawk 'BEGIN { for (i=1; i<=5; print "i =",i++); }'
    mawk: line 1: syntax error at or near print
    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From gazelle@gazelle@shell.xmission.com (Kenny McCormack) to comp.lang.awk on Fri Oct 3 07:06:07 2025
    From Newsgroup: comp.lang.awk

    In article <20251002231443.863@kylheku.com>,
    Kaz Kylheku <643-408-1753@kylheku.com> wrote:
    On 2025-10-03, Kenny McCormack <gazelle@shell.xmission.com> wrote:
    The topic for today is: What can we get away with for clause 3 of a "for"
    statement.

    Observe (notice that the print statement is *inside* the for statement):

    First, we use a print statement as clause 3:
    % gawk4 'BEGIN { for (i=1; i<=5; print "i =",i++); }'
    i = 1
    i = 2
    i = 3
    i = 4
    i = 5

    That's interesting, and useless; yet, stupidly, you cannot have comma >expressions like for (i = 0, j = 0; ...

    TAWK allows that syntax, but only in the "for" statement. It doesn't allow
    it as a general expression (outside of "for"), like C does.

    What you found is not portable:

    I'm not surprised. I was just amazed it works at all.
    --
    Which of these is the crazier bit of right wing lunacy?
    1) We've just had another mass shooting; now is not the time to be talking about gun control.

    2) We've just had a massive hurricane; now is not the time to be talking about climate change.
    --- Synchronet 3.21a-Linux NewsLink 1.2