• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Language Specyfication - how to read

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.
 
Adrian Sosialuk
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic