Knowledge

Dangling else

Source 📝

562:
statement: open_statement | closed_statement  ; open_statement: IF '(' expression ')' statement | IF '(' expression ')' closed_statement ELSE open_statement  ; closed_statement: non_if_statement | IF '(' expression ')'
587:
statement: open_statement | closed_statement  ; open_statement: IF '(' expression ')' statement | IF '(' expression ')' closed_statement ELSE open_statement | WHILE '(' expression ')' open_statement  ; closed_statement:
580:
statement: open_statement | closed_statement  ; open_statement: IF '(' expression ')' statement | IF '(' expression ')' closed_statement ELSE open_statement | WHILE '(' expression ')' open_statement  ; closed_statement:
208:. The convention when dealing with the dangling else is to attach the else to the nearby if statement, allowing for unambiguous context-free grammars, in particular. Programming languages like Pascal, C and Java follow this convention, so there is no ambiguity in the semantics of the 235:
generator, the programmer will often rely on the generated parser feature of preferring shift over reduce whenever there is a conflict. Alternatively, the grammar can be rewritten to remove the conflict, at the expense of an increase in grammar size (see
588:
simple_statement | IF '(' expression ')' closed_statement ELSE closed_statement | WHILE '(' expression ')' closed_statement  ; simple_statement: ...  ;
581:
simple_statement | IF '(' expression ')' closed_statement ELSE closed_statement | WHILE '(' expression ')' closed_statement  ; simple_statement: ...  ;
620:
statement open_statement IF '(' expression ')' statement IF '(' expression ')' closed_statement IF '(' a ')' (IF '(' expression ')' closed_statement ELSE closed_statement) IF '(' a ')' (IF '(' b ')' c ELSE 'd')
298:
Disallowing the statement following a "then" to be an "if" itself (it may however be a pair of statement brackets containing only an if-then-clause). This approach is followed by
390:
statement = ... | selection-statement selection-statement = ... | IF ( expression ) statement | IF ( expression ) statement ELSE statement
260:
The problem can also be solved by making explicit the link between an else and its if, within the syntax. This usually helps avoid human errors.
135:
is false (thus attaching the else to the second if). In other words, one may see the previous statement as either of the following expressions:
32: 79:
This gives rise to an ambiguity in interpretation when there are nested statements, specifically whenever an if-then form appears as
864: 801: 762::1999 (C): 6.8.4.1(3): "An else is associated with the lexically nearest preceding if that is allowed by the syntax.", available at 566:
Any other statement-related grammar rules may also have to be duplicated in this way if they may directly or indirectly end with a
54:
one may write conditionally executed code in two forms: the if-then form, and the if-then-else form – the else clause is optional:
602:
statement open_statement IF '(' expression ')' closed_statement ELSE open_statement 'if' '(' 'a' ')' closed_statement 'else' 'd'
900: 699: 890: 363: 313: 309: 276: 359: 308:
Requiring every "if" to be paired with an "else". To avoid a similar problem concerning semantics rather than syntax,
272: 248: 859:, Ellis Horwood series in computers and their applications, Chichester, West Sussex: Ellis Horwood, p. 20, 384: 227:
Depending on the compiler construction approach, one may take different corrective actions to avoid ambiguity:
563:
closed_statement ELSE closed_statement  ; non_if_statement: ...  ;
189: 51: 36: 24: 205: 895: 719: 678: 267:
Having an "end if" symbol delimiting the end of the if construct. Examples of such languages are
776: 860: 797: 791: 635: 40: 885: 763: 668: 28: 843: 816: 731: 201: 746:
An if-statement without an else-part shall not be immediately followed by the token else.
559:
The above example could be rewritten in the following way to remove the ambiguity :
759: 703: 630: 343:
Using different keywords for the one-alternative and two-alternative "if" statements.
879: 247:
context-free grammar. Alternatively, one may rely on a non-context-free grammar or a
682: 284: 216:. In these cases alternative grouping is accomplished by explicit blocks, such as 35:
statement results in nested conditionals being ambiguous. Formally, the reference
320:
without a fallback clause to be an error, effectively distinguishing conditional
366:
as its indentation rules delimit every block, not just those in "if" statements.
292: 44: 817:
Ambiguity of dangling else: non-context-free grammars are semantically opaque
232: 185: 673: 656: 657:"A final solution to the Dangling else of ALGOL 60 and related languages" 299: 288: 268: 181: 577:
However, we give grammar that includes both of if and while statements.
344: 777:"The Java Language Specification, Java SE 9 Edition, 14.5. Statements" 280: 184:, and has been resolved in various ways in subsequent languages. In 584:
Finally, we give the grammar that forbids ambiguous IF statements.
855:
Davie, Antony J. T.; Ronald Morrison (1981), Brian Meek (ed.),
844:
Ambiguity of dangling else: require braces when else follows if
595:
can only be parsed one way, because the other interpretation (
305:
Requiring braces (parentheses) when an "else" follows an "if".
790:
Dale, Nell B.; Weems, Chip (November 1996). "Dangling Else".
212:, though the use of a parser generator may lead to ambiguous 127:
is false (thus attaching the else to the first if) or when
243:
If the parser is hand written, the programmer may use a
237: 832:
Revised Report on the Algorithmic Language ALGOL 60
796:. Jones & Bartlett Learning. pp. 160–161. 231:If the parser is produced by an SLR, LR(1) or LALR 188:, the dangling else is the archetypal example of a 439:could ambiguously be parsed as if it were either: 694: 692: 8: 793:Introduction to Pascal and Structured Design 755: 753: 605:and then the parsing fails trying to match 393:Thus, without further rules, the statement 196:Avoiding ambiguity while keeping the syntax 672: 256:Avoiding ambiguity by changing the syntax 200:This is a problem that often comes up in 43:, meaning there is more than one correct 613:fails in the same way. The other parse, 647: 551:. Therefore, the first tree is chosen. 358:Requiring braces unconditionally, like 31:in which an optional else clause in an 727: 717: 340:, which do not have fallback clauses). 828:4.5.1 Conditional Statements — Syntax 547:block is associated with the nearest 7: 555:Avoiding the conflict in LR parsers 180:The dangling else problem dates to 14: 543:The C standard clarifies that an 351:for the one-alternative case and 744:ISO 7185:1990 (Pascal) 6.8.3.4: 591:With this grammar the statement 609:to "if (b) c". An attempt with 119:is true, but one may interpret 111:is unambiguously executed when 16:Problem in computer programming 387:, the grammar reads, in part: 362:. This is effectively true in 1: 700:"5.2 Shift/Reduce Conflicts" 857:Recursive Descent Compiling 917: 375:Concrete examples follow. 249:parsing expression grammar 834:, CACM 6,1, 1963 pp. 1-17 661:Communications of the ACM 83:in an if-then-else form: 655:Abrahams, P. W. (1966). 615:if (a) {if (b) c else d} 597:if (a) {if (b) c} else d 493: 441: 395: 263:Possible solutions are: 123:as being executed when 901:Conditional constructs 593:if (a) if (b) c else d 674:10.1145/365813.365821 355:for the general case. 353:if e1 then e2 else e3 202:compiler construction 190:shift-reduce conflict 52:programming languages 891:Computer programming 37:context-free grammar 830:in P. Nauer (ed.), 572:selection-statement 328:) from conditional 206:scannerless parsing 39:of the language is 316:by considering an 636:Most vexing parse 599:) is produced as 107:In this example, 29:parser generators 908: 870: 869: 852: 846: 841: 835: 825: 819: 814: 808: 807: 787: 781: 780: 773: 767: 757: 748: 742: 736: 735: 729: 725: 723: 715: 713: 712: 696: 687: 686: 676: 652: 616: 612: 611:closed_statement 608: 607:closed_statement 598: 594: 573: 569: 550: 546: 539: 536: 533: 530: 527: 524: 521: 518: 515: 512: 509: 506: 503: 500: 497: 487: 484: 481: 478: 475: 472: 469: 466: 463: 460: 457: 454: 451: 448: 445: 435: 432: 429: 426: 423: 420: 417: 414: 411: 408: 405: 402: 399: 354: 350: 339: 335: 327: 319: 223: 219: 134: 130: 126: 122: 118: 114: 110: 82: 23:is a problem in 916: 915: 911: 910: 909: 907: 906: 905: 876: 875: 874: 873: 867: 854: 853: 849: 842: 838: 826: 822: 815: 811: 804: 789: 788: 784: 775: 774: 770: 758: 751: 743: 739: 726: 716: 710: 708: 698: 697: 690: 654: 653: 649: 644: 627: 622: 614: 610: 606: 603: 596: 592: 589: 582: 571: 567: 564: 557: 548: 544: 541: 540: 537: 534: 531: 528: 525: 522: 519: 516: 513: 510: 507: 504: 501: 498: 495: 489: 488: 485: 482: 479: 476: 473: 470: 467: 464: 461: 458: 455: 452: 449: 446: 443: 437: 436: 433: 430: 427: 424: 421: 418: 415: 412: 409: 406: 403: 400: 397: 391: 381: 373: 352: 348: 337: 333: 325: 317: 258: 221: 217: 198: 178: 132: 128: 124: 120: 116: 112: 108: 105: 80: 77: 17: 12: 11: 5: 914: 912: 904: 903: 898: 893: 888: 878: 877: 872: 871: 865: 847: 836: 820: 809: 802: 782: 768: 749: 737: 728:|website= 688: 667:(9): 679–682. 646: 645: 643: 640: 639: 638: 633: 631:The lexer hack 626: 623: 619: 601: 586: 579: 574:non-terminal. 561: 556: 553: 494: 442: 396: 389: 380: 377: 372: 369: 368: 367: 356: 341: 312:deviates from 306: 303: 296: 257: 254: 253: 252: 241: 220:in Pascal and 197: 194: 137: 85: 56: 33:if–then(–else) 15: 13: 10: 9: 6: 4: 3: 2: 913: 902: 899: 897: 894: 892: 889: 887: 884: 883: 881: 868: 866:0-470-27270-8 862: 858: 851: 848: 845: 840: 837: 833: 829: 824: 821: 818: 813: 810: 805: 803:9780763703974 799: 795: 794: 786: 783: 778: 772: 769: 765: 761: 756: 754: 750: 747: 741: 738: 733: 721: 707: 706: 701: 695: 693: 689: 684: 680: 675: 670: 666: 662: 658: 651: 648: 641: 637: 634: 632: 629: 628: 624: 618: 600: 585: 578: 575: 560: 554: 552: 492: 440: 394: 388: 386: 378: 376: 370: 365: 361: 357: 346: 342: 331: 323: 315: 311: 307: 304: 301: 297: 294: 290: 286: 282: 278: 274: 270: 266: 265: 264: 261: 255: 250: 246: 245:non-ambiguous 242: 239: 234: 230: 229: 228: 225: 215: 211: 207: 204:, especially 203: 195: 193: 191: 187: 183: 176: 172: 168: 164: 160: 156: 152: 148: 144: 140: 136: 103: 99: 95: 92: 88: 84: 75: 71: 67: 63: 59: 55: 53: 48: 46: 42: 38: 34: 30: 26: 22: 21:dangling else 856: 850: 839: 831: 827: 823: 812: 792: 785: 771: 745: 740: 709:. Retrieved 704: 664: 660: 650: 617:) succeeds: 604: 590: 583: 576: 565: 558: 542: 490: 438: 392: 382: 374: 329: 321: 285:Visual Basic 262: 259: 244: 226: 213: 209: 199: 179: 174: 170: 166: 162: 158: 154: 150: 146: 142: 138: 131:is true and 115:is true and 106: 101: 97: 93: 90: 86: 78: 73: 69: 65: 61: 57: 49: 20: 18: 705:Bison 3.7.6 322:expressions 293:AppleScript 218:begin...end 25:programming 880:Categories 764:WG14 N1256 711:2021-08-07 642:References 330:statements 186:LR parsers 45:parse tree 896:Ambiguity 730:ignored ( 720:cite book 568:statement 349:if e do s 233:LR parser 41:ambiguous 766:, p. 134 760:ISO 9899 625:See also 371:Examples 300:ALGOL 60 289:Modula-2 269:ALGOL 68 214:grammars 210:language 182:ALGOL 60 50:In many 886:Parsing 683:6777841 345:S-algol 863:  800:  681:  364:Python 338:unless 314:Scheme 310:Racket 291:, and 281:PL/SQL 277:Eiffel 224:in C. 679:S2CID 360:Swift 347:uses 332:(i.e 324:(i.e 238:below 222:{...} 861:ISBN 798:ISBN 732:help 545:else 532:else 491:or: 477:else 428:else 336:and 334:when 177:s2) 175:else 171:then 163:then 155:else 151:then 143:then 102:else 98:then 91:then 74:else 70:then 62:then 19:The 669:doi 570:or 383:In 273:Ada 157:s2 153:s) 104:s2 76:s2 72:s1 27:of 882:: 752:^ 724:: 722:}} 718:{{ 702:. 691:^ 677:. 663:. 659:. 549:if 535:s2 511:if 496:if 480:s2 459:if 444:if 431:s2 410:if 398:if 326:if 318:if 287:, 283:, 279:, 275:, 271:, 240:). 192:. 173:s 169:b 167:if 161:a 159:if 149:b 147:if 141:a 139:if 121:s2 100:s 96:b 94:if 89:a 87:if 81:s1 68:b 66:if 64:s 60:a 58:if 47:. 806:. 779:. 734:) 714:. 685:. 671:: 665:9 538:; 529:} 526:; 523:s 520:) 517:b 514:( 508:{ 505:) 502:a 499:( 486:} 483:; 474:; 471:s 468:) 465:b 462:( 456:{ 453:) 450:a 447:( 434:; 425:; 422:s 419:) 416:b 413:( 407:) 404:a 401:( 385:C 379:C 302:. 295:. 251:. 165:( 145:( 133:b 129:a 125:a 117:b 113:a 109:s

Index

programming
parser generators
if–then(–else)
context-free grammar
ambiguous
parse tree
programming languages
ALGOL 60
LR parsers
shift-reduce conflict
compiler construction
scannerless parsing
LR parser
below
parsing expression grammar
ALGOL 68
Ada
Eiffel
PL/SQL
Visual Basic
Modula-2
AppleScript
ALGOL 60
Racket
Scheme
S-algol
Swift
Python
C
The lexer hack

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.