CMSI 488/588
Quiz 1

The test is open-everything with the sole limitation that you neither solicit nor give help while the exam is in progress.

Many of the questions ask about the language Carlos. We have not covered the language in great detail during lectures, This test, therefore, is measuring (at least in a few problems) how well you can look at a language specification and answer questions about it.

ProblemYou gotOut of
1
 
15
2
 
15
3
 
15
4
 
15
5
 
15
6
 
15
7
 
10
TOTAL
 
100
  1. Is it possible for the assignment statement
        x.x = x;
    
    to ever legally appear in a Carlos program? If it can, state what it means, semantically. If not, state why it is impossible (technically), hinting at either (1) why allowing it would be stupid (inconsistent with the language design), or (2) the inexplicable oversight or stupidity of the language designer for leaving it out.
  2. For each of the following Carlos fragments, tell whether it is a lexical error, syntax error, static semantic error, dynamic semantic error, or no error.
    1. c.real = 2.0; c.imag = -3.5;
    2. int f(int x) {int x = x;}
    3. string `ohana = "family";
    4. string pet = "dog"; pet = "rat";
    5. string pet = "cat"; pet[0] = 'r';
    6. int f(int x) {} int g() {f(1,2,3,4);}
  3. Sketch the AST for the Java fragment:
        static private synchronized long woofer(Object... v) {
            for (int y : f(x)) {
                x = p.data[0] * (2<<   5|-  x---c);
            }
        }
    
  4. Consider a language for describing vector graphics. An example program in this language is (formatted ugly to highlight the fact that line breaks do not matter):
        deg color 1 0 0 ccw
        90 forward 4 color 0 0 1 [ ccw 90
        forward 1.5 ] cw 90 forward 1.5
    

    This program draws the letter T with a red vertical line of size 4 units and topped with a 3 unit blue line. A program is a sequence of instructions. The instructions are:

        deg - switch to degree mode
        rad - switch to radians mode
        ccw θ - turn left (counterclockwise) by angle θ
        cw θ - turn right (clockwise) by angle θ
        forward n - draw a line by moving forward n units.
        backward n - draw a line by moving backward n units.
        color r g b - set color (r,g,b), values are floats in the range 0 to 1.
        [ - save current state
        ] - restore previously saved state
    
    Write a macrosyntax in EBNF for this language. Handle the brackets reasonably, please. State whether your grammar is LL(1) (meaning "can be parsed top-down with only one lookahead symbol) or not, and whether it is ambiguous or not.
  5. Write a regular expression for the language of "positive hexadecimal numerals divisible by 65536". (Here 65536 is a decimal numeral. Also, we are talking about regular hexadecimal numerals, not two's complement numerals.) Does there exist an LL(1) grammar for this language? If so, give it. If not, does there exist an LL(k) grammar for some k ≠ 1? If so, what is the smallest such k? If not, why not?
  6. There is a tradition in computer science, about as popular as "Hello, World", to name sample variables foo, goo, hoo, FOO, Foo, and so on. It is time to put a stop to this.

    1. Write a regular expression for "any string of alphanumeric characters, beginning with a letter, which is anything BUT a three-character string ending with 'oo' (case insensitive). You may use any of Java's regex machinery like lookahead or lookbehind if you like.
    2. Suppose you were trying to encode this regex as a JavaCC token rule. Show a JavaCC token specification for this class of strings. Hard, isn't it?
    3. Fortunately, if you were really using JavaCC, you could avoid the hassle of ugly token specifications. Explain how the prohibition of -oo variables would be handled in a real JavaCC application.
  7. EBNF generally uses

    Suppose I wanted to add a new one:

    Show how to write A # B # C using only the conventional EBNF markup.