3
$\begingroup$

I'm looking at the following grammar rules for the Java language described on the Oracle docs:

Statement:
    ...
    if ParExpression Statement [else Statement]
    StatementExpression ;
    ...

StatementExpression:
    Expression

ParExpression:
    ( Expression )

I don't understand why the StatementExpression rule is present at all. I also don't understand why ParExpression had to be written as a separate rule.

Why couldn't the grammar just look like the following?

Statement:
    ...
    if ( Expression ) Statement [else Statement]
    Expression ;
    ...
$\endgroup$
5
  • 2
    $\begingroup$ It didn't "have to be written as a separate rule". They chose to do so for stylistic or aesthetic reasons. Lots of grammars have redundant non-terminals. $\endgroup$ – rici Apr 17 at 18:21
  • 1
    $\begingroup$ In the grammar for Java 16, it looks like they've done both of the things you suggested, but they've also broken the different statement types out to their own nonterminals. So I'd chock it up to style. $\endgroup$ – Chris Bouchard Apr 18 at 5:08
  • 1
    $\begingroup$ (@ChrisBouchard or even chalk it up.) $\endgroup$ – greybeard Apr 18 at 6:09
  • 1
    $\begingroup$ I am laughing a lot right now. I never thought that I would be able to offer something of value here. I am not even a scientist, Hot Network Questions brought me here. $\endgroup$ – Marcos Zolnowski Apr 18 at 6:37
  • 2
    $\begingroup$ It's simply a bug in a ten year old spec that has long since been fixed in newer versions. Plus, you are not looking at the primary source. $\endgroup$ – Jörg W Mittag Apr 18 at 6:42
7
$\begingroup$

StatementExpression is described in the same specification document as Expression Statements. I would like to quote this passage specifically:

Certain kinds of expressions may be used as statements by following them with semicolons.

ExpressionStatement:
    StatementExpression ;

StatementExpression:
    Assignment
    PreIncrementExpression
    PreDecrementExpression
    PostIncrementExpression
    PostDecrementExpression
    MethodInvocation
    ClassInstanceCreationExpression

and this:

Unlike C and C++, the Java programming language allows only certain forms of expressions to be used as expression statements.

ParExpression is described as Parenthesized Expressions and Primary Expressions.

A parenthesized expression is a primary expression whose type is the type of the contained expression and whose value at run time is the value of the contained expression. If the contained expression denotes a variable then the parenthesized expression also denotes that variable.

The use of parentheses affects only the order of evaluation, except for a corner case whereby (-2147483648) and (-9223372036854775808L) are legal but -(2147483648) and -(9223372036854775808L) are illegal.

About your question:

Why have this and other redundant rules in the grammar?

They are not redundant, that is a mistake in the document, they are different types of expressions. See the grammar for the next version:

StatementExpression:
  Assignment
  PreIncrementExpression
  PreDecrementExpression
  PostIncrementExpression
  PostDecrementExpression
  MethodInvocation
  ClassInstanceCreationExpression
$\endgroup$
2
  • 1
    $\begingroup$ Because you can do this if(1+1==2){return;}, but not this 1+1==2; $\endgroup$ – Marcos Zolnowski Apr 18 at 6:11
  • 3
    $\begingroup$ This. Java 7 is ten years old, public support ended 6 years ago, premium support ended two years ago, extended support ends soon. This bug in the spec has long since been fixed. $\endgroup$ – Jörg W Mittag Apr 18 at 6:42
6
$\begingroup$

Edit: previously incorrect.

Chapter 18 does not have the correct definition of StatementExpression. The correct definition can be found in 14.8. StatementExpression is a subset of the expression grammar.

Justification for statement expression: In java, the statement 1 + 1; is invalid grammatically (unlike most other languages).

$\endgroup$
2
  • $\begingroup$ This is not the correct answer! You missed the correct answer: the syntax is defined piecemeal over the whole document, chapter 18 is supposed to collect all those pieces into a single overview, but there is simply a bug. If you look at the definition of ExpressionStatement in chapter 14.8, you see it looks different. The editors just made a mistake when copying the grammar fragment from 14.8 to 18. $\endgroup$ – Jörg W Mittag Apr 18 at 5:51
  • $\begingroup$ @JörgWMittag It appears you are correct and 14.8 gives justification for the StatementExpression rule. I'll update the answer. $\endgroup$ – qz- Apr 18 at 6:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.