CS471, Programming Language Structure I
Spring, 2003

Examination 1

Closed Book. Answer all the questions. The total points for each question or part of a question follows it in parentheses, thus: (12)

1

Below is a syntax definition of an experssion involving only addition (+) and multiplication (*).

a.       Show, by drawing two different parse trees for the expression 3+4*5 that the grammar is ambiguous.

b.      What value would be produced for each tree?

 

<expr> ::= <expr> + <expr> |

           <expr> * <expr> |

           ( <expr> ) |

           <number>

<number> ::= <number> <digit> | <digit>

 

2

Examine the code written with Pascal syntax below and answer the questions beneath it.

program A;

  var x : integer;

  procedure B;

    procedure D;

    begin

      print(x) // what does this print?

    end;

  begin

    D

  end;

  procedure C;

    var x : int;

    procedure E;

       var x : integer;

    begin

      x := 1;

      B

    end;

  begin

    x := 2;

    E

  end;

begin

  x := 3;

  C

end.

a. What does the program print if static scoping is used?

b. What does the program print if dynamic scoping is used?

c. Using pass-by-value parameters in appropriate places, modify the procedure calls (and the corresponding procedure declaration) in the simplest way possible that will print the third value for x (1, 2 or 3) not printed in parts a or b. Do not change any assignment statements or the order of procedure calls.

3

Draw diagrams showing the difference between compiling source code into executable machine code and interpreting the same source code. Why is it possible for the two methods to produce different results when the program is executed?

4

Below is a fragment of a denotational definition for assignment and expressions involving addition and subtraction of natural numbers.

S ::= S1;S2 | I=E | ...

E ::= I | N | E1 + E2 | E1 * E2

 

Mv S1;S2bs = MvS2b(Mv S1bs)

MvI=Ebs = update(vIb, MvEbs, s)

MvE1 + E2bs = MvE1bs + MvE2bs

MvE1 * E2bs = MvE1bs * MvE2bs

MvIbs = s(vIb )

MvNbs = n, where n is a member of the set of Natural numbers

 

Starting with an initial store, s0 = {(A,1),(B,2)}, give an expression, in terms of the function update and in its simplest form, for the final store after the execution of the following two statements:

 

A = 2;

B = A * 3

 

Hint: you can derive the result by expanding the expression Mv A=2;B=A*3 bs0. Your final expression should be an update of s1 (after A=2) which, in turn is an update of s0.

5

Even though it compiles and runs perfectly well, there is something horribly wrong with the following C code which is intended to reverse the contents of the array A. What is it and how might it be fixed?

#include <stdio.h>

#define SIZE 5

 

int A[SIZE] = {1, 2, 3, 4, 5};

 

void reverse(int *a, int size) {

  int *pe = &a[size - 1];

  int *ps = &a[0];

  while (pe > ps) {

    int *t = pe;

    *pe = *ps;

    *ps = *t;

    pe--;

    ps++;

  }

}

 

main() {

  int i;

  reverse (A, SIZE);

  for (i = 0; i<SIZE; i++)

    printf("%d:%d\n", i, A[i]);

}

 

Hint: The progam actually prints:

0:1

1:2

2:3

3:2

4:1