CSE3001/CSE5040: Homework #2, Imperative Languages

Due: Thursday, February 18, 1998, in class
  1. Argue for and against the operator precedence rules used in APL.
  2. Give an attribute grammar for checking if every variable used is declared. Use the following underlying context-free grammar.
          P    ::= declare Decl begin Stmt end
          Decl ::= [empty]
          Decl ::= identifier : Integer ; Decl
          Stmt ::= [empty]
          Stmt ::= identifier := 3 ; Stmt
          
    You may choose relatively complex attributes, like sets of identifiers. P must have a synthesized attribute check which is true if and only if every variable used (found in an assigment statement) is delcared.
  3. Bertrand Russell noticed the following inconsistency. Suppose that it were possible to define a set R whose elements are all the sets that do not contain themselves. That is, R = { S | S not in S }. Is R in R? If R in R then R not in R, a contradiction. So, R not in R, but then R in R, again a contradiction. This problem can be simulated in programming languages. Sets can be simulated by boolean-valued functions, called characteristic functions. The function representing a set of integers S will be declared like
    function S (x: integer): boolean
    
    and will return true exactly when the argument is in the set S. In a language like Pascal where functions can be passed as arguments it is possible to represent sets of sets. This makes it possible to try to represent the set R given above. Hint: This problem has nothing to do with the set data type of Pascal.
    1. Using the characteristic function representation of sets, write a Pascal program passing a function as a parameter to implement the the set complement operation. The complment of a set X is the set whose members are those that are not in X.
    2. Show that the set R may be simulated in the original definition of Pascal, which does not require specification of parameters of procedural parameters, by writing a Pascal program.
    3. What is the behavior of your program?
    4. Show that R is not expressible in versions of Pascal that require such specification. You have the difficult task of arguing that you cannot write such a program.
  4. Scope rules in Ada are different from those in Modula-3 and Java. Here is an example in Ada:
          declare
             x: Integer := y;
             y: Integer := 1;
          begin
             -- Print x and y
          end;
          
    Here is the same situation in Modula-3.
          VAR
             x: INTEGER := y;
             y: INTEGER := 1;
          BEGIN
             (* Print x and y. *)
          END
          
    What do you think happens when you compile the Ada program? What do you think happens when you run the Modula-3 program? What is the difference in scope rules? Explain the advantages and disadvantages of each approach.

    For the brave, what happens when you compile and run the Java programs below? Answer questions 1, 2, and 3 in the programs below and explain. Java acts like Ada in this example:

          class A {
             static int x = y;         // (1) Is y visible here?
             static int y = 1;
             static { /* Print x and y */ }
          }
          
    But you can get the Modula-3 behavior indirectly, because the scope rules in Java for classes and methods are different than those for simple class variables in Java. Consider:
          class Order {
             static int y = Later.y;     // (2) Is Later (and y) visible here?
          }
    
          class Later {
             static int x = Order.y;
             static int y = 1;
             static { /* Print x and y */ }
          }
          
    The order in which the classes Order and Later are declared makes no difference. Or an entirely different Java example:
          class Three {
             static int gety () {return y;}  // (3) Is y visible here?
             static int x = gety();
             static int y = 1;
             static { /* Print x and y */ }
          }
          

Ryan Stansifer <ryan@cs.fit.edu>
Last modified: Fri Feb 12 15:20:54 EST 1999