Salman Bin Abdul Aziz University Collage of Computer Engineering and Sciences Computer Science Department 1433-1434 (2012-2013) First Term CS 4300 Compiler Construction 8 th Level Final Exam 120 Minutes In the Name of ALLAH, the Most Merciful, the Most Beneficent. Make sure your exam consists of 4 questions in 7 pages. Answer all questions. Time distribution: 4 questions 25 minutes = 100 minutes + 20 minutes over = 120 minutes. Name العربية)........ :(باللغة Uni ID:.... Marks Distribution (Do not write in this part.) Question No. 1 2 3 4 Total Degrees 10 10 10 10 40 1) State whether the following are true or false. 1. An interpreter can usually give better error diagnostics than a compiler, because it executes the source program statement by statement. ( ) 2. The task of collecting the source program is sometimes entrusted to a separate program, called assembler. ( ) 3. One task of the lexical analyzer is correlating error messages generated by the compiler with the source program. ( ) 4. A lexeme is a sequence of characters in the source program that matches the pattern for a token. ( ) 5. A language that can be generated by a grammar is said to be a regular language. ( ) 6. FIRST( ), where is any string of grammar symbols, is the set of terminals that begin strings derived from. ( ) 7. A parse tree, showing the value(s) of its attribute(s) is called a syntax tree. ( ) 8. A dependency graph depicts the flow of information among the attribute instances in a particular parse tree; an edge from one attribute instance to another means that the value of the first is needed to compute the second. Edges express constraints implied by the semantic rules. ( ) 9. Static checking includes type checking, which ensures that operators are applied to compatible operands. ( ) 10. All assignments in static single-assignment form are to variables with distinct names. ( ) Page No 1 (Please look at the next page )
2) a. Write the algorithm for simulating DFA. Page No 2 (Please look at the next page )
2) b. Construct a NFA for the RE (a b) * abb. Page No 3 (Please look at the next page )
3) a. Apply eliminating left recursion algorithm to the following grammar: S A a b A A c S d Page No 4 (Please look at the next page )
3) b. Consider the non-left-recursive grammar E T E' E' + T E' T F T' T' * F T' F ( E ) id Compute FIRST(E), FIRST(E'), FIRST(T), FIRST(T'), FIRST(F), FOLLOW(E), FOLLOW(E'), FOLLOW(T), FOLLOW(T'), and FOLLOW(F). Page No 5 (Please look at the next page )
4) a. Construct the annotated parse tree for the input string 3 * 5 + 4 n, using the given grammar and rules: Page No 6 (Please look at the next page )
4) b. What are the common three-address instructions? Page No 7 (The end of questions) Good Luck Dr. Ahmed Al-Hussain Naghamish
Salman Bin Abdul Aziz University Collage of Computer Engineering and Sciences Computer Science Department 1) 1433-1434 (2012-2013) First Term Answers CS 4300 Compiler Construction 8 th Level Final Exam 120 Minutes 2) 1. T 2. F 3. T 4. T 5. F 6. T 7. F 8. T 9. T 10. T a. Simulating a DFA algorithm: this algorithm shows how to apply a DFA to a string. Input: A DFA D with start state s 0, accepting states F, and transition function move. An input string x terminated by an end-of-file character eof. Output: Answer ''yes" if D accepts x; "no" otherwise. Method: Apply the following algorithm to the input string x. s = s 0 ; c = nextchar(); while ( c!= eof ) { s = move(s, c); c = nextchar(); } if ( s is in F ) return "yes"; else return "no"; b.
3) a. We order the nonterminals S, A. There is no immediate left recursion among the S-productions, so nothing happens during the outer loop for i = 1. For i = 2, we substitute for S in A S d to obtain the following A- productions: A A c A a d b d Eliminating the immediate left recursion among these A-productions: A A c A a d b d yields the following grammar: S A a b A b d A A A c A a d A b. FIRST(F) = FIRST(T) = FIRST(E) = {(, id}. FIRST(E ) = {+, }. FIRST(T ) = {*, }. FOLLOW(E) = FOLLOW(E ) = {), $}. FOLLOW(T) = FOLLOW(T ) = {+, ), $}. FOLLOW(F) = {*, +, ), $}.
4) a. b. 1. Assignment instructions of the form x = y op z. 2. Assignments of the form x = op y. 3. Copy instructions of the form x = y. 4. An unconditional jump goto L. 5. Conditional jumps of the form if x goto L and iffalse x goto L. 6. Conditional jumps such as if x relop y goto L. 7. Procedure calls and returns are implemented using the following instructions: param x for parameters; call p, n and y = call p, n for procedure and function calls, respectively; and return y, where y, representing a returned value, is optional. Their typical use is as the sequence of three-address instructions param x 1 param x 2. param x n call p, n generated as part of a call of the procedure p(x 1, x 2,, x n ). 8. Indexed copy instructions of the form x = y[i] and x[i] = y. 9. Address and pointer assignments of the form x = &y, x = *y, and *x = y.