Is this syntactically valid Java?

Discussion in 'Java' started by Christopher Benson-Manica, Nov 7, 2007.

  1. 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.
     
    Christopher Benson-Manica, Nov 7, 2007
    #1
    1. Advertising

  2. Christopher Benson-Manica wrote:
    > 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?

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Nov 7, 2007
    #2
    1. Advertising

  3. Christopher Benson-Manica

    lyallex Guest

    Christopher Benson-Manica wrote:
    > 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/
     
    lyallex, Nov 7, 2007
    #3
  4. [comp.lang.java.programmer] Knute Johnson <> wrote:
    > 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.

    --
    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.
     
    Christopher Benson-Manica, Nov 7, 2007
    #4
  5. Christopher Benson-Manica

    Roedy Green Guest

    On Wed, 7 Nov 2007 15:25:22 +0000 (UTC), Christopher Benson-Manica
    <> 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?
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Nov 7, 2007
    #5
  6. Christopher Benson-Manica wrote:
    > 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
     
    Patricia Shanahan, Nov 7, 2007
    #6
  7. Christopher Benson-Manica

    Wojtek Guest

    Patricia Shanahan wrote :
    > The remainder of this message is of interest only to extreme language
    > lawyers.


    !

    --
    Wojtek :)
     
    Wojtek, Nov 7, 2007
    #7
  8. [comp.lang.java.programmer] Roedy Green <> wrote:

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

    --
    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.
     
    Christopher Benson-Manica, Nov 7, 2007
    #8
  9. Christopher Benson-Manica

    Michael Jung Guest

    Christopher Benson-Manica <> writes:
    > 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
     
    Michael Jung, Nov 7, 2007
    #9
  10. [comp.lang.java.programmer] Patricia Shanahan <> wrote:

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

    --
    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.
     
    Christopher Benson-Manica, Nov 12, 2007
    #10
  11. Christopher Benson-Manica

    derek Guest

    On 11/07/2007 at 10:25:22, Christopher Benson-Manica <> wrote:

    > 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
     
    derek, Nov 28, 2007
    #11
  12. derek wrote:
    > On 11/07/2007 at 10:25:22, Christopher Benson-Manica <> wrote:
    >
    >> 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
     
    Patricia Shanahan, Nov 28, 2007
    #12
  13. Christopher Benson-Manica

    Lew Guest

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


    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
     
    Lew, Nov 28, 2007
    #13
  14. Lew wrote:
    > Christopher Benson-Manica wrote:
    >>>> 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.

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

    ....

    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, Nov 29, 2007
    #14
  15. Christopher Benson-Manica

    Lew Guest

    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


    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
     
    Lew, Nov 29, 2007
    #15
  16. Christopher Benson-Manica

    derek Guest

    On 11/29/2007 at 09:28:17, Lew <> wrote:

    > 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

    > 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.... :)
     
    derek, Nov 29, 2007
    #16
  17. Christopher Benson-Manica

    Daniel Pitts Guest

    derek wrote:
    > On 11/29/2007 at 09:28:17, Lew <> wrote:
    >
    >> 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

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

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

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Nov 29, 2007
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aschel Kritsos

    Valid file types

    Aschel Kritsos, Nov 11, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    610
  2. Ajit
    Replies:
    1
    Views:
    3,658
    Victor Garcia Aprea [MVP]
    Apr 24, 2004
  3. Replies:
    5
    Views:
    19,784
  4. Replies:
    6
    Views:
    363
    CBFalconer
    Feb 28, 2007
  5. Alan Mackenzie

    Is this syntactically valid C++?

    Alan Mackenzie, Aug 14, 2010, in forum: C++
    Replies:
    5
    Views:
    331
Loading...

Share This Page