Are there any AWK syntax checkers?
Asked Answered
B

3

10

Are there any AWK syntax checkers? I'm interested in both minimal checkers that only flag syntax errors and more extensive checkers along the lines of lint.

It should be a static checker only, not dependent on running the script.

Belita answered 23/8, 2011 at 17:34 Comment(0)
D
11

If you prefix your Awk script with BEGIN { exit(0) } END { exit(0) }, you're guaranteed that none of your of code will run. Exiting during BEGIN and END prevents other begin and exit blocks from running. If Awk returns 0, your script was fine; otherwise there was a syntax error.

If you put the code snippet in a separate argument, you'll get good line numbers in the error messages. This invocation...

gawk --source 'BEGIN { exit(0) } END { exit(0) }' --file syntax-test.awk

Gives error messages like this:

gawk: syntax-test.awk:3:   x = f(
gawk: syntax-test.awk:3:         ^ unexpected newline or end of string

GNU Awk's --lint can spot things like global variables and undefined functions:

gawk: syntax-test.awk:5: warning: function `g': parameter `x' shadows global variable
gawk: warning: function `f' called but never defined

And GNU Awk's --posix option can spot some compatibility problems:

gawk: syntax-test.awk:2: error: `delete array' is a gawk extension

Update: BEGIN and END

Although the END { exit(0) } block seems redundant, compare the subtle differences between these three invocations:

$ echo | awk '
  BEGIN { print("at begin") }
  /.*/ { print("found match") }
  END { print("at end") }'
at begin
found match
at end

$ echo | awk '
  BEGIN { exit(0) }
  BEGIN { print("at begin") }
  /.*/ { print("found match") }
  END { print("at end") }'
at end

$ echo | awk '
  BEGIN { exit(0) } END { exit(0) }
  BEGIN { print("at begin") }
  /.*/ { print("found match") }
  END { print("at end") }'

In Awk, exiting during BEGIN will cancel all other begin blocks, and will prevent matching against any input. Exiting during END is the only way to prevent all other event blocks from running; that's why the third invocation above shows that no print statements were executed. The GNU Awk User's Guide has a section on the exit statement.

Deltadeltaic answered 27/8, 2011 at 3:11 Comment(3)
The END { exit(0) } doesn't really seem necessary. Or am I missing something?Belita
@Michael J. Barber: I've added an update to my answer to show the difference that it makes.Deltadeltaic
@Michael J. Barber: :) No problem.Deltadeltaic
L
2

GNU awk appears to have a --lint option.

Leoni answered 23/8, 2011 at 18:13 Comment(2)
The documentation for which suggests it focuses on differences between awk and gawk. Is it actually a syntax checker or a compatibility check? Either way it doesn't quite fit my needs, as it does some checks at runtime; I've edited the question to specify that.Belita
Oh, and +1 for the suggestion, it's certainly better than the nothing I have.Belita
M
0

For a minimal syntax checker, which stops at the first error, try awk -f prog < /dev/null.

Meza answered 23/8, 2011 at 18:17 Comment(2)
but that may execute some code within the awk script despite getting no file input. For example BEGIN { system("rm importantFile") }Anastos
Indeed, that looks definitely unsafe.Leland

© 2022 - 2024 — McMap. All rights reserved.