The dangling else
is a problem in computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...
in which a seemingly well-defined statement can become ambiguous. In many programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
s one may write conditionally executed code in two forms: the if-then form, and the if-then-else form:
This gives rise to an ambiguity in interpretation whenever an if-then form appears as
in an if-then-else form:
a then if
In this example,
is unambiguously executed when
is true and
is true, but one may interpret
as being executed when
is false (thus attaching the else to the first if) or when
is true and
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:
Avoiding ambiguity while keeping the syntax
This is a problem that often comes up in compiler
construction. 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 and C follow this convention, so there is no ambiguity in the
semantics of the language
, though the use of a parser generator may lead
to ambiguous grammars
Depending on the compiler construction approach, one may take different
corrective actions to avoid ambiguity:
- If the compiler is the product of a SLR, LR(1) or LALR LR parser
In computer science, an LR parser is a parser that reads input from Left to right and produces a Rightmost derivation. The term LR parser is also used; where the k refers to the number of unconsumed "look ahead" input symbols that are used in making parsing decisions...
generator, the programmer will often rely on the generated parser feature of preferring shift over reduce whenever there is a conflict.
- If the compiler is the product of a Pruning and Deep Pruning LR generator, one can issue directives than prune away the ambiguities completely.
- If the compiler is the product of a programmer instead of a parser generator, the programmer may use a non-ambiguous context-free grammar. Alternatively, one may rely on a non-context-free grammar or a parsing expression grammar
A parsing expression grammar, or PEG, is a type of analytic formal grammar, i.e. it describes a formal language in terms of a set of rules for recognizing strings in the language...
Programmers using C
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
and languages with similar syntax sometimes standardize a practice of using braces to clearly define the intent of the statement. Similarly, some programmers use logical and ternary operators to avoid ambiguity.
Avoiding ambiguity by changing the syntax
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
Possible solutions are:
- Having an "end if" statement. Examples of such languages are ALGOL 68
ALGOL 68 isan imperative computerprogramming language that was conceived as a successor to theALGOL 60 programming language, designed with the goal of a...
Ada is a structured, statically typed, imperative, wide-spectrum, and object-oriented high-level computer programming language, extended from Pascal and other languages...
Eiffel is an ISO-standardized, object-oriented programming language designed by Bertrand Meyer and Eiffel Software. The design of the language is closely connected with the Eiffel programming method...
PL/SQL is Oracle Corporation's procedural extension language for SQL and the Oracle relational database...
Realbasic is the object-oriented dialect of the BASIC programming language used in Real Studio, a programming environment, developed and commercially marketed by Real Software, Inc of Austin, Texas for Mac OS X, Microsoft Windows, 32-bit x86 Linux and the web.- Language features :RB is a strongly...
, and Modula-2
Modula-2 is a computer programming language designed and developed between 1977 and 1980 by Niklaus Wirth at ETH Zurich as a revision of Pascal to serve as the sole programming language for the operating system and application software for the personal workstation Lilith...
- Disallowing the statement following a then to be an if itself (it may however be a pair of statement brackets containing nothing but an if-then-close). This approach is followed by ALGOL 60
ALGOL 60 is a member of the ALGOL family of computer programming languages. It gave rise to many other programming languages, including BCPL, B, Pascal, Simula, C, and many others. ALGOL 58 introduced code blocks and the begin and end pairs for delimiting them...
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
, the latter relying also on indentation
An indentation may refer to:* A notch, or deep recesses; for instance in a coastline, or a carving in rock* The placement of text farther to the right to separate it from surrounding text....
- Requiring braces (parenthesizing) when an "else" follows an "if".
- Requiring every "if" to be paired with an "else". Thus is necessary in a language which does not allow mutable variables.