final

final - CS 352 Compilers Principles and Practice Final...

This preview shows pages 1–5. Sign up to view the full content.

CS 352 — Compilers: Principles and Practice Final Examination, 12/13/10 Instructions: Read carefully through the whole exam ﬁrst and plan your time. Note the relative weight of each question and part (as a percentage of the score for the whole exam). The total points is 100 (your grade will be the percentage of your answers that are correct). This exam is closed book, closed notes . You may not refer to any book or other materials. You have two hours to complete all three (3) questions. Write your answers on this paper (use both sides if necessary). Name: Student Number: Signature:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
1. (20%) Consider the following simple grammar and the language it describes: S ε | a | bSd | bScSd (a) (5%) Is this grammar LL(1)? Explain. [There is a simple argument.] Answer: No, the grammar is not LL(1) by inspection since the last two rules have a common preﬁx. (b) (5%) Is this language LL(1)? If so, exhibit a simple LL(1) grammar; if not, explain why not. Answer: Yes, the language is LL(1); by factoring the common preﬁx we get: S ε | a | bST T d | cSd (c) (5%) Is this grammar LR(0)? Explain. [Again, there is a simple argument.] Answer: No, the grammar is not LR(0) because there is a shift-reduce conﬂict between the ﬁrst rule and the other three. (d) (5%) Is this language regular? Why or why not? Answer: No, the language is not regular because it involves bracketing, such as b n d n , known not to be regular because it requires an unbounded number of states and so cannot be recognized by a ﬁnite automaton. 2
2. (25%) Consider the following M3 program as it executes: 1 MODULE List; 2 TYPE 3 T = OBJECT 4 head: INTEGER ; 5 tail: T; 6 METHODS 7 merge(t: T): T := Merge; 8 END ; 9 PROCEDURE Cons(head: INTEGER ; tail: T): T = 10 VAR t := NEW (T); 11 BEGIN 12 t.head := head; 13 t.tail := tail; 14 RETURN t; 15 END Cons; 16 PROCEDURE Merge(this, other: T): T = 17 BEGIN 18 IF other = NIL THEN 19 RETURN this; 20 ELSIF this.head < other.head THEN 21 RETURN Cons(this.head, other.merge(this.tail)); 22 ELSE 23 RETURN Cons(other.head, this.merge(other.tail)); 24 END ; 25 END Merge; 26 VAR 27 t1 := Cons(1, Cons(3, NIL )); 28 t2 := Cons(2, Cons(4, NIL )); 29 t := t1.merge(t2); 30 BEGIN 31 END List. 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
(a) (20%) Show a diagram of MIPS stack frames at the point when this program is execut- ing line 19 of Merge where other= NIL . Show where all the local variables and heap variables are (assume that all local variables are stored in memory in the stack, not in registers); show the value of all integer variables in the stack and heap, as well as each
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/23/2012 for the course CS 352 taught by Professor Staff during the Fall '08 term at Purdue.

Page1 / 13

final - CS 352 Compilers Principles and Practice Final...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online