[ Home ][ Classes ][ Research ][ Links ][ Biography ]

Up

CS Dept.




CSC 434 Programming Languages

Assignments and Exercises


Programming Assignments


Date Due

Assignment 1

2/2/2012 2/7/2012 (revised due date)

Assignment 2

2/21/2012

Assignment 3

3/27/2012

Assignment 4

4/10/2012

Assignment 5

4/26/2012




Study Guides

From Chapter 1

  1. Why is it useful for a programmer to have some background in language design, even though he or she may never actually design a programming language?
  2. What is the disadvantage of having too many features in a language?
  3. How can user-defined operator overload harm the readability of a program?
  4. Why is type checking the paramters of a subprogram important?
  5. What is aliasing?

From Chapter 3

  1. Compute the weakest precondition fo the following if statement:
    if (a == b)
        b = 2 * a + 1;
    else
        b = 2 * a;
    {b > 1}
  2. Give an operation semantic defintion of the C switch statement.
  3. Write an attribute grammar whose base BNF is that of Example 3.2 and whose type rules are the same as for the assignment statement example of Section 3.4.5.  Also show a parse tree for the sentence "A = A + (B * C)" assuming that A and B are declared as floats and C is declared as an int.  Label the parse tree with the attributes and identify which are intrinsic, inherited, and synthesized.
Solutions

From Chapter 5

  1. Consider the following program:

    procedure Main is
       X, Y, Z : integer;
       procedure Sub1 is
          A, Y, Z : Integer;
          begin  -- of Sub1
          ...
          end;   -- of Sub1
       procedure Sub2 is
          A, X, W : Integer;
          procedure Sub3 is
             A, B, Z : Integer;
             begin   -- of Sub3
             ...
             end;   -- of Sub3
          begin   -- of Sub2
          ...
          end;   -- of Sub2
    begin   -- of Main
    ...
    end;    -- of Main

    Assuming static scoping is used, list all the variables, along with the program unit where they are declared, that are visible in the bodies of Main, Sub1, Sub2, and Sub3.
  2. Consider the following program:

    function main is
       a, b, c : integer;
    begin   -- of main
    ...
    end;    -- of
    main
    function fun1 is
       b, c, d : Integer;
    begin  -- of
    fun1
       ...
    end;   -- of
    fun1
    function fun2 is
       c, d, e : Integer;
    begin   -- of fun2
       ...
    end;   -- of
    fun2
    function fun3 is
       d, e, f : Integer;
    begin   -- of
    fun3
       ...
    end;   -- of
    fun3

    Assuming dynamic scoping is used, list all the variables, along with the program unit where they are declared during the execution of the last function called if:
    1. main calls fun1; fun1 calls fun2; fun2 calls fun3.
    2. main calls fun2; fun2 calls fun3; fun3 calls fun1.

Solutions

