CSC 460/660, Midterm March 14, 2005 Name: Student ID: Signature: You have 75 minutes to write the 7 questions on this examination. A total of 100 marks is available. Justify all of your answers You may use one page of handwritten notes. Keep your answers short. If you run out of space for a question, you have written too much. The number in square brackets to the right of the question number indicates the number of marks allocated for that question. Use these to help you determine how much time you should spend on each question. Question Marks 1 /20 2 /16 3 /7 4 /10 5 /16 6 /7 7 /24 Total /100 Use the back of the page for rough work. Good luck UNIVERSITY REGULATIONS: No candidate shall be permitted to enter the examination room after the expiration of one half hour, or to leave during the first half hour of the examination. CAUTION: Candidates guilty of any of the following, or similar, dishonest practices shall be immediately dismissed form the examination and shall be liable to disciplinary action. 1. Making use of any books, papers or memoranda, electronic equipment, or other memory aid devices, other than those authorized by the examiners. 2. Speaking or communicating with other candidates. 3. Purposely exposing written papers to the view of other candidates. The plea of accident or forgetfulness shall not be received. 1
FIRST and FOLLOW Sets Question 1 [20 points] Consider the following context-free grammar: A B C x ɛ B C y A C w z B [6/20] Compute F IRST (A), F IRST (B), F IRST (C) [9/20] Compute F OLLOW (A), F OLLOW (B), F OLLOW (C) (Question 1 continued on next page) 2
FIRST and FOLLOW Sets [5/20] Why is this grammar not LL(1)? (End of question 1) 3
Recursive Descent Parsing Question 2 [16 points] Consider the following context free grammar: A B C D B up C down C ɛ C open close D end Assume the following variables and functions already exist: token is a variable that contains the look-ahead symbol as a string. EOF is a predefined string variable that matches end-of-file. accept() is a function that accepts the next token on the input and updates token to be the next lookahead. error() is a function to signal that something went wrong and parsing has stopped. Write a recursive descent parser for this language using Java-like syntax (you can use == to compare strings). (End of question 2) 4
Lexical analyzers Question 3 [7 points] Recall the dangeling else problem exhibited by this grammar: If Stat if Expr then Statement if Expr then Statement else Statement Briefly explain why adding a fi terminal like this: If Stat if Expr then Statement fi if Expr then Statement else Statement fi resolves the problem of the dangeling else problem (End of question 3) 5
Left Factorization and Common Sub-Expression Elimination Question 4 [10 points] Consider the following context-free grammar G: A A a A c b A B B B a B a c B d G is clearly not parsable by recursive descent because of left-recursion and common sub-expressions. [5/10] Eliminate the left recursion of G, and show the resulting grammar. [5/10] Eliminate the common sub-expressions from the result you got by eleminitating the left-recursion and show the resulting grammar. (End of question 4) 6
Question 5 [16 points] Consider the following context-free grammar: E T E E + T E ɛ T F T T F T ɛ F ( E ) id Derivations and Parse Trees [4/16] Give a left-most derivation of the string id + id * id. [4/16] Give a right-most derivation of the string id + id * id. (Question 5 continued on next page) 7
Derivations and Parse Trees [4/16] Are the parse trees for the left-most and the right-most derivations the same? if yes, draw the tree, if no draw the two different trees. [4/16] Briefly explain why both derivations will always be of the same length if they produce the same syntax tree. (End of question 5) 8
Predictive Parsable? Question 6 [7 points] Consider one production for a context-free language G: A α 1 α 2... α n What must be satisfied with respect to the above production for G to be parsable using a predictive parser without backtracking? (End of question 6) 9
LL(1) parsers Question 7 [24 points] Consider the following grammar: E P ( E ) v T P f ɛ T + E ɛ We can compute the following F IRST and F OLLOW sets: X F IRST (X) F OLLOW (X) E {f, v, (} {$, )} P {f, ɛ} {(} T {+, ɛ} {$, )} P ( E ) {f, ɛ} N/A v T {v} N/A f {f} N/A + E {+} N/A ɛ {ɛ} N/A [16/24] Fill in the following LL(1) parse table: ( ) f + v $ E P T (Question 7 continued on next page) 10
LL(1) parsers [8/24] Show, using your parsing table, how a table driven parser would parse the string f(v). Stack Input Production $E f ( v ) (End of question 7) (End of the exam) 11