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.
source to share
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
.
source to share
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;
source to share
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.
source to share