Algol-style vs C-style semicolons - (nf)
Andrew Klossner
andrew at orca.UUCP
Sun Jun 10 05:53:13 AEST 1984
> 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 at rand-relay) [ARPA]
More information about the Comp.lang.c
mailing list