“if” block without curly braces makes subsequent “else if” nested

可紊 提交于 2019-11-29 05:55:24

问题


AFAIK, if an "if" block is not provided the curly braces then only 1 statement is considered inside it. e.g.

if(..)
  statement_1;
  statement_2;

Irrespective of tabs, only statement_1 is considered inside the if block.

Following code doesn't get along with that:

int main ()
{
  if(false)  // outer - if
    if(false)  // nested - if
      cout << "false false\n";
  else if(true)
    cout << "true\n";
}

Above code doesn't print anything. It should have printed "true".
It appears as of the else if is automatically nested inside the outer if block. g++ -Wall issues warning, but that is not the question here. Once you put the curly braces, everything goes fine as expected.

Why such different behavior ?
[GCC demo: without braces and with braces].


回答1:


The behaviour isn’t actually different, it’s entirely consistent: the whole inner if block – including else if – is considered as one block.

This is a classical ambiguity in parsing, known as the “dangling-else problem”: there are two valid ways of parsing this when the grammar is written down in the normal BNF:

Either the trailing else is part of the outer block, or of the inner block.

Most languages resolve the ambiguity by (arbitrarily) deciding that blocks are matched greedily by the parser – i.e. the else [if] is assigned to the closest if.




回答2:


Because the else is actually being grouped with the inner if, not the outer one. It's actually being parsed as

int main ()
{
  if(false)  // outer - if (never gets executed)
  { 
    if(false)  // nested - if
    {
        cout << "false false\n";
    } else if(true) {
        cout << "true\n";
    }
  }
}

You can solve the problem by explicitly putting the braces where you want them.




回答3:


It shouldn't print anything. It is the equivalent to this, since the second if/else if is one block that belongs to the first if:

  if(false) {
    if(false)  // nested - if
      cout << "false false\n";
    else if(true)
      cout << "true\n";
  } 



回答4:


It is quite natural from the C parser viepoint.

The parser, while parsing if-statement, parses the condition expression first, then parses the first statement after condition, then looks for else keyword and, if the else presents, parses the second (alternative) statement.

However, the first statement is an if-statement too, so the parser calls the "if-parser" recursively (before testing for else keyword!). This recursive call parses the inner if-else statement completely (including else), and moves token position "past the end" of the whole code fragment.

Any attempt to implement alternative behaviour should involve some additional communication between "outer" and "inner" if-parsers: the outer parser should inform the "inner" not to be greedy(i.e. not to eat the else statement). This would add additional complexity to the language syntax.




回答5:


else statement always attaches to nearest if. Without branches nested if itself does not form meaningful statement, so parser goes on.



来源:https://stackoverflow.com/questions/11734604/if-block-without-curly-braces-makes-subsequent-else-if-nested

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!