awk syntax

Arnold Robbins at
Wed May 15 02:48:07 AEST 1991

Submitted-by: at (Arnold Robbins)

>In article <1991May11.224436.25175 at> arnold at writes:
>> (How do I know that        awk 'BEGIN { print "hi" } ; END { print "bye" }'
>> is legal while             awk 'BEGIN { print "hi" }   END { print "bye" }'
>> isn't?  Presenting a grammar for the language is almost a necessity...)

In article <1991May13.222855.9433 at> peter at (Peter da Silva) writes:
>It isn't? I use the latter all the time!

History lesson time.   First of all, posix awk is "new" awk, not old awk.
It is based on the awk in the 1988 book by Aho, Weinberger and Kernighan.
It has some additional features that have gone in to both att & gnu awk.

One of the things that happened when new awk was first realeased was a lot
of cleaning up and consistencizing (if I may coin a term) of the awk language.
In particular, rules had to be seperated by either a newline or a semi-colon,
just like the statements inside an action.  Here's a real live example
from my V.3.2 system:

	Script started on Tue May 14 12:25:28 1991
	audiofax1> rlogin tiktok
	ESIX System 5.3.2 Rev.D
	Copyright (C) 1984, 1986, 1987, 1988 AT&T
	Copyright (C) 1987, 1988 Microsoft Corp.
	Copyright (C) 1988, 1989, 1990 Everex Systems, Inc.
	All Rights Reserved
	Login last used: Tue May 14 12:24:29 1991
	tiktok> nawk 'BEGIN { print "hi" } ; END { print "bye" }' /dev/null
	tiktok> nawk 'BEGIN { print "hi" }  END { print "bye" }' /dev/null 
	nawk: syntax error at source line 1
	 context is
	        BEGIN { print "hi" }  >>>  END <<<  { print "bye" }
	nawk: bailing out at source line 1
	Connection closed.
	script done on Tue May 14 12:26:28 1991

Based on a cursory reading of the grammar in the posix spec, this rule

Alas, some time back, backwards compatibility reared it's ugly head within
AT&T, and for V.4 nawk, Brian Kernighan "fixed" things so that the seperator
is no longer necessary.  (This was at the request of the System V folks.)
David Trueman went ahead and fixed gawk to be the same way (adding heavily
to the number of shift/reduce conflicts in the grammar).

So, the upshot is that technically, leaving out the semi-colon or newline
is not legal, but most likely you can get away with it.
Arnold Robbins				 AudioFAX, Inc. | Threads are the
2000 Powers Ferry Road, Suite 200 / Marietta, GA. 30067 | lack of an idea.
INTERNET: arnold at  Phone:   +1 404 618 4281 |     -- Rob Pike
UUCP:	  emory!audfax!arnold  Fax-box: +1 404 618 4581 |

[ I think this discussion is getting more towards the realm of 
  comp.unix.questions.  I'm keeping this part of it here because it is
   related to *nix standards and a good example of how fun they are. -- mod ]
Volume-Number: Volume 23, Number 68

More information about the Comp.std.unix mailing list