2
Stanford University Computer Science 154 Problem Set 3 Due Monday, July 18, 2011 at 3:15pm. Look at syllabus for late policy All problem sets should be emailed to cs154-sum1011-sta@lists.stanford.edu and cc’d to [email protected], dropped obefore lecture, or placed in the CS154 box on the rst oor of Gates. See syllabus for collaboration policy. For this problem set, you may use the following version of the Context-Free Pumping Theorem: For any context-free language L, there is a number k > 0 such that for any w L such that |w| k there exist u,v,x,y,z Σ such that w = uvxyz , |vxy | k, vy = e, and uv n xy n z L for all n 0. PROBLEM 1 (4+5+5 points) Give a context-f ree grammar for each of the following three langua ges over the alphabet Σ = {a, b}: (A) L = {w : w has twice as many a’s as b’s}. (B) L = {a i b  j : i = 3  j + 2}. (C) L = {a i b  j : i = j and i = 2  j }. PROBLEM 2 (15 points) Recall that in PSet 2 we dene Fraternal-Twin(L) = {wy : |w| = |y| and w, y L}. We claim that if L is regular, Fraternal-Twin(L) is context free, and now(!!) you get to prove it in another way: Given a DFA M = ( K, Σ, δ ,s,F  ) that accepts a regular language L, give a formal construction for a PDA that accep ts F rater nal-Twin( L) and prove your construction correct. PROBLEM 3 (15 points) Recall that we call a grammar ambiguous if it allows two distinct leftmost derivations of the same string. Let G = (V, Σ,R,S  ) be the following grammar. S AS | e A A1 | 0A1 | e Σ = {0, 1} V = Σ {A, S }

pset3_z

  • Upload
    hoa-vo

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

Page 1: pset3_z

8/3/2019 pset3_z

http://slidepdf.com/reader/full/pset3z 1/2

Stanford University

Computer Science 154

Problem Set 3

Due Monday, July 18, 2011 at 3:15pm.Look at syllabus for late policy

All problem sets should be emailed to [email protected] and cc’d [email protected], dropped off before lecture, or placed in the CS154 box on the first floor

of Gates.See syllabus for collaboration policy.

For this problem set, you may use the following version of the Context-Free PumpingTheorem: For any context-free language L, there is a number k > 0 such that for anyw ∈ L such that |w| ≥ k there exist u,v,x,y ,z ∈ Σ∗ such that w = uvxyz, |vxy| ≤ k,vy = e, and uvnxynz ∈ L for all n ≥ 0.

PROBLEM 1 (4+5+5 points)

Give a context-free grammar for each of the following three languages over the alphabet Σ ={a, b}:(A) L = {w : w has twice as many a’s as b’s}.(B) L = {aib j : i = 3 j + 2}.(C) L = {aib j : i = j and i = 2 j}.

PROBLEM 2 (15 points)

Recall that in PSet 2 we define Fraternal-Twin(L) ={

wy :|w

|=

|y

|and w, y

∈L

}. We claim that

if  L is regular, Fraternal-Twin(L) is context free, and now(!!) you get to prove it in another way:Given a DFA M  = (K, Σ,δ ,s ,F ) that accepts a regular language L, give a formal construction

for a PDA that accepts Fraternal-Twin(L) and prove your construction correct.

PROBLEM 3 (15 points)

Recall that we call a grammar ambiguous if it allows two distinct leftmost derivations of the samestring.

Let G = (V, Σ, R , S  ) be the following grammar.

S  → AS  | e

A → A1 | 0A1 | e

Σ = {0, 1}

V  = Σ ∪ {A, S }

Page 2: pset3_z

8/3/2019 pset3_z

http://slidepdf.com/reader/full/pset3z 2/2

(A) Show that G is ambiguous.

(B) Give a new grammar that generates the same language as G but is unambiguous. Justify brieflywhy your grammar generates the same language and why it is unambiguous.

PROBLEM 4 (8+8 points)

Determine whether each of the following languages is context-free or not. Support each of youranswers with a proof.(A) {anbncndn : n ≥ 0}(B) {aib jck : i = j or i = k or j = k}

PROBLEM 5 (10+5 points)

(A) Transform the grammar G = (V, Σ, R , S  ) where V  = {S,A,B,a,b}, Σ = {a, b}, andR = {S  → Ab | Ba,A → AAb | Sa | a, B → BBa | Sb | b} into an equivalent grammar in Chomskynormal form. Use the algorithm presented in lecture.(B) Check whether the strings baaaabbb and ababbaa are generated by the grammar, using therecognition algorithm for grammars in Chomsky normal form given in class. Show your work.

PROBLEM 6 (5+5+5+5+5 points)

State whether each of the following languages is regular or not regular. Prove your answers. The

decimal notation for a number is the number written in the usual way, as a string over the alphabet{0, 1, . . . , 9}. In unary notation, only the symbol I  is used; thus 5 would be represented as IIIII in unary notation, and zero would be represented by the empty string.(A) {w : w is, for some n ≥ 0, the unary notation for 10n}(B) {w : w is, for some n ≥ 0, the decimal notation for 10n}(C) {anbmambn : m, n ≥ 0}(D) {am−n : m/n = 5}(E) {ambn : m, n are prime factors of an integer less than 2003}