You are on page 1of 6

CS 321 Homework 7 Nov 12, 2015

1. Write down the definitions of the following levels of ambiguity:


(a) ambiguous string
(b) ambiguous grammar
(c) ambiguous language

Now answer the following (justify your answer):


(d) does an ambiguous grammar always produce at least one ambiguous string?
(e) does an ambiguous language always contain at least one ambiguous grammar?
(f) is it the case that all strings in an ambiguous language also ambiguous?
(g) is it possible to know if a string is ambiguous (given a grammar)?
(h) is it possible to know if a grammar is ambiguous?
(i) is it possible to know if a language is ambiguous?
(j) is C++/Java/Python ambiguous?
(k) is English/Chinese ambiguous?

solution: Definitions.

• ambiguous string : a string s is ambiguous under grammar G, if


there are multiple parse trees for G to parse s
• ambiguous grammar: a grammar G is ambiguous, if
∃s ∈ L(G), s.t. s is ambiguous under G.

• ambiguous language: a language L0 is ambiguous, if


∀G, L0 = L(G) ⇒ G is ambiguous.
Answers.
(d) Yes, by definition.

(e) Yes, actually any grammar it contains is ambiguous.


(f) No. For example, L = {ai bj ck |i = j or j = k} is ambiguous but s = ab could have a unique parse tree
under some ambiguous grammar of L.

S → S0 |S1 split this language to two unambiguous languages

S0 → S0 c|A0 L0 = {ai bj ck |i = j}
A0 → aA0 b|

S1 → aS1 |A1 L1 = {ai bj ck |j = k}


A1 → bA1 c|

Note that L0 , L1 and their corresponding grammars are unambiguous.


From s ∈ L0 and s ∈
/ L1 we get s is unambiguous under this grammar.
Unique parsing for s: S ⇒ S0 ⇒ A0 ⇒ aA0 b ⇒ ab.

(g) Yes. By parse this string by the given grammar.


(h) Sometimes not. It is undecidable.
Note that for some grammar, it is possible to decide.
CS 321 Homework 7 Page 2 of 6

(i) Sometimes not. It is undecidable.


Note that for some language, it is possible to decide.
(j) No. The compiler/interpreter has a unique way to parse.
(k) Yes. Syntax ambiguity is quite common in natural language.

2. Write down a single grammar that works for _all_ the following sentences:
(a) I saw a boy with a telescope
(b) I saw boys with girls
(c) I eat sushi with tuna

And draw two parse trees for each sentence.

Solution:

S → NP VP
NP → N | D N | NP PP
VP → V NP | VP PP [∗]
PP → P NP
N → I | boy | telescope | boys | girls | sushi | tuna
V → saw | eat
P → with
D→a

(a) I saw a boy with a telecope.


S

NP VP

N
VP PP
I
V NP P NP
saw D N with D N
a boy a telescope
S

NP VP

N
V NP
I
saw
NP PP

D N P NP
a boy with D N

a telescope

Cont.
CS 321 Homework 7 Page 3 of 6

(b) I saw boys with girls.


S

NP VP

N V NP
I saw NP PP

N P NP

boys with N

girls
S

NP VP

N
VP PP
I
V NP P NP

saw N with N

boys girls

(c) I eat sushi with tuna.


S

NP VP

N V NP
I eat NP PP

N P NP

sushi with N

tuna
S

NP VP

N
VP PP
I
V NP P NP

eat N with N

sushi tuna

Cont.
CS 321 Homework 7 Page 4 of 6

3. Now write two unambiguous grammars for the above problem, one with the PP modifying NP,
and one with the PP modifying VP.
Solution:
G0 (PP modifying NP) : replace [*] with VP → V NP.
G1 (PP modifying VP) : replace [*] with

VP → VP’ PP
VP’ → V NP

4. Give an example of ambiguous language. Write a CFG for it and demonstrate why it is ambiguous.
Solution:
See question 1(f). s = abc is ambiguous under the grammar since s ∈ L0 and s ∈ L1 .

