Search code examples
javabnf

JAVA BNF "no short if" - what does this mean?


Basically the title. Looking through the Java BNF, I see "no short if" such as in:

<statement no short if> ::= <statement without trailing substatement> | <labeled statement no short if> | <if then else statement no short if> | <while statement no short if> | <for statement no short if>

What does it "no short if" mean? I see "NoShortIf" popping up in my lecture slides with no explanation of what it means.


Solution

  • The answer is in the link provided above in the comment by @Andy Turner:

    Statements are thus grammatically divided into two categories: those that might end in an if statement that has no else clause (a "short if statement") and those that definitely do not.

    Only statements that definitely do not end in a short if statement may appear as an immediate substatement before the keyword else in an if statement that does have an else clause.

    This simple rule prevents the "dangling else" problem. The execution behavior of a statement with the "no short if" restriction is identical to the execution behavior of the same kind of statement without the "no short if" restriction; the distinction is drawn purely to resolve the syntactic difficulty.