Solutions Set 2
Solutions Set 2
1. NPDAs
a. {1n0n | n>0}
b. {0n12n | n>=0}
2. DPDAs
d. Binary strings that start and end with the same symbol and have the same
number of 0s as 1s.
3. CFGs
S -> 0A | 1
A -> 0S | 1S | e
S -> AC | BC | DE | DF
A -> 0 | 0A | 0A1
B -> 1 | B1 | 0B1
C -> 2 | 2C
D -> 0 | 0D
E -> 1 | 1E | 1E2
F -> 2 | F2 | 1F2
4. Ambiguity
a. Explain why the grammar below is ambiguous.
S -> 0A | 1B
A -> 0AA | 1S | 1
B -> 1BB | 0S | 0
S S
0A 0A
0 0AA 0 0AA
00 1S 1 00 1 1S
001 1B 1 0011 0A
0011 0 1 00110 1
b. (extra credit)
c. Demonstrate that G (see problem set) is ambiguous.
d. (extra credit)
5. Converting to Normal Form
a. Put the following grammar into Chomsky Normal Form.
S -> e | A0 | 0 | AS1 | B0 | A1 | 1A
A -> A0 | 0
B -> B1 | BC
C -> CB | CA | 1B
S1 -> B0 | 1A
NOTE: We did not need to create a new start state because the given one
did not appear in the right side of any rule.
S -> A | CB
A -> C | D
B -> 1B | 1
C -> 0C | 0
D -> 2D | 2
Converts to
S -> 0C | 0 | 2D | 2 | CB
A -> C | D
B -> 1B | 1
C -> 0C | 0
D -> 2D | 2
We begin with a single nonterminal ("S") and form the string by making
substitutions according to the rules. Each rule of the form A->BC adds a
new nonterminal, and each rule of the form A->a converts one
nonterminal into a terminal. Since it takes n-1 steps to grow our string
from the original nonterminal to n nonterminals, and then n steps to
convert each of those nonterminals into terminals, it takes 2n-1 steps to
derive a string of length n.
This 2-way PDA works by moving right across the string to make sure it begins
with 0n1n. Then it moves left to the beginning of the 1s and continues to the right
to check for 1n0n.