From alex.buckley at oracle.com Tue Apr 7 20:36:15 2015 From: alex.buckley at oracle.com (Alex Buckley) Date: Tue, 07 Apr 2015 13:36:15 -0700 Subject: switch statement definition In-Reply-To: References: Message-ID: <55243FBF.2050601@oracle.com> 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 From alex.buckley at oracle.com Thu Apr 9 18:30:27 2015 From: alex.buckley at oracle.com (Alex Buckley) Date: Thu, 09 Apr 2015 11:30:27 -0700 Subject: switch statement definition In-Reply-To: References: <55243FBF.2050601@oracle.com> Message-ID: <5526C543.6050702@oracle.com> You are right -- I misread your SwitchBlockStatement. You do allow: case 3: case 4: c(); break; I think your SwitchBlockStatement accepts the same language as SwitchBlockStatementGroup, but with more ambiguity. Consider this code: switch (e) { case 1: foo(); break; bar(); case 2: quux(); break; } It is accepted by the JLS' SwitchBlock and by your SwitchBlock. In the JLS, bar(); is one of the BlockStatements in the first SwitchBlockStatementGroup. In your SwitchBlock, bar(); could be one of the BlockStatements in SwitchBlock OR the first SwitchBlockStatement. The JLS' SwitchBlockStatementGroup avoids such ambiguity by reflecting the natural flow of labels-statements-labels-statements-... Of course, bar(); is unreachable, but that's a semantic issue, not a syntactic one. Thanks for writing! Alex On 4/9/2015 3:00 AM, Johnny G?rard wrote: > Thanks you very much for answering me. > > I'm learning the specification right now and I should not try to > redefine such definition cause I don't understand enough about the > grammar rules. > > Though you interpreted my *SwitchBlockStatement *like this : > > *SwitchBlockStatement* : > SwitchLabel BlockStatement > > but it does not matter cause I was wrong anyway. > > Good day. > > On 4/7/2015 22:36, Alex Buckley wrote: >> 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 >> >> > > > > ------------------------------------------------------------------------ > Avast logo > > L'absence de virus dans ce courrier ?lectronique a ?t? v?rifi?e par le > logiciel antivirus Avast. > www.avast.com > >