• Syntax of function calls without arguments?

    From Janis Papanagnou@janis_papanagnou+ng@hotmail.com to comp.lang.awk on Thu May 11 04:19:16 2023
    From Newsgroup: comp.lang.awk

    In an old paper "Awk - A Pattern Scanning and Processing Language"[*]
    from the authors of Awk I read:

    "Awk also provides the arithmetic functions sqrt, log, exp, and
    int, for square root, base e logarithm, exponential, and integer
    part of their respective arguments.
    The name of one of these built-in functions, _without argument or
    parentheses_, stands for the value of the function on the whole
    record."

    POSIX[**] says:

    "Although the grammar (see Grammar) permits built-in functions to
    appear _with no arguments or parentheses_, [...], such use is
    undefined."

    (_emphasis_ in the quotes added by me).

    GNU Awk (for example) typically[***] returns errors:

    $ awk '{print sqrt}'
    awk: cmd. line:1: {print sqrt}
    awk: cmd. line:1: ^ syntax error
    $ awk '{print sqrt()}'
    awk: cmd. line:1: {print sqrt()}
    awk: cmd. line:1: ^ 0 is invalid as number of arguments for sqrt

    How do other Awks behave? - I'd assume that "oawk" and "nawk" might
    support that feature, but does any other awk implementation support
    that?

    Janis

    [*] https://people.eecs.berkeley.edu/~clancy/sp.unix.stuff/awk
    [**] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html
    [***] With length($0), length(), length as a singular(?) exception.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Keith Thompson@Keith.S.Thompson+u@gmail.com to comp.lang.awk on Wed May 10 19:34:47 2023
    From Newsgroup: comp.lang.awk

    Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
    In an old paper "Awk - A Pattern Scanning and Processing Language"[*]
    from the authors of Awk I read:

    "Awk also provides the arithmetic functions sqrt, log, exp, and
    int, for square root, base e logarithm, exponential, and integer
    part of their respective arguments.
    The name of one of these built-in functions, _without argument or
    parentheses_, stands for the value of the function on the whole
    record."

    POSIX[**] says:

    "Although the grammar (see Grammar) permits built-in functions to
    appear _with no arguments or parentheses_, [...], such use is
    undefined."

    (_emphasis_ in the quotes added by me).

    GNU Awk (for example) typically[***] returns errors:

    $ awk '{print sqrt}'
    awk: cmd. line:1: {print sqrt}
    awk: cmd. line:1: ^ syntax error
    $ awk '{print sqrt()}'
    awk: cmd. line:1: {print sqrt()}
    awk: cmd. line:1: ^ 0 is invalid as number of arguments for sqrt

    How do other Awks behave? - I'd assume that "oawk" and "nawk" might
    support that feature, but does any other awk implementation support
    that?

    Janis

    [*] https://people.eecs.berkeley.edu/~clancy/sp.unix.stuff/awk
    [**] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html [***] With length($0), length(), length as a singular(?) exception.

    gawk and mawk report a syntax error.

    original-awk on Ubuntu accepts it:

    $ original-awk --version
    awk version 20180827
    $ echo 2 | original-awk '{print sqrt}'
    1.41421
    $
    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    Working, but not speaking, for XCOM Labs
    void Void(void) { Void(); } /* The recursive call of the void */
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Ivan Shmakov@ivan@siamics.netNOSPAM.invalid to comp.lang.awk on Thu May 11 18:10:56 2023
    From Newsgroup: comp.lang.awk

    On 2023-05-11, Janis Papanagnou wrote:

    GNU Awk (for example) typically[***] returns errors:

    $ awk '{print sqrt}'
    awk: cmd. line:1: {print sqrt}
    awk: cmd. line:1: ^ syntax error
    $ awk '{print sqrt()}'
    awk: cmd. line:1: {print sqrt()}
    awk: cmd. line:1: ^ 0 is invalid as number of arguments for sqrt

    How do other Awks behave? - I'd assume that "oawk" and "nawk" might
    support that feature, but does any other awk implementation support
    that?

    The one included in BusyBox 1.35.0 doesn't seem to accept it:

    $ busybox awk '1 { print sqrt }'
    awk: cmd. line:1: Unexpected token
    $

    The one from NetBSD, http://man.netbsd.org/awk.1 , does:

    $ printf 2\\n | awk '1 { print sqrt }'
    1.41421
    $

    [*] https://people.eecs.berkeley.edu/~clancy/sp.unix.stuff/awk
    [**] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html [***] With length($0), length(), length as a singular(?) exception.
    --
    FSF associate member #7257 http://am-1.org/~ivan/
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Ben Bacarisse@ben.usenet@bsb.me.uk to comp.lang.awk on Thu May 11 20:54:57 2023
    From Newsgroup: comp.lang.awk

    Ivan Shmakov <ivan@siamics.netNOSPAM.invalid> writes:

    On 2023-05-11, Janis Papanagnou wrote:

    GNU Awk (for example) typically[***] returns errors:

    $ awk '{print sqrt}'
    awk: cmd. line:1: {print sqrt}
    awk: cmd. line:1: ^ syntax error
    $ awk '{print sqrt()}'
    awk: cmd. line:1: {print sqrt()}
    awk: cmd. line:1: ^ 0 is invalid as number of arguments for sqrt

    How do other Awks behave? - I'd assume that "oawk" and "nawk" might support that feature, but does any other awk implementation support
    that?

    The one included in BusyBox 1.35.0 doesn't seem to accept it:

    $ busybox awk '1 { print sqrt }'
    awk: cmd. line:1: Unexpected token
    $

    The one from NetBSD, http://man.netbsd.org/awk.1 , does:

    $ printf 2\\n | awk '1 { print sqrt }'
    1.41421
    $

    As does a version of Unix V7 awk running on a PDP11 simulator. It's
    obviously an old feature:

    $ echo 2 | awk '{print sqrt}'
    1.41421

    Curiously, it's only documented to apply to the length function. man
    awk says

    The built-in function length returns the length of its argu-
    ment taken as a string, or of the whole line if no argument.
    There are also built-in functions exp, log, sqrt, and int.
    --
    Ben.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Keith Thompson@Keith.S.Thompson+u@gmail.com to comp.lang.awk on Thu May 11 14:40:06 2023
    From Newsgroup: comp.lang.awk

    Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
    Ivan Shmakov <ivan@siamics.netNOSPAM.invalid> writes:

    On 2023-05-11, Janis Papanagnou wrote:

    GNU Awk (for example) typically[***] returns errors:

    $ awk '{print sqrt}'
    awk: cmd. line:1: {print sqrt}
    awk: cmd. line:1: ^ syntax error
    $ awk '{print sqrt()}'
    awk: cmd. line:1: {print sqrt()}
    awk: cmd. line:1: ^ 0 is invalid as number of arguments for sqrt

    How do other Awks behave? - I'd assume that "oawk" and "nawk" might
    support that feature, but does any other awk implementation support
    that?

    The one included in BusyBox 1.35.0 doesn't seem to accept it:

    $ busybox awk '1 { print sqrt }'
    awk: cmd. line:1: Unexpected token
    $

    The one from NetBSD, http://man.netbsd.org/awk.1 , does:

    $ printf 2\\n | awk '1 { print sqrt }'
    1.41421
    $

    As does a version of Unix V7 awk running on a PDP11 simulator. It's obviously an old feature:

    $ echo 2 | awk '{print sqrt}'
    1.41421

    Curiously, it's only documented to apply to the length function. man
    awk says

    The built-in function length returns the length of its argu-
    ment taken as a string, or of the whole line if no argument.
    There are also built-in functions exp, log, sqrt, and int.

    Just from that description, it's not clear that `length()` and `length`
    are both valid.
    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    Working, but not speaking, for XCOM Labs
    void Void(void) { Void(); } /* The recursive call of the void */
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Ben Bacarisse@ben.usenet@bsb.me.uk to comp.lang.awk on Fri May 12 01:46:38 2023
    From Newsgroup: comp.lang.awk

    Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

    Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
    Ivan Shmakov <ivan@siamics.netNOSPAM.invalid> writes:

    On 2023-05-11, Janis Papanagnou wrote:

    GNU Awk (for example) typically[***] returns errors:

    $ awk '{print sqrt}'
    awk: cmd. line:1: {print sqrt}
    awk: cmd. line:1: ^ syntax error
    $ awk '{print sqrt()}'
    awk: cmd. line:1: {print sqrt()}
    awk: cmd. line:1: ^ 0 is invalid as number of arguments for sqrt

    How do other Awks behave? - I'd assume that "oawk" and "nawk" might
    support that feature, but does any other awk implementation support
    that?

    The one included in BusyBox 1.35.0 doesn't seem to accept it:

    $ busybox awk '1 { print sqrt }'
    awk: cmd. line:1: Unexpected token
    $

    The one from NetBSD, http://man.netbsd.org/awk.1 , does:

    $ printf 2\\n | awk '1 { print sqrt }'
    1.41421
    $

    As does a version of Unix V7 awk running on a PDP11 simulator. It's
    obviously an old feature:

    $ echo 2 | awk '{print sqrt}'
    1.41421

    Curiously, it's only documented to apply to the length function. man
    awk says

    The built-in function length returns the length of its argu-
    ment taken as a string, or of the whole line if no argument.
    There are also built-in functions exp, log, sqrt, and int.

    Just from that description, it's not clear that `length()` and `length`
    are both valid.

    Indeed. The POSIX specification makes it clearer with

    length[([s])] and "... if there is no argument"

    The gawk man page writes length([s]) so its "if s is not supplied" is
    not quite so clear. mawk's man page make no reference to the special
    case at all.

    I find it interesting that the special case of length applying to $0 has
    been retained in modern AWKs, but not the general case. I'd have
    thought it would have been simpler to do what the old AWK did. Maybe
    too many bugs occurred from writing 'exp', but 'length' was too useful a shorthand to outlaw.
    --
    Ben.
    --- Synchronet 3.20a-Linux NewsLink 1.114