Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Is this syntactically valid Java?

Thread view: 
Christopher Benson-Manica - 07 Nov 2007 15:25 GMT
Is the following syntactically valid?

public class Foo {
 public static void main(String[] args) {
   for( foo: ; ; ); // Note statement label
 }
}

javac is rejecting this, but IntelliJ is saying that it is valid.  I
would appreciate a reference to the JLS if possible, so I can file a
bug report either with Sun or with JetBrains.

Signature

 C. Benson Manica    | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
  sdf.lonestar.org   | Google groups, due to rampant unchecked spam.

Knute Johnson - 07 Nov 2007 17:08 GMT
> Is the following syntactically valid?
>
[quoted text clipped - 7 lines]
> would appreciate a reference to the JLS if possible, so I can file a
> bug report either with Sun or with JetBrains.

I don't know if it is valid or not but why would you want to write code
like this?  What is is supposed to do?

Signature

Knute Johnson
email s/nospam/knute/

Christopher Benson-Manica - 07 Nov 2007 18:54 GMT
>> public class Foo {
>>   public static void main(String[] args) {
>>     for( foo: ; ; ); // Note statement label
>>   }
>> }

> I don't know if it is valid or not but why would you want to write code
> like this?  What is is supposed to do?

I have no interest in writing any such code, I merely wish to
determine whether there is a bug in my IDE or javac.  I certainly
don't know what the code would do.

Signature

 C. Benson Manica    | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
  sdf.lonestar.org   | Google groups, due to rampant unchecked spam.

lyallex - 07 Nov 2007 18:42 GMT
> Is the following syntactically valid?
>
[quoted text clipped - 7 lines]
> would appreciate a reference to the JLS if possible, so I can file a
> bug report either with Sun or with JetBrains.

This does not compile in Eclipse

JLS Third Edition section 14.14 'The for Statement' and sub sections

get the spec at http://java.sun.com/docs/books/jls/
Roedy Green - 07 Nov 2007 19:05 GMT
On Wed, 7 Nov 2007 15:25:22 +0000 (UTC), Christopher Benson-Manica
<ataru@vinland.freeshell.org> wrote, quoted or indirectly quoted
someone who said :

>    for( foo: ; ; ); // Note statement label

Why does it matter? It is meaningless nonsense?
Are you writing a compiler?
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Christopher Benson-Manica - 07 Nov 2007 20:54 GMT
>>    for( foo: ; ; ); // Note statement label

> Why does it matter? It is meaningless nonsense?

Yes, it's meaningless.

> Are you writing a compiler?

No.  I am trying to determine whether to file a bug report with
JetBrains.  (After reading the JLS, I believe the answer is "yes".)

Signature

 C. Benson Manica    | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
  sdf.lonestar.org   | Google groups, due to rampant unchecked spam.

Patricia Shanahan - 07 Nov 2007 20:09 GMT
> Is the following syntactically valid?
>
[quoted text clipped - 7 lines]
> would appreciate a reference to the JLS if possible, so I can file a
> bug report either with Sun or with JetBrains.

I believe it is not syntactically valid.

The remainder of this message is of interest only to extreme language
lawyers. I'm going to show the available productions, and the JLS
sections where I found them. This is based on the third editions,
http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html

First note that "for" is a reserved word, and as far as I can tell
appears only in for statement productions in 14.14.

The statement cannot be a EnhancedForStatement because it requires a ":"
followed by an expression. 14.14.2

Now consider it as a BasicForStatement, 14.14.1. In each production, the
portion between the "for(" and the first of the required three
semicolons is an optional ForInit. The question becomes whether "foo:"
can be a ForInit.

ForInit:
        StatementExpressionList
        LocalVariableDeclaration

Can "foo:" be a StatementExpressionList?

StatementExpressionList:
        StatementExpression
        StatementExpressionList , StatementExpression (14.14.1)

Since "foo:" does not contain a ",", it would have to be a single
StatementExpression.

In 14.8:

StatementExpression:
        Assignment
        PreIncrementExpression
        PreDecrementExpression
        PostIncrementExpression
        PostDecrementExpression
        MethodInvocation
        ClassInstanceCreationExpression

"foo:" does not match any of these.

Now consider the LocalVariableDeclaration case. In 14.4:

LocalVariableDeclaration:
        VariableModifiers Type VariableDeclarators

In 8.4.1:

VariableModifiers:
    VariableModifier
    VariableModifiers VariableModifier

VariableModifier: one of
    final Annotation

so "foo:" does not match VariableModifiers.

"foo" could be a type, but then ":" would have to match VariableDeclarators.

In 8.3:

VariableDeclarators:
    VariableDeclarator
    VariableDeclarators , VariableDeclarator

VariableDeclarator:
    VariableDeclaratorId
    VariableDeclaratorId = VariableInitializer

VariableDeclaratorId:
    Identifier
    VariableDeclaratorId [ ]

All cases of VariableDeclarators begin with an identifier, and ":" is
not a valid identifier.

Patricia
Wojtek - 07 Nov 2007 20:37 GMT
Patricia Shanahan wrote :
> The remainder of this message is of interest only to extreme language
> lawyers.