From Chapter 6

  1. What are the arguments for and against representing Boolean values as single bits in memory?
  2. What are the arguments for and against Java's implicit heap storage recovery, when compared with the explicit heap storage recover required in C/C++?  Consider real-time systems. 
  3. In class we talked about how an array reference is mapped to a memory address for fixed stack-dynamic and stack-dynamic arrays where the elements of the array are in contiguous memory cells using both row-major and column-major order.  Give the formula for mapping of an array reference (A[i1]...[iM]) to a memory address for an array that has M dimensions with sizes N1...NM using column-major ordering.  Assume array subscripts begin at zero.
  4. Give the formula for mapping of an array reference (A[i][j][k]) to a memory address for an array that has 3 dimensions using row-major ordering.  However, this time assume array subscripts can be within a range of values that don't necessarily begin at zero.  In other words, the array is declared as:
     
    <base_element> A[lb1..ub1][lb2..ub2][lb3..ub3]

    (The first dimension index can range from
    lb1 (lower bound) through (and inclusive) ub1 (upper bound).
Solutions

From Chapter 7

  1. Describe a situation in which the add operator in a programming language would not be associative.
  2. Consider the following precedence table to operators:

    Precedence
    Operator
    Associativity
    Highest ( )
    left to right

    *, /, not
    left to right

    +, -, &, mod
    left to right

    - (unary)
    right to left

    =, /=, <, <=, >=, >
    left to right

    and
    left to right
    Lowest
    or, xor
    left to right

    Give the order of evaluation of the following expressions.  Use parentheses and subscripts to indicate the order:
    1. a * b - 1 + c
    2. a * (b - 1) / c mod d
    3. (a - b) / c & (d * e / a - 3)
    4. -a or c = d and e
    5. a > b xor c or d <= 17
    6. -a + b

    For example, the expression a + b * c + d should be written as (3  (2  a  +  (1  b  *  c  )1  )2  +  d   ) 3.

  3. Let the function f() be defined as:
    int f(int *k) {
        *k += 4;
        return 3 * (*k) - 1;
    }

    Suppose f() is used in a program as follows:

    void main() {
        int i = 10, j = 10, sum1, sum2;
        sum1 = (i / 2) + f(&i);
        sum2 = f(&j) + (j / 2);
    }


    If you are unfamiliar with the syntax (C) used here, it means that the parameters, i and j, are passed by reference.  That means that the function f() modifies its parameters (i.e. side effect).  What are the values for sum1 and sum2 if:
    1. the operands are evaluated left to right?
    2. the operands are evaluated right to left?
Solutions

From Chapter 8

  1. What are the pros and cons of using unique closing reserved words on compound statements (such as endif, endfor, and endwhile).
  2. In Ada, the choice lists of the case construct must be exhaustive so that there can be no unrepresented values in the control expression. In other words, the others clause (default case) is required.  In Java and C/C++, unrepresented values do not need to be addressed (because the default is optional).  What are the pros and cons of these two designs?
Solutions


From Chapter 9

  1. Consider the following program (in no particular language):

    void swap(int a, int b) {
        int temp;
        temp = a;
        a = b;
        b = temp;
    }
    void main () {
        int value = 2, list[5] = {1, 3, 5, 7, 9};
        swap (value, list[0]);
        swap(list[0], list[1]);
        swap(value, list[value]);
    }


    What are the values of the variables value and list (all 5) after each call to the swap() function using each of the following parameter passing techniques:
    1. Pass by value
    2. Pass by reference
    3. Pass by value-result
    4. Pass by name

  2. Consider the following program (in no particular language):

    void f(int first, int second) {
        first += first;
        second += second;
    }
    void main () {
        int list[2] = {1, 3};
        f(list[0], list[1]);
    }


    What are the values of the variable list (both values) after the call to the f() function using each of the following parameter passing techniques:
    1. Pass by value
    2. Pass by reference
    3. Pass by value-result
    4. Pass by name

From Chapter 10

  1. Consider the following program (in no particular language):

    procedure Bigsub
        MySum : Float;
        L : Float;
        procedure A
            X : Integer;
            procedure B(Sum : Float)

                Y, Z : Float;
                begin // procedure B
                    Z := Sum;
                    Y := MySum;
                    MySum := MySum + 1;
                    C(Z);
                end;
            begin // procedure A
                X := L;
                MySum := MySum + 1;
                B(X);
            end;
            procedure C(Plums : Float)
            begin // procedure C
                MySum := MySum + Plums;
                ...                               <-------------------- (1)
            end;
        begin // procedure Bigsub
            ...
            MySum := 0;
            L := 10;
            A();
            ...
        end;

    1. Assuming that we are using static chains to implement non-local variables, give the chain offset / local offset for each variable that is within scope for each procedure (Bigsub, A, B, and C)
    2. Assuming that we are using displays to implement non-local variables, give the display offset / local offset for each variable that is within scope for each procedure (Bigsub, A, B, and C)
    3. Show the stack with all the activation records instances and thier contents (including variables, values, static links, and dynamic links) when execution reaches point (1) in procedure C above.


Reading

Test 1

  • Chapter 1
    • Sections 1.1 - 1.3
    • Sections 1.5 - 1.7
    • Know the Language Evaluation Criteria and the things that contribute to those
  • Chapter 3
    • Skimsections 3.1 - 3.3 (this should be review of CSC 360)
    • Sections 3.4 - 3.5 (Skim 3.5.2.5 through 3.5.2.8)
  • Chapter 5
    • All of Chapter 5

Test 2

  • Chapter 6
    • All of Chapter 6
    • Understand arrays and demonstrate the mapping of array reference to memory location using both heap-dynamic an stack-dynamic arrays
    • Understand pointer types, the problems with pointers, and possible solutions
  • Chapter 7
    • All of Chapter 7
  • Chapter 8
    • Sections 8.1 - 8.4

Test 3

  • Chapter 9
    • Sections 9.1 - 9.6
    • Be able to determine what the referencing environment is for a subprogram using static or dynamic scoping
    • Be able to determine the result of calls to subprograms using the parameter passing techniques: pass-by-value, pass-by-result, pass-by-value-result, pass-by-reference, and pass-by name.
  • Chapter 10
    • Section 10.1-10.5
    • Understand the subprogram call sequence
    • Know what goes in the activation record for a subprogram
    • Be able to show the activation record instances on the stack given a program and call sequence
    • Be able to show the static and dynamic chains on the stack
    • Be able to compute the static chain offset / local offset pairs for all variables that are within scope and any give place in the program

Final

  • Major Field Test



This page was last updated: January 25, 2012

Email:

[ Home ][ Classes ][ Research ][ Links ][ Biography ]