Is this syntactically valid Java?

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

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.
 
K

Knute Johnson

Christopher said:
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.

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?
 
L

lyallex

Christopher said:
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.

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/
 
C

Christopher Benson-Manica

[comp.lang.java.programmer] Knute Johnson said:
Christopher Benson-Manica wrote:
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.
 
P

Patricia Shanahan

Christopher said:
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.

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
 
M

Michael Jung

Christopher Benson-Manica said:
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.

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
 
C

Christopher Benson-Manica

[comp.lang.java.programmer] Patricia Shanahan said:
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.
 
D

derek

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.
--
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.

yes

this is my footer
 
P

Patricia Shanahan

derek said:
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.
--
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.

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
 
L

Lew

Christopher said:
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.

Patricia said:
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.

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
^
testit/Foo.java:41: ';' expected
for( foo: ; ; ); // Note statement label
^
testit/Foo.java:41: illegal start of expression
for( foo: ; ; ); // Note statement label
^
3 errors
$

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

Patricia Shanahan

Lew said:
Christopher said:
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.

Patricia said:
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.

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?

Patricia
 
L

Lew

Patricia said:
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.
 
D

derek

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.... :)
 
D

Daniel Pitts

derek said:
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.... :)
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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top