next up previous
Next: About this document

Compiler Design
Final Exam , Spring 1995.


e

Answer All Questions:

NAME:

LOGIN NAME:

This is an open book, open notes examination. There are eight questions. All questions carry 15 points. Some questions have a and b parts in which case they are weighed equally. If you get 100 points, then you have max-ed the paper. It is possible that you get more than 100 points, which will be counted as extra points towards your grade. Goodluck and have a happy summer.

Problem Points Scored Points Possible
          
1 15
         
2 15
         
3 15
         
4 15
         
5 15
         
6 15
         
7 15
         
8 15
         

  1. a)Construct minimum state DFA for the following regular expression: b) Write a Lex program to reverse the letters in each word for a given text. (e.g. if the input is : begin I am Smart end the output is: ngieb I ma tramS dne ) A word is a bunch of letters and digits separated by spaces or newlines or tabs.
  2. a) Consider the grammar
    S -> ( S ) S | ) S ( S | epsilon
    The terminal symbols are ( and ). Is the above grammar ambiguous and explain with examples.

    b) Construct a SLR parsing table for the following grammar.

    E -> E + T | T
    T -> T F | F
    F -> F * | a | b
  3. Give a syntax directed translation scheme ( a pseudo yacc program will suffice) to differentiate expressions formed by applying the arithmetic operators + and * to the variable x and constants. Assume no simplification takes place. As an example if the input is x * (x + 1995) , the output will be 1 * (x + 1995) + x * ( 1 + 0) . The rules for differentiation: diff(E1+E2) = diff(E1)+ diff(E2); diff(E1*E2) = E2* diff(E1) + E1*diff(E2);
  4. a) Find the most general unifier for the following type expressions:

    b) What is the type of the following function (please derive the steps in the style of 6.12

    fun member(a,x) = 
    if null(x) then error
    else if a = hd(x) then x
    else member(a, tl(x));

    Assume the type of error is void.

  5. a)Suppose the procedure swap2 is declared as follows:

    procedure swap2(x,y: integer);
    	procedure f() : integer;
    		var x:integer;
    	begin (* f *)
    		z := x; x: = y; return z
    	end f;
    begin (* swap2 *)
    	y := f()
    end swap2;
    Describe the effect of the procedure call swap2(i,A[i]) under each of the following parameter-passing methods:
    • call-by-value
    • call-by-reference
    • call-by-value-result
    • call-by-name
    b) Do problem 7.8 page 457 just the b) part only.
  6. a) Do problem 8.14 page 510. b) Convert the following into three address statements.
    main()
    { int i, a[5],b[5];
     for (i=0;i<5;i++)
     a[b[i]] = b[a[i]];
    }
  7. a) What information should be maintained in an abstract syntax tree, so that assembler code can be generated by walking down the tree. b) For this C program segment, construct the tree and generate the assembler code you are allowed to use pseudo assembler code.

    for (i=0;i<10;i++)
    	a[i] = random() % 2;
  8. a) Find the optimal code on a single register machine. You are allowed to rearrange the statements but preserve the same functionality.
    a:=b+c; d:=e; f:= a* c;
    b) For Fig 10.74 (page 713) Compute the live variables at the end of each block.



next up previous
Next: About this document

Moorthy
Fri Apr 25 14:03:25 EDT 1997