5.1.7(e). L = {w \in {a,b}* : n_a(w) =/= n_b(w)}. This is a difficult problem. We will approach it by decomposing the language until we get pieces that we can cope with. Let L1 = {w \in {a,b}* : n_a(w) > n_b(w)}, and L2 = {w \in {a,b}* : n_b(w) > n_a(w)}. Then L = L1 union L2. The strings in L1 are of the following format: aa...a w1 aaa..a w2 aaaa... w3 aaaa.. where w1, w2, etc. have equal number a's and b's and the intervening strings of a's have at least one a. Let E generate strings of equal number of a's and b's. E -> aEb | bEa | EE | \lambda a+ will be generated by: A -> aA | a L1 will be generated by: C -> ED D -> AED | AE This can generate E (AE)* AE. Since E can generate lambda, this generates all sequences of the form aa...a w1 aaa..a w2 aaaa... w3 aaaa, both beginning and ending with a's and w's, such that there is at least one extra a. Similarly, b+ will be generated by: B -> bB | b L2 will be generated by: F -> EG G -> BEG | BE This generates E (BE)* BE, that generates all strings of the form bbbb ..b w1 bbb..bb w2 bb..bb w3 bb..bb, both beginning and ending with b's and w's (strings of equal number of a's and b's). Since L is the union of L1 and L2, we have a rule S -> F | C, where S is the start symbol of the grammar. ************************************ 5.1.8(d): L = {a^n b^m c^k: n+2m = k} which is the same as, L = {a^n b^m (cc)^m c^n : n,m >= 0} The following rules generate {b^m(cc)^m : m >= 0} B -> bBcc | lambda The following rules generate {a^n B c^n: n >= 0} S -> aSc | B All the rules together generate L. *********************************** 5.1.8(g): L = {a^n b^m c^k: k =/= n+m} Let L3 = {a^n b^m c^k: k = n+m} L = L1 union L2, where, L1 = {a^n b^m c^k: k > n+m} and L2 = {a^n b^m c^k: k < n+m} Note that L3 = {a^n b^m c^m c^n: n,m >= 0} L3 is generated by the following grammar: P -> aPc | Q Q -> bQc | lambda L1 is generated by adding one or more c's at the end of strings of L3. R -> Pc | Rc L2 is generated by either having extra a's or b's or both in strings of L3. So, we have: S -> aSc | aS | aP | T T -> bT | bQ Since L is the union of L1 and L2, we need the following rule with the start symbol U U -> S | R ********************************************* 5.1.19: Give a derivation tree for w=aabbbb from the grammar S->AB|lambda A->aB B->Sb S / \ / \ / \ A B / \ / \ a B S b / \ | / \ lambda S b / \ / \ A B / \ / \ a B S b / \ | S b lambda | lambda ********************************************* 5.1.24: We know that the productions rules of the grammar must all start with a variable, then be followed by an arrow, and then be followed by a string of terminals and nonterminals. If we were to write the language of such rules/strings as a regular expression we would get, (A + B + C)'->'(A + B + C + a + b)* where '->' denotes an alphabet character corresponding to the arrow. A grammar for this language can be given as follows: S -> V '->' R V -> A | B | C R -> aR | bR | lambda where again '->' is a terminal character of the grammar that represents the arrorow. ******************************************** 5.2.3: Find an s-grammar for L={a^n b^{n+1} : n>=2}. First consider the language L1={a^n b^{n+1} : n>=1}. L1 can be generated by variable X using the following s-grammar X->aXB | b B->b We will build the grammar from L from the one for L1 by adding an extra a and extra b to the strings generated by X. S->aXB X->aXB | b B->b This is still an s-grammar and generates L. 5.2.7: An easy way to make it unambiguous is to just remove S -> aaB from the productions. It does not change the set of strings in the language, because we can still derive aaB from S as follows: S => AB => AaB => aaB Now there is no possibility of ambiguity. We should always use S -> AB as the first rule. If there are n a's in the string, then we should use A -> Aa, the first n-1 times and then use A -> a once. B can only go to b, so there is no possibility of ambiguity there. 5.2.12: Show that the language L={ww^R : w \in {a,b}*} is not inherently ambiguous. To show this we simply need to argue that there exists an unambiguous grammar for L. This can be done by showing an example of such a grammar. Consider the grammar G given by, S->aSa S->bSb S->lambda Below I give a formal proof that this grammar is unambiguous. An imformal argument suffices. Basically, you can argue that for any given string ww^R generated by the grammar that there is only one sequence of rule applications that can produce it since there is only one rule that can add b's and only one rule that can add a's. More formally, let Ln = {ww^R : w \in {a,b}^n}, that is Ln is the set of strings in L that have length 2n. We prove that G is unambigous by induction on n. That is we want to show that for all n>=0, there is only a single parse tree for all strings in Ln. Base Case: (n=0) In this case L0={lambda}. Clearly there is only a single parse tree for the string lambda, mainly S | lambda So the base case is proven. Inductive Case: Assume that for all k aSbS | bSaS | lambda Consider the string w = abab. This string can be derived via the following derivation S => aSbS => abSaSbS => abaSbS => ababS => abab which corresponds to the derivation tree S | ------------------- | | | | a S b S | | ------------- lambda | | | | b S a S | | lambda lambda Also w can be derived by the following derivation and derivation tree S => aSbS => aSbaSbS => abaSbS => ababS => abab S | ------------------- | | | | a S b S | | lambda ------------- | | | | a S b S | | lambda lambda Since the two trees are different this shows that the grammar is ambiguous. Note that the fact that the derivations are different does not necessarily mean the grammar is ambiguous, since different orders of applying rules can correspond to the same trees. Thus, it is important to show TWO DISTINCT TRUE when proving that a grammar is ambiguous.