HiveBrain v1.2.0
Get Started
← Back to all entries
gotchaMinor

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

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
expressionthiswhytheresolvesjustjavaredundantgrammarthat

Problem

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

Solution

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

Code Snippets

ExpressionStatement:
    StatementExpression ;

StatementExpression:
    Assignment
    PreIncrementExpression
    PreDecrementExpression
    PostIncrementExpression
    PostDecrementExpression
    MethodInvocation
    ClassInstanceCreationExpression
StatementExpression:
  Assignment
  PreIncrementExpression
  PreDecrementExpression
  PostIncrementExpression
  PostDecrementExpression
  MethodInvocation
  ClassInstanceCreationExpression

Context

StackExchange Computer Science Q#139042, answer score: 7

Revisions (0)

No revisions yet.