Homework Assignment 4 Solutions
CSCI 2670 Introduction to Theory of Computing, Fall 2016
Due Tuesday October 18, 2016
This homework assignment is about context-free grammars, Chomsky nor-mal form, and pushdown automata.
1. Design a context-free (or regular) grammar to describe the language recognized bythe following NFA.
Give a derivation and parsing tree for string aababa.
Solutions:
• The regular grammar for the language recognized by the NFA is:
A1 → A3 | bA2 | ε
A2 → aA2 | aA3 | bA3
A3 → aA1
• The derivation of string aababa is:
A1 ⇒ A3 ⇒ aA1 ⇒ aA3 ⇒ aaA1 ⇒ aabA2
⇒ aabaA2 ⇒ aababA3 ⇒ aababaA1 ⇒ aababaε = aababa
Draw parsing tree (in the classroom):
1
2. Use the grammar rules given in Question 2.1 (page 154), give parsing trees for thefollowing strings
(1) (a+ a)× (a× a)(2) ((a)× a) + a
Solutions:
• The grammar is:
E → E + T |T
T → T × F |F
F → (E) | a
(1) the left-most derivation of string (a+ a)× (a× a):
E ⇒ T ⇒ T × F ⇒ F × F ⇒ (E)× F ⇒ (E + T )× F ⇒ (T + F )× F
⇒ (F + F )× F ⇒ (a+ F )× F ⇒ (a+ a)× F ⇒ (a+ a)× (E)⇒ (a+ a)× (T )
⇒ (a+ a)× (T × F )⇒ (a+ a)× (F × F )⇒ (a+ a)× (a× F )⇒ (a+ a)× (a× a)
Draw the parsing tree (in the classroom);
(2) the left-most derivation of string ((a)× a) + a:
E ⇒ E + T ⇒ T + T ⇒ F + T ⇒ (E) + T ⇒ (T ) + T ⇒ (T × F ) + T
⇒ (F × F ) + T ⇒ ((E)× F ) + T ⇒ ((T )× F ) + T ⇒ ((F )× F ) + T
⇒ ((a)× F ) + T ⇒ ((a)× a) + T ⇒ ((a)× a) + F ⇒ ((a)× a) + a
Draw the parsing tree (in the classroom).
3. Question 2.4 (c) and (e) on page 155.
Solutions:
(c) L = {w |w starts and ends with the same symbol }:
S → 0M0 | 1M1 | ε
A→ 0M | 1M | ε
(e) L = {w |w = wR, that is, w is a palindrome}:
S → 0S0 | 1S1 |M
M → ε | 0 | 1
4. Convert the following CFG to an equivalent CFG in Chomsky normal form, given theprocess given in Theorem 2.9.
S → ε, S → 0S1S, S → 1S0S
Solutions: (20 points in total)
Follow the 4 steps, i.e.,
(1) adding new start variable S0:
S0 → S, S → ε, S → 0S1S, S → 1S0S
(2). removing ε rules (S → ε):
S0 → S, S0 → ε, S0 → S, S → 01S, S → 0S1, S → 01, S → 0S1S,
S → 10S, S → 1S0, S → 10, S → 1S0S
(3) removing unit rules (S0 → S):
S0 → ε, S0 → 01S, S0 → 0S1, S0 → 01, S0 → 0S1S,
S0 → 10S, S0 → 1S0, S0 → 10, S0 → 1S0S
S → 01S, S → 0S1, S → 01, S → 0S1S,
S → 10S, S → 1S0, S → 10, S → 1S0S
(4) patching to proper forms, introducing new rules
Z → 0, W → 1, Ws →WS, Sw → SW, Zs → ZS, Sz → SZ,
Sws → SWs, Szs → SZs
patching to get additional rules:
S0 → ZWs, S0 → ZSw, S0 → ZW, S0 → ZSws, S0 →WZs, S0 →WSz, S0 →WZ, S0 →WSzs
S → ZWs, S → ZSw, S → ZW, S → ZSws, S →WZs, S →WSz, S →WZ, S →WSzs
and old rules:S0 → ε
5. Question 2.14 on page 156.
Solutions:
• The grammar isA→ BAB |B | ε
B → 00 | ε
• add the new start variable S0:
S0 → A
• remove ε-rules (B → ε):
S0 → A, A→ AB |BA |A |BAB |B | ε, B → 00
remove ε-rules (A→ ε):
S0 → A | ε, A→ AB |BA |BB |BAB |B, B → 00
• remove unit rules (A→ B):
S0 → A | ε, A→ AB |BA |BAB |BB | 00, B → 00
remove unit rules (S0 → A):
S0 → ε |AB |BA |BAB |BB | 00
A→ AB |BA |BAB |BB| 00, B → 00
• patching, introducing new rules:
Z → 0, Ab → AB
thenS0 → ε |AB |BA |BAb |BB |ZZ
A→ AB |BA |BAb |BB|ZZ, B → 00
6. Question 2.5 on page 155 (for question 2.4 (b) only).
Solutions: