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 by the following NFA. Give a derivation and parsing tree for string aababa. Solutions: • The regular grammar for the language recognized by the NFA is: A 1 → A 3 | bA 2 | A 2 → aA 2 | aA 3 | bA 3 A 3 → aA 1 • The derivation of string aababa is: A 1 ⇒ A 3 ⇒ aA 1 ⇒ aA 3 ⇒ aaA 1 ⇒ aabA 2 ⇒ aabaA 2 ⇒ aababA 3 ⇒ aababaA 1 ⇒ aababa = aababa Draw parsing tree (in the classroom): 1
6
Embed
Homework Assignment 4 Solutions - Computer Sciencecobweb.cs.uga.edu/~cai/courses/2670/2016fall/2670HW4-solutions.pdfHomework Assignment 4 Solutions CSCI 2670 Introduction to Theory
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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