archives

« Bugzilla Issues Index

#4099 — 13.6: ambiguity re do-while statement


In 13.6 "Iteration Statements",
the syntax of a do-while statement is:
do Statement while ( Expression ) ;_opt
(ignoring parameters).

The optionality of the semicolon leads to a grammatical ambiguity. E.g., consider
do {} while (0) ;
In a StatementList context, this could be a do-while statement in which the semicolon is not omitted, or it could be a do-while statement in which the semicolon *is* omitted, followed by an empty statement.


An alternative approach would be to remove the "opt" subscript in the BNF, and extend ASI to handle cases in which the semicolon is omitted. (E.g., in 11.9.1, add a bullet to rule 1: "The token before the offending token is the right parenthesis of a do-while statement." Or is this a broader condition than what web reality allows?)


I think ASI is the right way to do it.

Unfortunately, I can't think was any way to describe it that is substantially different from you suggest and that makes inserting of the ; conditional upon recognizing the ) as part of a do-while statement. Yet in order to recognize a do-while the ; first needs to be present. Seems a little to circular.

The best I've come up with is:

• The previous token is ) and inserting ; will allow the token input stream immediately preceding the offending token to be parsed as a do-while statement (13.6.1).


(In reply to Allen Wirfs-Brock from comment #1)
>
> ... that makes inserting of the ; conditional upon recognizing the )
> as part of a do-while statement. Yet in order to recognize a do-while
> the ; first needs to be present. Seems a little to circular.

Hm, yeah, okay.

> The best I've come up with is:
>
> • The previous token is ) and inserting ; will allow the token input stream
> immediately preceding the offending token to be parsed as a do-while
> statement (13.6.1).

Well, ASI's 'additional overriding condition' uses wording such as
"the semicolon would then be parsed as X"
and
"that semicolon would become Y",
so you could use similar wording:

• The previous token is ) and the inserted semicolon would be parsed as
the terminating semicolon of a do-while statement.

which is a bit simpler.

Moreover, you could drop "The previous token is )", since it's redundant.

• The inserted semicolon would be parsed as the terminating semicolon
of a do-while statement.


(In reply to Michael Dyck from comment #2)
>
> Well, ASI's 'additional overriding condition' uses wording such as
> "the semicolon would then be parsed as X"
> and
> "that semicolon would become Y",
> so you could use similar wording:

yes, I saw that and it made me feel a little less dirty.

>
> • The previous token is ) and the inserted semicolon would be parsed as
> the terminating semicolon of a do-while statement.

yes, I like this a little better than my formulation.

>
> Moreover, you could drop "The previous token is )", since it's redundant.
>
> • The inserted semicolon would be parsed as the terminating semicolon
> of a do-while statement.

I prefer to keep the mention of the ) as it provides some additional context for why this rule exists.


fixed in rev35 editor's draft


fixed in rev35