Still a hanging problem?

What is a freeze problem? (Is this the correct name?)

Following the C ++ coding standard (forget which one), I always use parentheses (block) with control structures. So I don't usually have this problem (to which "if" does the last (?) Still belong), but to understand possible problems in foreign code it would be good with a clear understanding of this problem. I remember reading about this in a book about Pascal many years ago, but I cannot find this book.

+2


source to share


4 answers


Which one if

belongs else

to?

if (a < b)
    if (c < d)
        a = b + d;
    else
        b = a + c;

      

(Obviously, you should ignore indentation.)



The fact that "hangs is still a problem."

C / C ++ gets rid of ambiguity by having a rule that says you can't use if

-without-an- else

like if

-body a- if

-with -ap- else

.

+5


source


Another ambiguous.

Info here: http://theory.stanford.edu/~amitp/yapps/yapps-doc/node3.html

But a classic example:



if a then
  if b then
     x = 1;
  else 
     y = 1;

      

against.

if a then
  if b then
     x = 1;
else 
  y = 1;

      

+9


source


Looking at this in terms of langauge design.

Standard BNF -like grammar for if

- else

:

Statement :-   .. STUFF..
          |    IfStatement

IfStatement :- IF_TOKEN '(' BoolExpression ')' Statement IfElseOpt

IfElseOpt :-   /* Empty */
          |    ELSE_TOKEN Statement

      

Now from the point of view of parsers:

if (cond1) Statement1
   if (cond2) Statement2
else Statement3

      

When you hit ELSE_TOKEN, the parser has two options: SHIFT or REDUCE. The problem is that the selection requires a different rule that the parser must follow. The default for most parser generators is SHIFT.

+1


source


I don't see a problem for pascal?

This is the wrong indentation.

if a then
  if b then
     x = 1;
  else 
     y = 1;

      

Removing the semi-column after x = 1 will make it indented correctly.

This correct indentation

if a then
  if b then
     x = 1;
else 
  y = 1;

      

-1


source







All Articles