aspose file tools*
The moose likes Beginning Java and the fly likes Java Language Specyfication - how to read Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Language Specyfication - how to read" Watch "Java Language Specyfication - how to read" New topic
Author

Java Language Specyfication - how to read

Adrian Sosialuk
Ranch Hand

Joined: Jun 07, 2007
Posts: 57
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Adrian]: Is it all to indicate that one could use some of the expressions
multiple times ?


Yes.

[B][Adrian]: To be more precise, why can it not be written this way:


[/B]

Doing it that way would imply that a SwitchBlockStatementGroups contains exactly one SwitchBlockStatementGroup, and that a SwitchLabels contains exactly one SwitchLabel. Because that's the only option you've listed. Doing it the way the JLS does, each of those implies one or more of their constituent parts. That's the standard pattern in the JLS grammar for saying "one of more of Foo":

They could have also achieved this a little more concisely with the "opt" notation, which you have expressed using an underscore:

But that's a minor point. They could have also introduced additional special notations for "one or more" (and they do something like this in chapter 18 in an alternate formulation of the grammer). But that's not what they did in the main JLS chapters; oh well. Either way, it's easy enough to parse what they mean once you're used to the idea. Just pay attention to whether or not a given construct name ends in "s" - if it does, it's probably a plural, and you should expect that there will be a definition similar to Foos above.


"I'm not back." - Bill Harding, Twister
Adrian Sosialuk
Ranch Hand

Joined: Jun 07, 2007
Posts: 57
Thanks Jim !

I fact, I was wondering how it would be more
"readable" and the only conclusion I came to was
their own way ... But that's probably because I'd still
had it in my mind Personally, it looks to me like
a kind of recursion, but sun likes that kind of expressions
(glorious Enum<T extends Enum<T>> ...).

Anyway, thanks a lot !

Cheers,

Adrian
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Language Specyfication - how to read
 
Similar Threads
switch() flow of execution
do while and curly braces:-
Wrapper class variable in switch
switch statement
Switch statement