Download presentation
Presentation is loading. Please wait.
1
Key Answers for Homework #7
1. This problem is to clean up context-free grammar G shown below. Using the two algorithms presented in the class, we are going to eliminate all useless symbols from the following context-free grammar G. Answer the following two questions. (a) Using the algorithm in Lemma 1 for grammar G construct a context-free grammar G’ such that L(G) = L(G’) and every nonterminal symbol in G’ derives a string x (VT)*, where VT is set of terminal symbols of G. (b) Using the algorithm in Lemma 2 for grammar G’, the result of part (a), and construct a context-free grammar G’’ such that L(G) = L(G’’), and for each symbol X (which is either terminal or nonterminal symbol) in G’’, starting with the start symbol, it is possible to derive a string containing X. Your answer should include detailed procedure (steps of the algorithm) that you took to collect the information needed to get your answer. No grade will be given to an answer that only shows the final result. G: S BA | CDg B aEA A DEd C Ce D Df | E Eg | g F hF | h G cGCc I cDEFab Answer: (a) OLDV = {}; NEWV = {D, E, F} OLDV = NEWV; NEWV = OLDV {A, I} = {A, D, E, F, I}; OLDV = NEWV; NEWV = OLDV {B} = {A, B, D, E, F, I}; OLDV = NEWV; NEWV = OLDV {S} = {A, B, D, E, F, I, S}; OLDV = NEWV; NEWV = OLDV { } = {A, B, D, E, F, I, S}; V´N = NEWV = {A, B, D, E, F, I, S}; // Set of nonterminal symbols which derive a string x (VT)* G’: S BA B aEA A DEd D Df | E Eg | g F hF | h I cDEFab
2
G’: S BA B aEA A DEd D Df |
E Eg | g F hF | h I cDEFab Answer (b): Constructing a context-free grammar G’’ such that L(G) = L(G’’), and for each symbol X (which is either terminal or nonterminal symbol) in G’’, starting with the start symbol, it is possible to derive a string containing X. V´T = V´N = {}; // initialize with empty set V´N = V´N {S} V´T = V´T {} = {} V´N = V´N {A, B} = {S, A, B} V´T = V´T {} = {} V´N = V´N {D, E} = {S, A, B, D, E} V´T = V´T {a, d} ={a, d} V´N = V´N {} = {S, A, B, D, E} V´T = V´T {f, g, } ={a, d, f, g, } V´N = V´N {} = {S, A, B, D, E} V´T = V´T {} ={a, d, f, g, } G’’: S BA B aEA A DEd D Df | E Eg | g
3
2. Covert the following context-free grammar into a Chomsky Normal Form.
S abABCde A aAa B ba C cab Answer: Given rule Converted rules S abABCde S S1S S1 a S2 S3S S3 b S4 AS S5 BS6 S6 CS S7 S8S S8 d S9 e A aAa A A1A A1 a A2 AA A3 a B ba B B1B B1 b B2 a C cab C C1C C1 c C2 C3C C2 a C2 b
4
3. (a) Show that the following grammar is ambiguous.
S S + S | S * S | T T a | b Answer: (a) For string a + b * a, we have the following two parse trees. S + T a b * (b) Let + be the addition and * be the multiplication. Then the above grammar generates mathematical expressions with the two operators on two variable a and b. Convert the above grammar to an unambiguous context-free grammar such that it generates the same language and * has higher precedence than +. Answer: S S + A | A A A * T | T T a | b * Notice that this grammar restricts such that multiplications are done as a term of an addition, if any. (c) Convert the above grammar to an unambiguous context-free grammar such that - it generates the same language, - the two operators * and + have the same precedence, and - the evaluation is done right to left. Answer: S T + S | T * S | T T a | b * Notice that all the operators in a string are generated left to right according to the order of appearance.
5
4. The following syntax flow graph is a simplified definition of the Pascal’s syntax flow graph for < IF statement>. Assume that all the strings in a circle are terminal symbols. <IF-statement> IF ( <bool> ) then else <statement> c d a + b a - b (a) Convert this syntax flow graph for <If-statement> to a context-free grammar that generate the same language, and show that the grammar is ambiguous. Answer: In the following grammar, <…> denotes nonterminals, and others are terminals. <IF-statement> is starting nonterminal. <IF-statement> IF(<bool>)then<statement> | IF(<bool>)then<statement>else<statement> <statement> a – b | <IF-statement> | a + b <bool> c | d This grammar is ambiguous because there are two parse trees for string w = IF(c) then IF (d) then a + b else a - b. (Ignore blanks in the string. They are inserted for readability.)
6
<IF-statement> IF(<bool>)then<statement> | IF(<bool>)then<statement>else<statement> <statement> a – b | <IF-statement> | a + b <bool> c | d For simplicity, lets transform the above grammar as shown below with the nonterminals and terminals substituted as follows; <IF-statement> = S <bool> = B <statement> = A IF = i then = t else = e a + b = p a – b = m S i(B)tA | i(B)tAeA A m | S | p B c | d Then, for string i(c)ti(d)tpem (which is IF (c) then IF (d) then a + b else a – b according to the original grammar) has two parse trees as follows. i ( B ) t A d p S i ( B ) t A e A m c S i ( B ) t A c i ( B ) t A e A p m d
7
(b) Convert your grammar to an unambiguous grammar that generates the same if statements. You should briefly describe your idea. Answer: We change the grammar such that else part, which is e, is always generated together with the nearest if part ( i in the grammar) located to the left. (Notice that S has recursion at the right end of its production.) S i ( B ) t S c i ( B ) t A e A p m d S i(B)tS | i(B)tAeS | i(B)tA | i(B)tAeA A p | m B c | d String i(c)ti(d)tpem can only be generated as shown to the right.
8
5. Using the pumping lemma we want to prove that language L = {xxR | x {a, b, c}* } is not regular as follows. Complete the proof by answering the questions. Proof. Suppose that L is regular. Then L should satisfy the pumping lemma. Let n be the constant of the lemma. Clearly, all the following strings belong to L. (a) abccba (b) a100b100b100a (c ) an/2bn/2b n/2an/ (d) anbnbnan Question 1: Which string will you choose for z of the lemma? Why the other strings cannot be a candidate for z? If none of the above strings can be a candidate for z, what is your choice from L? Answer: Strings in (a) and (b) cannot be a candidate for z because their length is fixed. For n greater than the length of these strings, the lemma is not applicable. Strings in (c ) and (d) are fine, because their length is not less than n. I will choose the string in (d), since there will be smaller number of cases to be analyzed than the string in (c ). Question 2: Let u, v, w {a, b, c}* be the strings of the lemma such that z = uvw. What will be in v? Shortly describe all possible contents of string v. Answer: Since |uv| n, and |v| 1, string v contains some a’s in the prefix an of the string such that 1 |v| n. Question 3: The pumping lemma says “For all i 0, string uviw L.” How do you deal with this statement for your proof? Justify what you do. Answer: Since this statement uses universal quantifier for i, it is enough to show one value of i for which the statement is wrong. Thus, we choose i = 2. Clearly, uv2w L, since uv2w xxR for any x {a, b, c}*
9
6. Which of the following language are regular. Prove your answer
6. Which of the following language are regular. Prove your answer. Your answer will be graded according to the logical completeness. L1 = {aibjak | i, j, k > 0 } L2 = {aibjai | i, j > 0 } Answer: Since aa*bb*aa* is a regular expression which denotes L1, it is regular. (Also you can show that L1 is regular by showing a regular grammar or FA whose language is L1. Language L2 is not regular. We use the pumping lemma for the proof. Suppose that L2 is regular. Then it should satisfy the pumping lemma for regular languages. Let n be the constant of the lemma, and let z = anban, which is in the language L2. Since | z | n, according to the pumping lemma there exist u, v, w such that (i) z = uvw, (ii) |uv| n, (iii) |v| 1, and (iv) for all i 0, z’ L2 By conditions (i) – (iii) above, v contains only a’s which belong to the prefix an of z, and 1 |v| n. It follows that uv2w L2 . Thus language L2 does not satisfy the pumping lemma. We are in contradiction. L2 is not regular.
10
7. For a string x, let #a(x), #b(x), and #c(x) denote, respectively, the number of a’s, number of b’s and number of c’s in string x. Which of the following languages are context-free? Prove your answer. Your answer will be graded according to the logical completeness. L3 = { x | x {a,b,c}+, and #a(x) > #c(x) } L4 = { x | x {a,b,c}+, and #a(x) = #b(x) = #c(x) } L5 = {x | x {a,b,c}+, and #a(x) > #b(x) > #c(x) } Answer: L3 is context-free language because we can construct a PDA which recognizes this language as follows, where X {a, c, A, Z0}. (b, Z0/Z0) (a, Z0/AZ0) (a, A/aA), (a, a/aa), (c, a/ ), (b, X/X) (c, A/) (c, Z0/cZ0), (c, c/cc), (a, c/ ), (b, X/X) start (c, Z0/cZ0)
11
We prove that language L4 = { x | x {a,b,c}+, and #a(x) = #b(x) = #c(x) } is not context-free. Suppose it is. Then the language should satisfy the pumping lemma for context-free languages. Let p be the constant of the lemma. Choose z = apbpcp. Then we can apply the same argument that we used for proving that {aibici | i 0 }is not context-free, and show that z’ L4. This language does not satisfy the pumping lemma. We are in contradiction. It is not context-free. We prove that language L5 = {x | x {a,b,c}+, and #a(x) > #b(x) > #c(x) } is not context-free using the pumping lemma for context-free languages. Suppose it is, and let z = ap+2bp+1cp. Since |vwx| p and |vx| 1, v and x together contain at most two different symbols among a, b and c (see the illustration below). We have following two cases. When v and x contain a’s or b’s: clearly z’ = uv0wx0y L5 When v and x contains b’s or c’s: obviously z’ = uv2wx2y L5 L5 does not satisfy the pumping lemma. We are in contradiction. The language is not context-free. z = aaa abb bcc c P p p u v w x y p
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.