CS 250 - Programming Languages                                            Spring 2001

Assignment 2 -- Due February 12.

Finish Chapter 2 of your book, and answer the following questions. Turn in your answers to the questions as hardcopy and your program code in an email.

  1. Statements like the following are syntactically ambiguous according to the Concrete Syntax of Jay in Appendix B of your book:
    1. if (x<0)
         if (x==0) y = y - 1;
         else y = 0;
    a.  Show why the statement is ambiguous.
    b.  Show how the Java syntax deals with these statements to eliminate this ambiguity.  That is, show the parse tree that results using the syntax for IfThenStatement in the Java Language Specification and argue that there is no other parse.
  2. Give a Jay abstract syntax tree for the following Assignment:
    1. z = 2*x + 3/y - 4;
  3. Consider the Abstract Syntax of Jay in Appendix B of your book.
  4. a. Draw an abstract syntax tree for the following Jay Statements using the that definition as a guide:
      x = 3; y = 4; Answer = x + 2 * y ;
    b. Give a postfix expression (which is equivalent to a postorder traversal of the abstract syntax tree) for this Jay construct.
  5. Discuss the following assertion: "While loops in Java are redundant. That is, any while loop can be equivalently expressed as an equivalent for loop." If this is true, describe an algorithm that would convert any while statement into an equivalent for statement. If it is not true, give an example of a while statement that has no equivalent for statement (i.e., a counterexample).
  6. Complete the design of a lexical analyzer for the small language Jay, whose lexical features and syntax are described in Appendix B of your book.  To assist you with this work, a skeleton version of the lexical analyzer Lex and its associated TokenStream and Token classes are provided in the files Token.java, TokenStream0.java, and Lex.java in the directory ~echown/courses/250/interpreter.  Running your completed program with the input text program.jay, your output should be identical with that shown in the file Lex.output in that directory.  In their current skeleton form, these programs produce the trivial output shown in the file Lex0.output. This program should be done on your own, not in pairs.

  7. A tutorial for using the Java Debugger, jdb, is available on-line.
  8. The syntax of Jay does not currently include String literals (only integers and booleans are present).  Suggest how these literals can be added to the lexical syntax of the language, and then how they could be recognized by the class TokenStream.
  9. Implement the changes you suggested in the previous question. Do include a writeup for the previous question.