Examination Formal Languages and Automata Theory TDDD14 (Formella Språk och Automatateori) 2012–01–14, 08.00 – 12.00 1. Allowed help materials • A sheet of notes - 2 sided A5 or 1 sided A4. The contents is up to you. The notes should be signed in the same way as the exam sheets and returned together with the exam. • English dictionary Tillåtna hjälpmedel: • Ett papper med valfria anteckningar - 2 sidor A5 eller 1 sida A4. Anteckningarna ska signeras på samma sätt som tentamensarken och bifogas tentamen vid inlämnandet. • Engelsk ordbok 2. You may answer in Swedish or English. 3. Total number of credits is 32: 3: 16 p, 4: 21 p, 5: 26 p. 4. Jour (person on duty): Jonas Wallgren. GOOD LUCK ! 1 Make sure that you justify your answers! Unexplained answers will be granted 0 points. (For example, assume that you are writing a grammar for a given language. Then you should also explain that the grammar indeed generates the language). 1. (2p) Using a standard method construct a DFA equivalent to the NFA given by the table. (Its set of states is Q = {1, 2, 3}, the input alphabet Σ = {a, b, c}, the start state is 1, and 3 is the only final state.) ε a b c →1 {2, 3} ∅ {2} {3} ∅ {1} {3} {1, 2} 2 3F ∅ ∅ ∅ ∅ 2. (2p) (a) Consider regular expressions α = (a + b)(ab + ba)∗ ∗ and β = (ab)∗ (ba)∗ (a + b). Give an example of a string in L(α)\L(β), and of a string in L(β)\L(α), if such string exists. (If a required string does not exist, you should explain that this is indeed the case.) (b) The same for ∗ γ = b(ab + aab)∗ b and δ = ba + baa bb. Remember that \ denotes the set difference. 3. (2p) Consider the NFA given by the table. Using a standard method, construct a regular expression defining the same language. (The set of states of the NFA is Q = {A, B, C, D}, the input alphabet Σ = {0, 1, 2, 3}, the start state is A, and C is the only final state.) 0 1 2 3 →A {B, C} ∅ ∅ ∅ B ∅ {A} {D} ∅ CF ∅ ∅ ∅ {A} D {D} ∅ {C} ∅ 2 4. (2p) Using a standard method, construct the minimal DFA equivalent to the DFA given by the table. (Its set of states is Q = {A, B, C, D, E, F, G, H}, the input alphabet Σ = {0, 1}, the start state is A, and {C, F } is the set of final states. →A B CF D E FF G H 0 1 B C D E F A H F E F H H C B B C 5. (2p) Construct a context-free grammar for the language L = { am+n bm+k ck+j dl+n | j > l, m, n, k, l ≥ 0 } Make it clear what are the sets generated by the nonterminal symbols of your grammars, and what is the role of each production. 6. (6p) For each of the following languages answer whether it is regular, context-free but not regular, or not context-free. A brief, informal explanation is sufficient. (a,b) L1 is the language generated by the context-free grammar S → ASB | A → a | aa B→b|c| and L2 is generated by S → ASB | d A→a|b B → bB | cB | . In the grammars, S, A, B are nonterminal symbols, a, b, c, d are terminal symbols, and S is the start symbol. (c) L3 = { xei y | x ∈ {a, b}∗, y ∈ {c, d}∗, |x| > |y|, |x| < 2|y|, i ≥ 3 }, (d) L4 is the image of L3 under the homomorphism h : {a, b, c, d, e}∗ → {0, 1, 2}∗ such that h(a) = 01, h(b) = h(c) = , h(d) = 11, h(e) = 2. (e) L5 = { xei y | x ∈ {a, b}∗ , y ∈ {c, d}∗ , |x| > i, i < 2|y| }, 3 7. (1p) Choose a grammar from problem 6 (a,b) and explain whether it is ambiguous. Comment: If it turns out to be unambiguous then an informal explanation is sufficient. 8. (3p) Prove that a language L6 = { a(bj a)j | j > 0 } is not regular or that it is not context-free. Use the appropriate pumping lemma or employ reasoning similar to the proof of the lemma. 9. (2p) Explain briefly the notion of a universal Turing machine (UTM). What is the language accepted by it? To which class of languages does it belong? 10. (3p) We say that a state of a Turing machine M is useless if it is never reached in any computation of M . We say that a Turing machine M writes A on some input (where A is a symbol from the tape alphabet of M ) when M writes A on the tape at some step of some computation of M . (Assume that this includes writing A into a cell already containing A.) Show that the problem “a state q of a Turing machine M is useless” is undecidable. Use the fact that it is undecidable whether a given Turing machine M writes a given symbol A on some input. In other words, show that the language of the considered problem, this means Turing machine M , US = hM, qi does not reach state q on any input is not recursive, using the fact that the language Turing machine M WP = hM, Ai on some input writes A is not recursive. 11. (3p) Which of the following statements are true, which are false? Justify your answers. (a) There exists a homomorphism h which makes all the strings longer (this means that |x| < |h(x)| for each string x over the appropriate alphabet, where |w| is the length of string w). 4 (b) If L1 and L2 are a regular languages and L1 = L2 L3 then L3 is a regular language. (c) There exists an ambiguous context-free grammar which is LR(1). 12. (1p) Consider two context-free grammars S → Ac A → | aAb | b S → Ab A → | aAb | b (S is the start symbol of the grammar, a, b, c are the terminal symbols, and A is a nonterminal symbol.) One of them is LL(1) and the other is not. Explain which. 13. (3p) In an attempt to construct LR parsers for certain grammars, we applied the standard method of constructing a DFA for the viable prefixes of a grammar. Some fragments of the obtained DFA’s are given below. Complete the missing items in the given states, the missing lookahead sets and the missing symbols labelling the arrows. In each case answer the following questions. Justify your answers. • Does the fragment of a DFA satisfy the conditions for the grammar to be LR(0)? • The same question about the conditions for LR(1). You may skip adding missing items or lookahead sets if they are not needed to answer the questions. For instance if you find the items in some state to violate the LR(1) conditions then you do not need to complete the other states. a, b, (, ), + are terminal symbols and S, A are nonterminal symbols of the grammars; S is the start symbol. (a) S → • Ab { $ } S → •A {$} A a −→ ←− The productions of the grammar are S → Ab | A, A → aAb | a. 5 (b) | ↓ A → ( • A) { +, ) } A → ( • A+A) { +, ) } ←− −→ y The productions of the grammar are A → b | (A) | (A + A) and S → A. 6