Dangling else
Encyclopedia
The dangling else is a problem in computer programming
in which a seemingly well-defined statement can become ambiguous. In many programming language
s one may write conditionally executed code in two forms: the if-then form, and the if-then-else form:
if a then s
if a then s1 else s2
This gives rise to an ambiguity in interpretation whenever an if-then form appears as
if a then if b then s else s2
In this example,
if a then (if b then s) else s2
or
if a then (if b then s else s2)
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:
Programmers using C
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.
and its if, within the syntax. This usually helps avoid human
errors.
Possible solutions are:
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
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:
if a then s
if a then s1 else s2
This gives rise to an ambiguity in interpretation whenever an if-then form appears as
s1
in an if-then-else form:if a then if b then s else s2
In this example,
s
is unambiguously executed when a
is true and b
is true, but one may interpret s2
as being executed when a
is false (thus attaching the else to the first if) or when a
is true and b
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:if a then (if b then s) else s2
or
if a then (if b then s else s2)
Avoiding ambiguity while keeping the syntax
This is a problem that often comes up in compilerconstruction. 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 parserLR parserIn 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 grammarParsing expression grammarA 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 (programming language)
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 elseand its if, within the syntax. This usually helps avoid human
errors.
Possible solutions are:
- Having an "end if" statement. Examples of such languages are ALGOL 68ALGOL 68ALGOL 68 isan imperative computerprogramming language that was conceived as a successor to theALGOL 60 programming language, designed with the goal of a...
, AdaAda (programming language)Ada is a structured, statically typed, imperative, wide-spectrum, and object-oriented high-level computer programming language, extended from Pascal and other languages...
, EiffelEiffel (programming language)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/SQLPL/SQLPL/SQL is Oracle Corporation's procedural extension language for SQL and the Oracle relational database...
, REALbasicREALbasicRealbasic 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-2Modula-2Modula-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 60ALGOL 60ALGOL 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...
and PythonPython (programming language)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 indentationIndentationAn 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....
to disambiguate.
- 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.