The dangling else ambiguity

• Ambiguity in programming language constructs

• A construct in a programming language is ambiguity, if the construct can be interpreted in more than 1 way

• Some programming languages (notably: C, C++, Java) have an if statement that is ambiguous - i.e., the statement can be interpreted in MORE THAN 1 WAY

• This dangling-else ambiguity problem was first discovered around 1960 in the ALGOL60 programming language

• The Dangling-Else ambiguity

• Suppose we want to write an FONT color="blue"> conditional statement to determine the shipping rate to different countries of the world:

 To ship a 1 lb packet within the US, the cost is \$5 , except for Hawaii To ship to Hawaii (which is very remote), the cost \$10 To ship a 1 lb packet outside the US, the cost is \$20

The following is an if-statement that computes the "shippingCharge":

 ``` double shippingCharge; shippingCharge = 5.00; // Assume the cost is \$5 if ( country is equal to "USA") ) if ( state is equal to "HI" ) shippingCharge = 10.00; // Hawaii will cost \$10 else shippingCharge = 20.00; // Outside USA is \$20 ```

• There are two ways to read the above program syntactically correctly:

First Way Second Way
```
double shippingCharge;

shippingCharge = 5.00;

if ( country equals "USA" )
if ( state equals "HI" )
shippingCharge = 10.00;
else
shippingCharge = 20.00;
```
```
double shippingCharge;

shippingCharge = 5.00;

if ( country equals "USA" )
if ( state equals "HI" )
shippingCharge = 10.00;
else
shippingCharge = 20.00;
```

You can see the if-else association better after indenting it properly:

First Way Second Way
```
double shippingCharge;

shippingCharge = 5.00;

if ( country equals "USA" )
if ( state equals "HI" )
shippingCharge = 10.00;
else
shippingCharge = 20.00;
```
```
double shippingCharge;

shippingCharge = 5.00;

if ( country equals "USA" )
if ( state equals "HI" )
shippingCharge = 10.00;
else
shippingCharge = 20.00;
```

• Clearly, the two interpretations are syntactically correct

Futhermore the two interpretations result in different program execution:

1. The First Way interpretation result in computing the cost in the way that we wanted

2. The Second Way interpretation computes a shipping charge of \$5 for international shipments, \$10 for Hawaii, and \$20 for other states in the US !!!

• Clearly, ambiguity in computer programs is unaceptable (the computer could pick the wrong interpretation )

• Resolving the Dangling-Else ambiguity

• There are a number of techniques to resolve ambiguity in a language:

1. The elegant way is to design languages that are NOT ambiguous

2. The simple-minded way is to design rules to resolve ambiguity

(The latter method is used when the resolve an ambiguity when an ambiguity is discovered after a language has been designed and in use)

• How to design languages that avoid the dangling-else ambiguity

• A slightly more complex syntax rule is used for the conditional statements that includes a terminal "fi" symbol :

 ``` if ( boolean expression ) one-or-more-statement fi if ( boolean expression ) one-or-morestatement else one-or-more-statement fi ```

• Using the new syntax rule the above programs will be written as:

First Way in new syntax Second Way in new syntax
```
double shippingCharge;

shippingCharge = 5.00;

if ( country equals "USA" )
if ( state equals "HI" )
shippingCharge = 10.00;
fi
else
shippingCharge = 20.00;
fi
```
```
double shippingCharge;

shippingCharge = 5.00;

if ( country equals "USA" )
if ( state equals "HI" )
shippingCharge = 10.00;
else
shippingCharge = 20.00;
fi
fi
```

There is ONLY ONE WAY to read each of the program segment above !! (try it !!!)

• Java, C and C++ have chosen to resolve the Dangling-Else ambiguity uses the following rule:

 An else keyword always associates with the nearest preceeding if keyword that does NOT cause a syntax error

• Therefore, the C/C++ compiler will read this program as follows:

 ``` double shippingCharge; shippingCharge = 5.00; if ( country.equals("USA") ) if ( state.equals("HI") ) shippingCharge = 10.00; else shippingCharge = 20.00; ```

It will associate the else keyword with the second if keyword

• \$64,000 Question:

Then how do you write the following C++ program:

 ``` double shippingCharge; shippingCharge = 5.00; if ( country equals "USA" ) if ( state equals "HI" ) shippingCharge = 10.00; else shippingCharge = 20.00; ```

• You need to use { } to introduce syntax errors when C++ tries to associate the else with the second if:

 So, the proper way to write the program is: ``` double shippingCharge; shippingCharge = 5.00; if ( country.equals("USA") ) { if ( state.equals("HI") ) shippingCharge = 10.00; // Shipping to Hawaii is \$10 } else shippingCharge = 20.00; // Shipping to outside USA is \$20 ```

Associating the else with the second if will now result in a syntax error !!!