Previously discussed
• The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant (See: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/03/intro-java.html)
• All that the Java compiler cares about is syntactical correctness
Ambiguous syntax
• Ambiguous syntax
• A language allows ambiguous syntax is the same sentence can be read in more than one way
Ambiguous syntax (cont.)
• Example: English (in fact, all human languages)
can mean:
• An American History professor
• a History professor who is an American citizen (i.e.: American History professor)
• a professor of American History. (i.e.: American History professor)
Ambiguous syntax (cont.)
• How humans deal with ambiguity:
• Humans can often use additional information to resolve the ambiguity
(E.g., we have additional information about the specific professor)
• If we cannot resolve the ambiguity, we (humans) can ask further questions to resolve it.
Ambiguity in programming languages ?
• Difference between natural language and programming language:
• A natural language evolves through usage
It's evolution is not controlled
---------------------------------------------------------------
• A programming language is designed by one or a group of humans
It's design is completely controlled
Ambiguity in programming languages ? (cont.)
• Commonly held goal in the design of a programming language:
• A programming language should have an unambiguous syntax
• In other words:
• There is exactly one way to interpret each program statement syntactically
Ambiguity in programming languages ? (cont.)
• Historical facts about programming languages:
• Older programming languages does have ambiguous syntax
These languages include:
• Algol 60
• Pascal
• C
Ambiguity in programming languages ? (cont.)
• Modern programming languages are unambiguous
Examples:
• Algol 68
• Ada
• Modula
Ambiguity in programming languages ? (cont.)
• However:
• Because Java (and C++) are derived from the older programming language C (and C has an ambiguous syntax):
(This is very unfortunate for a modern language like Java and C++, but the designers want to keep C's syntax extremely badly)
• Java has inherited the ambiguous syntax in C...
The dangling else ambiguity
• The dangling else ambiguity was first discovered in the programming language Algol 60
• I will use a concrete example to illustrate this famous ambiguity
The dangling else ambiguity (cont.)
• Programming problem: shipping cost
• Country code for US = 1
Cost to ship to a package to a destination in the US = $5.00, except for Hawaii which cost $10.00
The state code for Hawaii = 50
• The shipping cost for a destination outside the US is $20.00
The dangling else ambiguity (cont.)
• Write a Java program than reads in:
and prints the shipping cost
• A country code (integer)
• A state code (integer)
The dangling else ambiguity (cont.)
• Note:
• There is no ambiguity in the algorithm given as a structure diagram
• The ambiguity will be introduced when we write the algorithm in the Java programming language !!!
cost = 5.0;
if ( country_code == 1 ) if ( state_code == 50 ) cost = 10.0; // Hawaii else cost = 20.0; // Outside US
The dangling else ambiguity (cont.)
(The algorithm is unnecessarily confusing because I want to show the ambiguous syntax....)
The dangling else ambiguity (cont.)
• There are 2 different yet syntactically correct ways to read the if-statements:
The dangling else ambiguity (cont.)
• Explanation:
• The first way is to associate the keyword else with the first keyword if
See the corresponding structure diagram
• The first if is an if-else-statement
• The second if is an if-statement that comprises the then-part of the (first) if-else-statement
The dangling else ambiguity (cont.)
• The second way is to associate the keyword else with the second keyword if
See the corresponding structure diagram
• The first if is an if-statement
• The second if is an if-else-statement that comprises the then-part of the (first) if-statement
The dangling else ambiguity (cont.)
• (This is the only ambiguous syntax in the Java programming language which it had inherited from C)
The dangling else ambiguity (cont.)
• Resolving the dangling-else ambiguity:
• Java (and C, C++) imposes the following rule:
• The keyword else is associated to the nearest keyword if that makes a syntactically correct statement
The dangling else ambiguity (cont.)
• Java program:
import java.util.Scanner; public class DanglingElse01 { public static void main(String[] args) { int country_code, state_code; double cost; Scanner in = new Scanner(System.in); // Construct Scanner object System.out.print("Enter country code: "); country_code = in.nextInt(); // Read in integer System.out.print("Enter country code: "); state_code = in.nextInt(); // Read in integer
The dangling else ambiguity (cont.)
cost = 5.0; if ( country_code == 1 ) if ( state_code == 50 ) cost = 10.0; // Hawaii else cost = 20.0; // Outside US System.out.println("Shipping cost = " + cost); } }
The dangling else ambiguity (cont.)
• Sample execution:
Enter country code: 1 (code for US) Enter country code: 40 (not Hawaii) Shipping cost = 20.0 (should be $5 !)
The dangling else ambiguity (cont.)
• Example Program: (Demo above code) – Prog file:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/Progs/DanglingElse01.java
• How to run the program:
• Right click on link and save in a scratch directory
• To compile: javac DanglingElse01.java
• To run: java DanglingElse01