S ⇒ S0 ⇒ S0 c ⇒ A0 c ⇒ aA0 bc ⇒ abc
S ⇒ S1 ⇒ aS1 ⇒ aA1 ⇒ abA1 c ⇒ abc
5. Write a CFG for the language over {a,b} which has same number of a’s as b’s.
Is this CFG ambiguous? Justify.
Solution:

S → aSb | bSa | SS | 
It is ambiguous. Choose s = ,

S⇒S⇒
S ⇒ SS ⇒ S ⇒ 
6. Write a CFG for {a^m b^n | m != n, and m,n >= 0 }.
Solution:

S0 → A | B
A → aA | aS
B → Bb | Sb
S →  | aSb

7. Write a CFG for {a^n b^n a^m b^m | m,n >= 1} \cup {a^n b^m a^m b^n | m,n >= 1 }.
If your grammar is ambiguous, prove it.
What about we replace the \cup with \cap?
Solution:
∪ (Note that m, n ≥ 1, not 0)

S → S0 | S1
S0 → W W
W → aW b | ab
S1 → aT b | aS1 b
T → bT a | ba

Cont.
CS 321 Homework 7 Page 5 of 6

∩ No CFG satisfies this.


(pump s = ap bp ap bp )
8. Draw a DFA for the strings over {a,b} with every pair of a’s followed by at least one b.
Then convert this DFA to a right-linear CFG (regular grammar).
In a right-linear CFG, all rules are either in the form of A->aB or A->a.
Solution:
b

b
start A B
a

b a

S0 →  | A
A → b | bA | a | aB
B → aC | b | bA
C → b | bA

9. Convert this CFG


S -> 0S0 | 1S1 | \epsilon
to Chomsky Normal Form (CNF).
Note that The CNF defined on the slides is missing one minor detail -- what is it?
Solution:

S →  | C0 C0 | C1 C1 | C0 U | C1 V
S1 → C0 C0 | C1 C1 | C0 U | C1 V
U → S1 C0
V → S1 C1
C0 → 0
C1 → 1

Missing part: if S →  is allowed, then any right-hand S is not allowed.


10. Write a CFG for the complement of copy language, and justify your answer.
(If you can write a formal proof, great, but just explaining it would also help).
Solution:
CFG G:
S → A | B | AB | BA
A → 0 | 0A0 | 0A1 | 1A0 | 1A1 string with odd length and the medium character is 0
B → 1 | 0B0 | 0B1 | 1B0 | 1B1 string with odd length and the medium character is 1

∀s ∈
/ LC (copy language), discuss two cases.

Cont.
CS 321 Homework 7 Page 6 of 6

1. |s| is odd. Then


s ∈ L(A) or s ∈ L(B)
⇒ s ∈ L(S).

2. |s| is even. Denote s = c0 c1 ..cp−1 c00 c01 ...c0p−1 , p ≥ 0. We have

s∈
/ LC
⇒ ∃j, s.t. 0 ≤ j < p, cj 6= c0j , i.e. {cj , c0j } = {0, 1}
⇒ s1 ∈ L(W0 ), s2 ∈ L(W1 ), where s = s1 s2 , |s1 | = 2j + 1, {W0 , W1 } = {A, B}
⇒ s ∈ L(S).

11. For each of the following CFG,


(a) describe (in English) the language it generates
(b) decide whether the language is regular
(c) if it is regular, write a right-linear grammar (i.e., regular grammar) for it.

(A) S -> SaS | b


(B) S -> aSb | bSa | \epsilon
(C) S -> SSS | a | ab

Solution:

(A) String with odd length that any character with even/odd index must be b/a.
Regular. Regular expression: b(ab)∗ .

S → bB | b
B → aS

(B) String that copies a random string as the first half, reverse it then make a reversion on every character
as the second half.
Not regular. (pump s = ap bp )
(C) String with odd number of a and every b is on the right side of some a.
Regular. Regular expression: (a|ab)((a|ab)(a|ab))∗ .

S → aA | a
A → aC | bB | b
B → aC
C → aA | a | bD
D → aA | a

The End.

You might also like