Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.1 (Tek) 9/26/83; site orca.UUCP
Path: utzoo!watmath!clyde!burl!ulysses!mhuxl!houxm!houxz!vax135!cornell!uw-beaver!tektronix!orca!andrew
From: andrew@orca.UUCP (Andrew Klossner)
Newsgroups: net.lang.c
Subject: Re: Algol-style vs C-style semicolons - (nf)
Message-ID: <886@orca.UUCP>
Date: Sat, 9-Jun-84 15:53:13 EDT
Article-I.D.: orca.886
Posted: Sat Jun  9 15:53:13 1984
Date-Received: Sun, 10-Jun-84 07:05:31 EDT
References: <3000028@uokvax.UUCP> <270@hou2g.UUCP>
Organization: Tektronix, Wilsonville OR
Lines: 68

> Not wishing to drag out a rather uninteresting discussion but unable to
> resist pontificating on language design matters, I submit the following
> on the semicolon controversy.  It is true that semicolons are not, strictly
> speaking, statement terminators in C, because not all statements end with
> them.  In fact, a quick look at the grammar in the C Reference Manual shows
> that only the following statements are terminated with semicolons:
> 
> 	expression ;
> 	do statement while ( expression ) ;  /* This one was new to me! */
> 	return expr ;
> 	goto identifier ;
> 	continue ;
> 	break ;
> 	;			/* A null statement */
> 
> The rest, including for loops, while loops and compound statements are not.
> It is the presence of null statements that confuses the matter by making
> people think that
> 
> 	while ( expr ) statement ;
> 
> is one statement and is terminated with a semicolon, when in fact it is
> two statements -- a while loop followed by a null statement.
> 
> Can anyone explain why, of all the loop statements, only do-while is
> terminated with semicolon?  The required closing right-paren would seem
> sufficient for easy parsing.

You've become confused about the grammar.  It isn't really true that
the other loops may terminate with something other than semicolon or
right-brace.

Here are the other for loops, while loops, and compound statements:

	compound-statement
	if (expression) statement
	if (expression) statement else statement
	while (expression) statement
	for (expression-1-opt; expression-2-opt; expression-3-opt) statement
	switch (expression) statement
	case constant-expression: statement
	default: statement
	identifier: statement

compound-statement:
	{ declaration-list-opt statement-list-opt }

Notice that, except for "compound-statement", all of these for loops,
while loops, and compound statements end in "statement".  Every
statement-producing rule that doesn't end in "statement" ends in
semicolon.  Through the recursive nature of programming language
grammars, this ensures that each statement construct will end in a
single semicolon, unless it ends in compound-statement in which case it
end in a right-brace.

Consider the alternative, e.g., "if (expression) statement;".  This
would lead to statements such as

	if (a==0) b=5;;

with mandated double semicolons, and the conventional

	if (a==0) b=5;

would be a syntax error.

  -- Andrew Klossner   (decvax!tektronix!orca!andrew)      [UUCP]
                       (orca!andrew.tektronix@rand-relay)  [ARPA]