switch statement definition
Alex Buckley
alex.buckley at oracle.com
Thu Apr 9 18:30:27 UTC 2015
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 <http://www.avast.com/>
>
> L'absence de virus dans ce courrier électronique a été vérifiée par le
> logiciel antivirus Avast.
> www.avast.com <http://www.avast.com/>
>
>
More information about the jls-jvms-spec-comments
mailing list