Spring 2002, CSE 428: Solution of Midterm 1

Question 1

    1. no
    2. no
    3. no
    4. no
  1. There are the following three kinds of ambiguity:
    1. Associativity of ";". For instance the string x := 1 ; y := 1 ; z := 1 hat two derivation trees
    2. Precedence between if-then-else and ";". For instance the string if x < y then x := 1 else y := 1 ; z := 1 hat two derivation trees
    3. Precedence between try-catch and ";". For instance the string try x := 0 catch x := 1 ; y := 1 hat two derivation trees
  2. Note: the full solution requires to draw the two different trees for each string. Ask the instructor if you have doubts about how to draw them.

Question 2

  1. 
        r |- e1  eval  false   r |- e2  eval  v
       ------------------------------------------
             r |- (e1 and e2) eval false
    
    
        r |- e1  eval  true    r |- e2  eval  v
       ------------------------------------------
              r |- (e1 and e2) eval  v
    
    
  2.     if e1 then e2 else false
    

Question 3

  1. The final value of z is 5
  2. The final value of z is 7

Question 4

  1. 
    AR of                            AR of the block  
    caller of p         p            where p is declared
            ^   ------------------      ^ 
         CL |___|_               |      | 
            +-->|                |      |
            |   ------------------      |
            |   |               _|______| SL
            |   |                |
            |   ------------------
            |   |    -----       |<-+---+---+---+ 
            |   |  x | 1 |       |  |   |   |   |          
            |   |    --------    |  |   |   |   |
            |   |  b | true |    |  |   |   |   |
            |   |    --------    |  |   |   |   |
         CL |   ------------------  |   |   |   |
            |                       |   |   |   |
            |          q I          |   |   |   |
            |   ------------------  |   |   |   |
            |___|_               |  |   |   |   |
            +-->|                |  |   |   |   |
            |   ------------------  |   |   |   |
            |   |               _|__|SL |   |   |
            |   |                |      |   |   |
            |   ------------------      |   |   |
            |   |    -----       |      |   |   |
            |   |  x | 2 |       |      |   |   |
            |   |    -----       |      |   |   |
         CL |   ------------------      |   |   |
            |                           |   |   |
            |          r I              |   |   |
            |   ------------------      |   |   |
            |___|_               |      |   |   |
            +-->|                |      |   |   |
            |   ------------------      |   |   |
            |   |               _|______|SL |   |
            |   |                |          |   |
            |   ------------------          |   |
            |   |    ---------   |          |   |
            |   |  b | false |   |          |   |
            |   |    ---------   |          |   |
         CL |   ------------------          |   |
            |                               |   |
            |          q II                 |   |
            |   ------------------          |   |
            |___|_               |          |   |
            +-->|                |          |   |
            |   ------------------          |   |
            |   |               _|__________|SL | 
            |   |                |              |
            |   ------------------              |
            |   |    -----       |              |
            |   |  x | 0 |       |              |
            |   |    -----       |              |
            |   ------------------              |
            |                                   | 
            |          r II                     |
            |   ------------------              |
            |___|_               |              |
                |                |              |
                ------------------              |
                |               _|______________|SL 
                |                |
                ------------------
                |    --------    |
                |  b | true |    |
                |    --------    |
                ------------------    
      
    
        CL = Control Link
        SL = Static  Link
    
  2. The value printed is 1

Question 5

  1. The value printed is 4
  2. The following variables are active during the execution of p():

Question 6

  1. No ML, One DP to the stack
  2. No ML, No DP
  3. One ML, No DP
  4. No ML, One DP to the heap
  5. One ML, One DP to the heap