Introduction to Compiler Design Midterm Examination Solution Name: _____________ Reg-No: __Solution_____ 1. (20 pts) [CFG ] Consider the grammar and the string: 5/9 2007 G: score: _____________ SÆSS+| SS# | a a a + a # (a) Give a rightmost derivation for the string. (5pts) Ans: S ÆSS# ÆSa# Æ SS+a# Æ Sa+a# Æaa+a#. (b) Give a parse tree for the string. Ans: S( S( (5 pts) S(a), S(a), + ), S(a), # ). Note: we use S(t1,t2,,,,tn) to represent a tree with root S and subtrees t1,t2,…,tn. (c) Is the grammar ambiguous or unambiguous ? why? (5pts) Ans: No, since it can be shown that for every input, there is at most one parse tree for it. (d) Describe the language generated by this grammar? ( 5 pts) Ans: It is the set of the postfix forms of all expressions which are composable by two binary operators (+ and #) and one constant a. 2. [5 pts] Consider the grammar : S Æ S a | a Which class of grammar does this grammar belong to? (a) LL(1) (b) LL(2) (c) LR(0) (d) LR(1) (e) LALR(1) Ans: ____cde______ 3. (30 pts) [LL(1) parsing] Given the following grammar G: S ÆE$ E Æ T E' T Æ F T' T' Æ * F T' E' Æ + T E' | ε F Æ (E) (a) Find all nonterminals in G that are nullable. (4pts) 1 | |d ε Ans: ______________T',E'__________ (b) Fill in the following table with FIRST and FOLLOW sets for the grammar: (10 pts) N FIRST(N) FOLLOW(N) S (,d {} E (,d ),$ E' + ),$ T (,d +,),$ T' * +,),$ F (, d *,+,),$ (c) Complete the following LL(1) table for the grammar: N\Σ (10 pts) d + * ( ) $ S E$ err err E$ err err E TE' err err TE' err err E' err +TE' err err ε ε T FT' err err FT' err err T' err ε *FT' err ε ε F FÆ d error error FÆ(E) error error (d) Is this grammar LL(1) ? why? (4 pts) Ans: __Yes, since there is at most one rule in every cell of the LL(1) table. ___________________________________________________________________ 4. (10pts) Let G = (N,Σ, S,P) be a CFG where N is the set of non-terminal symbols, S is the set of terminal symbols and P the set of production rules. Define the relation First ⊆ N x Σ as the set of all pairs (A,a) such that a can appear in the first position of some sentential forms derived from A, i.e., First = { (A, a) ∈ N X Σ | A Æ* a β for some β ∈ (N U Σ)* }. The relation First can be defined inductively as follows: (a) Basis: (A, a) ∈ First for all A Æ B1B2…Bn a … ∈ P, where n ≥ 0 and B1,…,Bn are all nullable. (b) Closure: If (C,c)∈First and B1,..,Bn are nullable and A ÆB1B2…Bn C… ∈ P, then (A,c) ∈First . Now let Last = { (A, a) ∈ N X Σ | ∃β∈(N U Σ)* 2 s.t. A Æ* β a }. List an inductive definition for the relation Last : (a) Basis: If there exists A Æ … a B1,…,Bn∈ P where n ≥ 0 and each Bk ( 1≤ k ≤ n ) is nullable, then (A,a) ∈ Last. (b) Closure: If there is a rule A Æ … C B1,…,Bn∈ P where n ≥ 0 and each Bk ( 1≤ k ≤ n ) is nullable, and (C,d) ∈ Last, then (A,d) ∈ Last. 5. [10 pts] Write regular expressions for the following languages: (a) All strings over {a,b} containing at least 2 b's. Ans: (a + b)* b (a + b)* b (a + b)* (b) Decimal number 0 ~ 255. Note that all leading zero's except 0 are not accepted.. (4pts) Ans: (0 + 1 +…+ 9) // 0 ~ 9 + (1 +…+ 9) (0 + 1 +…+ 9) // 10 ~99 + 1(0+1 +…+ 9) (0 + 1 +…+ 9) // 100 ~199 + 2(0+1 +…+ 4) (0 + 1 +…+ 9) // 200~249 + 25(0+1 +…+ 5) // 250 ~255 (c) Binary numbers that are greater than 101. Note. leading zero's are permitted. Ans: 0* (100 + 101) (1+0)+ + 0* (110 + 111) ( 1 + 0)* 6. [20 pts ; minimization of FA] For the finite automata F given below, (a) Complete the following chart to find all pairs of states that cannot be merged without affecting the accepted language. [12pts] 3 1 2 x1 3 x0 x0 4 x1 x1 x0 5 - x1 x0 x1 6 x1 x1 x0 - x1 7 x2 x1 x0 x1 x2 x1 8 x1 - x0 x1 x1 x1 x1 1 2 3 4 5 6 7 8 2 ≈8, 4 ≈6 , 1 ≈5 (b) Draw the minimized finite automata according the result of (a). 0 [15] 0 [[3]] 1 1 [28] 1 1 [46] 0 0 1 [7] 0 7. (15 pts) [From Regular Expression to DFA] Consider the regular expression α given below: (a+b)* ba(a+b) (a) Write an NFA which recognizes the language represented by α and contains no more than 4 states. Remember to label each state. [5 pts] (b) Convert the NFA you got at (a) to an equivalent DFA by using the subset construction. Note: you must identify each state of your DFA with the set of old NFA states it corresponds to and make sure all unused states are removed. [10pts] 4 (a) : Ans : a,b 0 b a 1 a,b 2 (b) : Ans : 3 a a b 0 b b 01 b a 02 b a a 013 8. (25 pts)[LR parsing] Consider the grammar 0. S Æ E$ 1. E Æ E E + 2. E Æ E E ! (a) Build the LR(0) NFA for this grammar [5pts] 5 3. E Æ a 03 E SÆy E$ 1 ε ε E EÆy EE+ $ SÆEy$ EÆ EyE+ E SÆE$y + EÆ EEy+ 2 E EÆy EE! ε 3 a EÆy a EÆ EyE! E EÆ a y ε ! EÆ EEy! EÆ EE+y EÆ EE! y ε to 1,2 and 3 (b) Build the LR(0) DFA for this grammar [10pts] SÆy E$ (1)EÆy EE+ (2)EÆy EE! (3)EÆy a a E SÆEy$ +123 (4) EÆ EyE+ (5) EÆ EyE! a SÆE$y EÆ EE+y E a EÆ a y $ + ! 12345+ (6) EÆ EEy+ (7) EÆ EEy! E a (c) Is this an LR(0) grammar ? Give evidence. [5pts] Yes, because there is no s/r or r/r conflict in the LR(0) parsing table. 6 EÆ EE! y (d) Is this an SLR(1) grammar ? Give evidence. [5pts] Yes, since it is already an LR(0) grammar and every LR(0) grammar is a SLR(1) grammar. 7