Examination Formal Languages and Automata Theory TDDD14 (Formella Språk och Automatateori) 2012–10–24, 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 31: 3: 16 p, 4: 21 p, 5: 26 p. 4. Jour (person on duty): Wlodek Drabent, tel. (013 28) 89 29. 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) Consider the NFA whose transition function is given by the table. (Its set of states is Q = {A, B, C, D, E, F }, the input alphabet Σ = {0, 1}, the start state is A and the final state are D, F .) Using a standard method construct an equivalent DFA. 1 2 1 →A {B, E} ∅ ∅ ∅ {B, C} {B} B C ∅ {D} {D} ∅ ∅ ∅ DF E ∅ {E} {E, F } ∅ ∅ ∅ FF 2. (2p) 0 0 3 → A F {B} {C} {D} {A} B C {B} {A} D {C} 3. (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}, the input alphabet Σ = {0, 1}, the start state is A, and {F, G} is the set of final states.) Using a standard method, construct a regular expression defining the same language as the given NFA. (Its set of states is Q = {A, B, C, D}, the input alphabet Σ = {0, 1, 2, 3}, the start state and the final state is A; the missing positions of the table stand for ∅.) →A B C D E FF GF 0 1 B C D E C B F F G G G G G G 4. (2p) Let L be the language accepted by the DFA from the previous problem. Consider the relation ≡L on strings over Σ = {0, 1}, defined by x ≡L y ⇔ ∀z ∈ Σ∗ (xz ∈ L ⇔ yz ∈ L). How many equivalence classes does ≡L have? Why? Choose one of the equivalence classes, and give a DFA defining it. 2 5. (1p) Construct an ambiguous context-free grammar. The grammar should have at least two nonterminal symbols, and neither -productions nor unit productions. 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. Remember that wR denotes the string w reversed, and #a(w) denotes the number of occurrences of symbol a in w. (a) L1 = ∗ #a(w) is even, #b(w) ≡ 1 mod 3 w ∈ {a, b, c, d} . w does not have a substring abc (b) L2 = { w ∈ {a, b, c, d}∗ | #a(w) is even, #b(w) < #c(w)} . (c) L3 = { w ∈ {a, b, c, d}∗ | #a(w) < #b(w) < #c(w)} . (d) L4 = { w ∈ {a, b, c, d}∗ | #a(w) < #b(w) < #c(w) < 6 } . (e) L5 = { wR x ∈ {a, b, c, d}∗ | w = 2(x) }, where 2(a1 a2 · · · an ) = a1 a1 a2 a2 · · · an an , for any n ≥ 0 and a1 , a2 , . . . , an ∈ {a, b, c, d}∗ (i.e. each symbol ai is replaced by ai ai ). 7. (5p) One of the languages L6 = { a p b q+r c s d q+t e p+r | p, q, r, t ≥ 0, s > t }, L7 = { a p+q b r c s d q+r e s | p, q, r, s ≥ 0 } is context-free and the other is not. Build a context-free grammar for the one that is. For the other one provide a proof that it is not regular, or that it is not context-free. For the grammar, explain the set generated from each its nonterminal symbol, and the role of each production. For the proof, use the appropriate pumping lemma or employ reasoning similar to the proof of the lemma. Hints: Begin from finding the context-free language. Confirm that it is context-free by constructing a grammar. In the proof it is useful to find a simple string to pump. 8. (3p) Which of the following statements are true, which are false? Justify your answers. (a) Let NFA M 0 be obtained from an NFA M by swapping its final and non-final states: M = (Q, Σ, ∆, S, F ) and M 0 = (Q, Σ, ∆, S, Q\F ). Then L(M 0 ) = Σ∗ \ L(M ). 3 (b) There exists a language L such that for every homomorphism h the language h(L) is not regular. (c) If languages L1 , L2 are recursively enumerable then LR 1 ∩ L2 is recursively enumerable. 9. (1p) Explain briefly the notion of the language of a decision problem. Hint: The notion is used below. 10. (3p) Show that the problem “a Turing machine M rejects string x” is undecidable. Use the fact that the halting problem is undecidable (i.e. it is undecidable whether M halts on input x). In other words, show that the language of the problem RP = { hM, xi | Turing machine M rejects input x } is not recursive, using the fact that the language HP = { hM, xi | Turing machine M halts on input x }. is not recursive. 11. (1p) Consider a grammar with productions S → aB | bA | A → aS | bAA B→b (where a, b are terminal symbols, and S is the start symbol). Explain why it is not LL(1). 12. (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). 4 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 the terminal symbols and S, A, B, C are the nonterminal symbols of the grammars; S is the start symbol. (a) | ↓ C → b•A {$} A b ←− −→ The productions of the grammar, starting with C and A are C → aB | bA, A → aC | bAA (b) A → b • AA { a, b } −→ a a −→ −→ The productions of the grammar, starting with C and A are C → | aB | bA, A → aC | bAA. 5