

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


S โ aSbS | bSaS |
Answer: The set of all strings with an equal number of aโs and bโs.
a) Give the grammar that generates the language of the regular expression: 01(0 | 1) Answer:
S โ A 1 B A โ 0 A | B โ 0 B | 1 B |
b) Give the leftmost and rightmost derivations of the following strings using the grammar from part a):
i) 1001 Answer: Leftmost: S โ A 1 B โ 1 B โ 10 B โ 100 B โ 1001 B โ 1001 Rightmost: S โ A 1 B โ A 10 B โ A 100 B โ A 1001 B โ A 1001 โ 1001
ii) 00011 Answer: Leftmost: S โ A 1 B โ 0 A 1 B โ 00 A 1 B โ 000 A 1 B โ 0001 B โ 00011 B โ 00011 Rightmost: S โ A 1 B โ A 11 B โ A 11 โ A 011 โ A 0011 โ A 00011 โ 00011
c) Give the parse trees for the strings in part b).
i) Answer:
ii) Answer:
Note: The important productions rules here are for the symbols R and T. R enforces the constraint i 6 = j and T enforces the constraint j 6 = k. The start symbol just ensures that one or the other must be true for the string to be in the language of this grammar.
a) Is this grammar ambiguous? Why? Answer: Yes. The string aab has two parse trees, leftmost derivations, and rightmost derivations.
b) Redesign the grammar to be unambiguous. Answer: S โ aSb | A A โ Aa |
Note: This is the grammar for the language ambn^ | m โฅ n
S โ aA | B A โ aaB B โ bB | a