# Why does the Java grammar have a StatementExpression that resolves to just Expression? Why have this and other redundant rules in the grammar?

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

• 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. – rici Apr 17 at 18:21
• 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. – Chris Bouchard Apr 18 at 5:08
• (@ChrisBouchard or even chalk it up.) – greybeard Apr 18 at 6:09
• 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. – Marcos Zolnowski Apr 18 at 6:37
• 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. – Jörg W Mittag Apr 18 at 6:42

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.

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

• Because you can do this if(1+1==2){return;}, but not this 1+1==2; – Marcos Zolnowski Apr 18 at 6:11
• 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. – Jörg W Mittag Apr 18 at 6:42

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

• 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. – Jörg W Mittag Apr 18 at 5:51
• @JörgWMittag It appears you are correct and 14.8 gives justification for the StatementExpression rule. I'll update the answer. – qz- Apr 18 at 6:05