简体   繁体   English

JAVA BNF“不容错过”-这是什么意思?

[英]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: 通过Java BNF进行浏览,我看到“不容错过”,例如:

<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. 我的演讲幻灯片中弹出“ NoShortIf”,但没有解释它的含义。

A "short if" is an if statement without an else. “ short if”是没有else的if语句。

"Short ifs" are not allowed in certain situations to eliminate ambiguity. 在某些情况下,不允许使用“简短假设”来消除歧义。

The following is valid Java. 以下是有效的Java。 There are no "short ifs" and no ambiguity. 没有“简短的假设”,也没有歧义。

boolean flag = false;
if (x > 0) 
    if (x > 10)
        flag = true;
    else
        flag = false;
else 
    flag = true;

The following is also valid java code, but without the "short if" rules there is ambiguity as to which if the else belongs. 以下也是有效的Java代码,但是如果没有“ short if”规则,则else是否属于哪个模棱两可。

if (x > 0)  if (x < 10) flag = true; else  flag = false;

The following Java language rules 以下Java语言规则

IfThenStatement:
   if ( Expression ) Statement
IfThenElseStatement:
   if ( Expression ) StatementNoShortIf else Statement

imply that the meaning of the above code is 暗示以上代码的含义是

if (x > 0)  
    if (x < 10) 
       flag = true; 
    else  
       flag = false;

That is, the else belongs to the inner if statement. 也就是说,else属于内部if语句。

Let's test in Java to be sure 让我们在Java中进行测试以确保

static Boolean shorty (int x) {
    Boolean flag = null;
    if (x > 0)  if (x < 10) flag = true; else  flag = false;
    return flag;
}


public static void main(String[] args) {
    System.out.println(shorty(-1));
    System.out.println(shorty(5));
    System.out.println(shorty(20));
}

The output is 输出是

null
true
false

The answer is in the link provided above in the comment by @Andy Turner: 答案在@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. 因此,语句在语法上分为两类:可能以不包含else子句的if语句(“ short if语句”)结尾的语句和绝对没有的子句。

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. 只有在肯定具有else子句的if语句中,绝对不会以短if语句结尾的语句才可以作为立即子语句出现在关键字else之前。

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; 具有“ no short if”限制的语句的执行行为与没有“ no short if”限制的相同种类的语句的执行行为相同; the distinction is drawn purely to resolve the syntactic difficulty. 区别纯粹是为了解决语法困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM