[874] in java-interest
Another syntax question (another ambiguous construct)
daemon@ATHENA.MIT.EDU (Scott Hudson)
Sat Aug 12 03:34:53 1995
From: hudson@cc.gatech.edu (Scott Hudson)
To: java-interest@java.sun.com
Date: Wed, 9 Aug 1995 14:16:18 -0400 (EDT)
The language spec grammar contains the production:
statement = 'try' statement ('catch' '(' parameter ')' statement )*
('finally' statement )?
Is this what is intended? This is what javac currently does. However, this
allows:
try try a(); catch(foo e) b(); catch(bar f) c();
which is ambiguous. Worse yet, you get fun stuff like:
try try a(); catch(foo e) b(); catch(bar f) c(); finally d(); catch(t g) e();
There are of course ways to disambiguate by fiat. However adding additional
ambiguity when its not needed does not seem like a good plan. Further the
obvious way to do that (associate the catch with the nearest try) is clearly
not going to do what the programmer intended in almost all cases where this
would come up since it always leaves one or more try blocks with no catch
or finally clauses (e.g., my first one). On the other hand, forcing association
in the other direction, in addition to being "unintuitive", makes my second
example impossible to parse.
I think what you want is:
statement = 'try' '{' statement* '}'
('catch' '(' parameter ')' '{' statement* '}' )*
('finally' '{' statement* '}')?
There are all sorts of arguments for this syntax, over the original:
its a lot easer to understand
its a whole lot easier to parse
all examples in the language spec use it (has anyone seen code that doesn't?)
no workable disambiguation
that's how C++ does it
The only argument I see against it is that this is not the way javac
(apparently) does it now.
Scott
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com