PROPOSAL: open and close brace optional for single statement try, catch, finally, method declaration

Glenn A. Marshall glenn.a.marshall at gmail.com
Tue Mar 31 05:22:48 PDT 2009


On Mon, Mar 30, 2009 at 9:25 AM, Tom Hawtin <Thomas.Hawtin at sun.com> wrote:

> Glenn A. Marshall wrote:
>
>> 1.  This example is syntactically invalid since the first try does not
>> have
>> a catch nor finally clause; it is not ambiguous.  Good example tho.
>>
>
> Neal's example:
>
> >> try
> >> try stmt;
> >> catch(...) stmt;
> >> catch(...) stmt;
> >> finally stmt;
>
> Under the proposal "try stmt; catch(...) stmt;" is a statement. So the
> above is equivalent to:
>
> try
> stmt2;
> catch(...) stmt;
> finally stmt;
>
> But "try stmt; catch(...) stmt; catch(...) stmt;" is also a statement. So
> the example is also equivalent to:
>
> try
> stmt2;
> finally stmt;
>
> Which is different. Which is it?
>
> This is similar to the dangling-else ambiguity.
> http://en.wikipedia.org/wiki/Dangling_else


It is, indeed, a precise parallel to the dangling-else ambiguity, when
viewed from a grammar perspective.

>From JLS 14.5: "The Java programming language ... arbitrarily decree that an
else clause belongs to the innermost if to which it might possibly belong"

Under this proposal, a catch and a finally belongs to the innermost try to
which they might possibly belong.

<http://en.wikipedia.org/wiki/Dangling_else>
>
>  2.  The optional omission of the braces is only applicable to simple,
>> single
>> statement try, catch, finally (and method declaration).  Braces are still
>> allowed, and are required for disambiguation, as well as for multiple
>> statement try, catch finally (and method declaration).  They are are
>> needed
>> in this example.
>>
>
> That reads as if you think of a block statement (in for example the typical
> while usage) as not a type of statement. A block is a statement. You could
> ammend the grammar so the proposal uses non-block statement, but this is not
> how the rest of the language works.


Indeed, the grammar would change.  It was my hope that this proposal was
sufficiently detailed that the grammar changes needed would be clear,
conceptually.  Are they?


>
>
>
>
> BTW: Does anyone have a good suggestion for tool and basic Java grammar to
> check that syntax modifications are still parseable?
>
> Tom
>



More information about the coin-dev mailing list