Skip to content

awk: a comment line next to a newline breaks the statement ("invalid statement start") #164

Open

Description

@sylvestre

A # comment on a line of its own is rejected as a syntax error when it sits
between two statements that are separated by ; or by another comment line,
after if (...), after switch (...) {, or inside a function call argument
list. Real-world awk scripts are full of comment lines, so this is the most
frequent parse failure in the GNU test suite: 31 tests stop on it.

Reproduction

Rust (incorrect)

$ printf 'BEGIN {\n    x = 1;\n    # set y\n    y = 2\n    print x, y\n}\n' > c.awk
$ ./target/release/awk -f c.awk
Error: Syntax error
   ╭─[ c.awk:3:12 ]
 3 │     # set y
   │            ╰── Unexpected token: invalid statement start.

GNU (correct)

$ gawk -f c.awk
1 2

Other shapes that fail the same way:

BEGIN {
# first comment line
# second comment line
print 1 }

BEGIN { if (1)
    # comment
    print 2 }

BEGIN { printf("%s\n",
    # comment
    "x") }

Test

#[test]
fn comment_lines_between_statements() {
    ucmd()
        .arg("BEGIN {\n  x = 1;\n  # one\n  # two\n  y = 2\n  print x, y\n}")
        .succeeds()
        .stdout_only("1 2\n");
}

#[test]
fn comment_line_after_if_condition() {
    ucmd()
        .arg("BEGIN { if (1)\n  # why\n  print \"yes\" }")
        .succeeds()
        .stdout_only("yes\n");
}

#[test]
fn comment_line_in_argument_list() {
    ucmd()
        .arg("BEGIN { printf(\"%s-%s\\n\",\n  # first\n  \"a\",\n  # second\n  \"b\") }")
        .succeeds()
        .stdout_only("a-b\n");
}

Exposed by the GNU awk test suite: aadelete2, arrayind1, arrayind3, arynasty,
arynocls, close_status, clsflnam, delarpm2, fldterm, forcenum, forref, fpat1,
fpat4, gsubtst2, hsprint, indirectbuiltin, iolint, leaddig, lintindex, mdim3,
mdim8, nasty, profile10, profile11, profile14, profile17, regx8bit, sortglos,
strnum2, typeof6, wjposer1.

Activity

  1. added
    bug
    good first issue
    gnu-compat
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggnu-compatgood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions