CPSC425 Spring 2008
HW 6: The Java Language Specification and Lexical Analysis in ML

  1. In class we talked about using BNF to define a statement in Java. To see how it's really done, consult the the online Java Language Specification at http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html. Use the table of contents to browse lexical and syntactic grammars and to familiarize yourself with the grammar notation being used. Remember that the lexical grammar tells how individual tokens are formed, and the syntactic grammar tells how they are put together to form legal programs. Most of the fun happens in the syntactic grammar.

    Now answer the following questions. You may find the index helpful; note that the specification is the relevant piece of the grammar.

    1. The grammars in this document are in a form of extended BNF. Identify two extensions to plain BNF that are used.
    2. We noted that in Java every statement is followed by a semicolon. How is this specified in the grammar?
    3. We discussed the possibility of a statement actually being a series of statements, and whether curly braces were needed in some situations but not in others. Are the rules different for a series of statements inside a while? A for statement? An if statement? A method body? You'll find lots of shenanigans designed to deal with the "dangling else" problem, mostly referring to "short ifs." Try to figure out what's going on here; we'll talk more about it in class on Tuesday.

  2. Carefully read the original HW6 handout, which describes the ML lexer assignment. You don't need to write the whole lexer yet; get started by using String.tokens and String.translate, the two functions that are described under Program Structure, to write a program that takes a string and returns a preliminary list of the tokens in the string. "Preliminary" means you don't have to recombine strings, real numbers, :=, <=, >, or >= -- for example, for the real number 14.259 you can return the tokens 14, ., and 259.

    String.tokens and String.translate will do most of the work for you, but you have to write and pass functions that do the right thing. (Confused about a function taking a function as a parameter? See Chapter 7, Exercise 7 on page 116 for a brief introduction, and Chapter 9 for more information.) Study the examples on the handout!!

E-mail your ML code and the answers to the grammar questions before class. -