Hi everyone !
A snippet from the book:
SwitchStatement:
switch ( Expression ) SwitchBlock
SwitchBlock:
{ SwitchBlockStatementGroups_opt SwitchLabels_opt }
SwitchBlockStatementGroups:
SwitchBlockStatementGroup
SwitchBlockStatementGroups SwitchBlockStatementGroup
SwitchBlockStatementGroup:
SwitchLabels BlockStatements
SwitchLabels:
SwitchLabel
SwitchLabels SwitchLabel
SwitchLabel:
case ConstantExpression :
case EnumConstantName :
default :
EnumConstantName:
Identifier
Could someone please help me to understand how I am supposed
to read it ? The beginning is easy - switch statement is a statement
made of the key
word "switch" then left parentheses then expression then
right parentheses then SwitchBlock. SwichBlock consists of "{" then
SwitchBlockStatementGroups which is optional and then SwitchLabels
which is optional as well, then "}". So far so good. But SwitchBlockStatementGroups is
SwitchBlockStatementGroup
SwitchBlockStatementGroups SwitchBlockStatementGroup
How does it relate to the switch statement ? I just don't get it.
Why is it repeated ? I mean, SwitchStatementGroups and
SwitchStatementGroup ? How am I supposed to read it ?
To be more precise, why can it not be written this way:
SwitchStatement:
switch ( Expression ) SwitchBlock
SwitchBlock:
{ SwitchBlockStatementGroups_opt SwitchLabels_opt }
SwitchBlockStatementGroups:
SwitchBlockStatementGroup
SwitchBlockStatementGroup:
SwitchLabels BlockStatements
SwitchLabels:
SwitchLabel
SwitchLabel:
case ConstantExpression :
case EnumConstantName :
default :
EnumConstantName:
Identifier
Going that way, we wouldn't need some parts of it at all:
SwitchStatement:
switch ( Expression ) SwitchBlock
SwitchBlock:
{ SwitchBlockStatementGroup_opt SwitchLabel_opt }
SwitchBlockStatementGroup:
SwitchLabel BlockStatement
SwitchLabel:
case ConstantExpression :
case EnumConstantName :
default :
EnumConstantName:
Identifier
Is it all to indicate that one could use some of the expressions
multiple times ? If so, I find that way not very clear ...
And it's not only about this particular grammar example. It's all
over the JLS. I would like to sort it out once and forever
Thanks,
Adrian