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.
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?
--
Knute Johnson
email s/nospam/knute/
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/
>> 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.
> for( foo: ; ; ); // Note statement label
Why does it matter? It is meaningless nonsense?
Are you writing a compiler?
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
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 :-)
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
>> 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".)
> 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.
yes
this is my footer
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
derek wrote:
>> yes
Patricia Shanahan wrote:
> 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:
> $ javac -d ../build/classes/ testit/Foo.java
> $
> $ javac -version
> javac 1.6.0_03
> $
--
Lew
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
Patricia Shanahan wrote:
> 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.
--
Lew
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' Tech Blog: <http://virtualinfinity.net/wordpress/>