switch statement definition

Alex Buckley alex.buckley at oracle.com
Tue Apr 7 20:36:15 UTC 2015


This allows:

   switch (e) {
     // SwitchLabels BlockStatements
     case 1:
     case 2: a(); b();
             break;

     // SwitchBlockStatement
     case 3: c();

     // SwitchBlockStatement
     case 4: c();
   }

but you can't group the case 3: and case 4: labels (since 
SwitchBlockStatement only allows a single SwitchLabel) and you can't add 
a break; after either c(); (since SwitchBlockStatement only allows a 
single BlockStatement). Your grammar is too tight.

If you modify SwitchBlockStatement to allow multiple SwitchLabel terms 
and multiple BlockStatement terms, then you basically recreate 
SwitchBlockStatementGroup.

In general it's not desirable to tweak longstanding rules of grammar 
unless there's a very very good reason. A very very good reason in JLS8 
was total consistency of notation across all chapters (including a 
simplification of SwitchStatement!) -- see 
https://bugs.openjdk.java.net/browse/JDK-8020782.

Alex

On 3/29/2015 1:30 PM, Johnny Gérard wrote:
> Hi,
>
> I find the switch statement syntactic definition pretty unclear.
>
> I'd like to know if the following definition is correct and could be a
> new definition for the specification :
>
> *SwitchStatement*:
>      switch ( Expression ) SwitchBlock
>
> *SwitchBlock*:
>      { /{/SwitchLabel/}/ }
>      { SwitchLabels BlockStatements /{/SwitchBlockStatement/}/ }
>
>
> *SwitchBlockStatement*:
>      SwitchLabel
>      BlockStatement
>
> So, to be clear : I replaced *SwitchBlock* by a new one and**created the
> non terminal symbol *SwitchBlockStatement*.
> This new *SwitchStatement *is not using the *SwitchBlockStatementGroup*
> symbol.
>
> I hope this is clear enough.
>
> Johnny Gérard
>
>
> ---
> L'absence de virus dans ce courrier électronique a été vérifiée par le
> logiciel antivirus Avast.
> http://www.avast.com


More information about the jls-jvms-spec-comments mailing list