[868] in java-interest

home help back first fref pref prev next nref lref last post

Another syntax question (another ambiguous construct)

daemon@ATHENA.MIT.EDU (Scott Hudson)
Sat Aug 12 01:33:28 1995

From: "Scott Hudson" <hudson@cs.arizona.edu>
To: java-interest@java.sun.com
Date: Fri, 11 Aug 1995 16:38:36 -0700 (MST)

*** This is a repost due to apparent network failure.
*** Apologies if you get this twice.
*** Note: this is not my normal email address (but it does forward to me).

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

home help back first fref pref prev next nref lref last post