!

Signature

Wojtek :-)

Christopher Benson-Manica - 12 Nov 2007 14:26 GMT
> I believe it is not syntactically valid.

(snip excellent response)

Thank you *very* much.  I feel much more comfortable now with
reporting this (obscure!) parsing bug to the JetBrains people.

Signature

 C. Benson Manica    | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
  sdf.lonestar.org   | Google groups, due to rampant unchecked spam.

Michael Jung - 07 Nov 2007 21:20 GMT
> Is the following syntactically valid?
>
[quoted text clipped - 7 lines]
> would appreciate a reference to the JLS if possible, so I can file a
> bug report either with Sun or with JetBrains.

To me it seems not to be allowed, but purely for BNF reasons. The
first part in for must contain a StatementExpression, while a
LabeledStatement is not a StatementExpression as far as I can tell.
From JSL (3rd Ed.), but IANAL.

Michael
derek - 28 Nov 2007 21:27 GMT
> Is the following syntactically valid?
> public class Foo {
[quoted text clipped - 5 lines]
> would appreciate a reference to the JLS if possible, so I can file a
> bug report either with Sun or with JetBrains.

yes

this is my footer
Patricia Shanahan - 28 Nov 2007 21:49 GMT
>> Is the following syntactically valid?
>> public class Foo {
[quoted text clipped - 7 lines]
>
> yes

If you are saying it is syntactically valid, could you give your reasoning?

In response to the quoted message, I went through the relevant
productions in the grammar in the JLS, and did not think it was possible
to generate the label in the for. See
http://groups.google.com/group/comp.lang.java.programmer/msg/008b9ce336a09d3e

However, I could be wrong about it because it is easy to miss something
when doing manual processing of a large formal grammar.

Patricia
Lew - 28 Nov 2007 22:01 GMT
Christopher Benson-Manica wrote:
>>> Is the following syntactically valid?
>>> public class Foo {
[quoted text clipped - 5 lines]
>>> would appreciate a reference to the JLS if possible, so I can file a
>>> bug report either with Sun or with JetBrains.

derek wrote:
>> yes

> If you are saying it is syntactically valid, could you give your reasoning?
>
[quoted text clipped - 5 lines]
> However, I could be wrong about it because it is easy to miss something
> when doing manual processing of a large formal grammar.

derek is mistaken.  Here's what I get from Java 6 (with the loop on line 41):

> $ javac -d ../build/classes/ testit/Foo.java
> testit/Foo.java:41: not a statement
[quoted text clipped - 8 lines]
> 3 errors
> $

Contrariwise, if I eliminate the "foo: " from the line:

> $ javac -d ../build/classes/ testit/Foo.java
> $
> $ javac -version
> javac 1.6.0_03
> $

Signature

Lew

Patricia Shanahan - 29 Nov 2007 13:05 GMT
> Christopher Benson-Manica wrote:
>>>> Is the following syntactically valid?
[quoted text clipped - 27 lines]
>> testit/Foo.java:41: not a statement
>>     for( foo: ; ; ); // Note statement label
...

I'm not sure that is determinative in the context of this thread. The
original issue was that javac rejects code that IntilliJ accepted. I
think, based on my previous analysis, that javac is correct and IntelliJ
got it wrong, but maybe derek has a valid argument the other way round?

Patricia
Lew - 29 Nov 2007 14:28 GMT
Lew wrote:
>> derek is mistaken.  Here's what I get from Java 6 (with the loop on
>> line 41):
>>
>>> $ javac -d ../build/classes/ testit/Foo.java
>>> testit/Foo.java:41: not a statement
>>>     for( foo: ; ; ); // Note statement label

> I'm not sure that is determinative in the context of this thread. The
> original issue was that javac rejects code that IntilliJ accepted. I
> think, based on my previous analysis, that javac is correct and IntelliJ
> got it wrong, but maybe derek has a valid argument the other way round?

Twenty bucks says he doesn't.

Signature

Lew

derek - 29 Nov 2007 14:47 GMT
> Lew wrote:
> >> derek is mistaken.  Here's what I get from Java 6 (with the loop on
[quoted text clipped - 8 lines]
> > got it wrong, but maybe derek has a valid argument the other way round?
> Twenty bucks says he doesn't.

Congrats you get twenty bucks.
I have no valid argument.
I was testing out something with my newsgroup posting software.
I picked an old thread that i didnt think anyone was paying attention to and posted "yes" to it without even reading the thread.
Next time i need to test something i will post to the testing group.
Sorry bout that.... :)
Daniel Pitts - 29 Nov 2007 17:15 GMT
>> Lew wrote:
>>>> derek is mistaken.  Here's what I get from Java 6 (with the loop on
[quoted text clipped - 15 lines]
> Next time i need to test something i will post to the testing group.
> Sorry bout that.... :)
Most news readers that I've used will boost the thread to the top of the
list when new activity is posted to it.  Even if that wasn't the case,
the test group you mentioned is *exactly* for this purpose :-)

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.