CS 330 Home Schedule Resources

Assignment 2
Kernel Language Syntax and Semantics

Estimated time: 2 hours

The objective of this homework is to

Exercises

  1. Writing BNF descriptions of data (10 points)
    1. Write a BNF grammer that describes a LIFO stack of elements, where each element can be an integer or an atom. Represent the stack using Oz lists or records.
    2. Write a one-line BNF description of a Java-style if-statement that is comparable to the Oz kernel if statement (on page 49).

  2. Interpreting BNF (20 points)
    For each of the following code fragments, determine whether it is a valid statement in the kernel language. If so, prove it by providing a syntactic derivation. If not, translate it into one or more valid kernel language statements.
    1. f(a X Y) = f(A x Y)
    2. 3=X
    3. Foo=proc {$ X Y} X=Y end
    4. local N in 
         N=true
         if N then 0 else 1 end
      end 
      
    5. local A in
         local C in
           A=a 
           C=c
           case s(1:A 3:C) of s(1:X 3:Y) then {Browse X}
           else {Browse 'else'} end
         end
      end
      

  3. Free and bound identifiers. (5 points) Consider the following statement:
       proc {P X}
          if X>0 then {P X-1} end
       end
    
    Is the second occurrence of the identifier P free or bound? Justify your answer. Hint: this is easy to answer if you first translate to kernel syntax.

  4. Contextual environment. (10 points) Section 2.4 explains how a procedure call is executed. Consider the following procedure MulByN and its call:
        declare MulByN A=10 B N in
        N=3                                % *
        proc {MulByN X ?Y}                 % *
             Y=N*X                         % *
        end                                % *
        {MulByN A B}
        {Browse B}
    
    What value, if any, is printed in the Browser? Now modify the code so that the lines marked with an asterik form the body of a local statement that declares a new identifier N. Consider two scenarios: one where the identifier N is removed from the declare line, and another where it remains and is bound to 4. How does the overall execution change? Explain why in terms of what is happening to the environment when MulByN is called.

  5. Functions and procedures. (5 points) If a function body has an if statement with a missing else case, then an exception is raised if the if condition is false. Why is this behavior correct? This situation does not occur for procedures. Explain why it should not.

  6. Feedback.
    Scale
    1. Not at all
    2. Not very
    3. Slightly
    4. Somewhat
    5. Mostly
    6. Very
    7. Extremely