You are on page 1of 32

L = { aibjck : i > j * k }

L = {aibj : j = 4i + 2 }

L = {w {a, b, c}* : every a has a matching b and a matching c somewhere in w and no b or c is considered to match more than one a!} "or e#am$%e, L inc%&des aabcbc, aabcbccc, and bccbbba' (&t the strings aabbc, bcbcaaa, and ab are not in L'

L {anbm : n + m = 2 mod)!, n *, m *}

Example
L = {bam*bam2b+bamn : n 2, m*, +, mn ,, and mi mj -or some i, j} . /010. -or L:

. 2"3 -or L:

1rove that L is not reg&%ar'

Finding an Unambiguous Grammar


3.2.B' L = {w {a, b}* : w contains e4&a% n&mbers o- a5s and b5s} a! 6 a6b 6 b6a 6 6 66 b! i! 6 66 a6b6 aa6bb6 aabba6b aabbab ii! 6 66 666 a6b66 aa6bb66 aabb66 aabba6b6 aabbab6 aabbab 7wo di--erent $arse trees, b&t the same bracketing 8a8ab9b98ab9 j&st 5s in di--erent $%aces!' :orse: iii! 6 a6b a66b aab6b 8a8ab98ba9b9 c! 6o we5ve got an ambig&o&s grammar'

d! :hat i- we try to -i# it the same way we -i#ed ba%anced $arentheses getting rid o- e#ce$t at the to$!: 6 67 7 ab 7 a7b 7 ba 7 b7a 7 77 (&t this doesn5t work:

8aabb9 8ab9

8a 8ab9 8ba9 b9

6 6 7a 6 7b 7a . 7a .( 7a .(7a 7b ( 7b (. 7b (.7b . .*

;* . region -irst ;* ( region -irst ;* j&st a sing%e . region ;* two regions, an . -o%%owed by a ( ;* we write this instead o- 7a 7a7a

;* this . region can be com$osed o- a sing%e ba%anced set o- a5s and b5s . .*. ;* or it can have arbitrari%y many s&ch ba%anced sets' .* a.b ;* a ba%anced set is a string o- . regions inside a matching a, b $air .* ab ;* or it bottoms o&t to j&st the $air a, b ( (* ( (*( (* b(a

(* ba

Is L Regular?
L = {w {a, b}* : w contains e4&a% n&mbers o- a5s and b5s}

Chomsk !ormal Form


<<+7 <7 77*" 7" " <! " id
6te$ )' < * 7, " 7 * " 3= = <<+7 77*" " <! " id 7hen we add: <7*" < <! < id 7 <! 7 id 6te$ 4' ?!

< <17 7 7>" " L<? " id < 7>" < L<? < id 7 L<? 7 id 1+ >* L

6te$ @'

< < <5 <5 17 7 7 75 75 > " " L "5 "5 < ? " id < 7 75 < L"5 < id 7 L "5 7 id 1+ >* L ?!

1rove that conte#t -ree %ang&ages are c%osed &nder di--erence with any reg&%ar %ang&age'

1rove that L = {anbmcndm: n m} is not conte#t -ree &sing the $&m$ing %emma -or conte#t -ree %ang&ages'

2onvert the -o%%owing conte#t -ree grammar to 2homsky /orma% "orm' 6how yo&r ste$s' 6 a.a A b(b A .2Aa (2Ab 2 20. A 0 . A ( A ab

2onstr&ct a 2"3 -or L = {w: w is the binary re$resentation o- a nonBnegative integer m&%ti$%e o- 4 witho&t %eading Ceros}

2onstr&ct a 2"3 -or L = {aibjck: )i @k + 2}

6how that L 3! is deterministic conte#t -ree by constr&cting a deterministic 10. -or LD' 3 is as -o%%ows: 6 a6b A a6 A 66 A

"rue or #alse$
a'7he set o- reg&%ar %ang&ages over some a%$habet is a s&bset o- the set o- conte#t -ree %ang&ages over the same a%$habet' b' E- a conte#t -ree grammar is not in reg&%ar grammar -orm it cannot generate a reg&%ar %ang&age' c'. 10. acce$ts an in$&t string i- and on%y i- it cons&mes a%% in$&t and %eaves the 10. in a -ina% state' d' 1ractica% $arsers &se nondeterminism to he%$ them -ind the correct $arse -or a given in$&t' e'E- L* and L2 are conte#t -ree %ang&ages, then L** L*L2 L*,!! is conte#t -ree' -' E- a %ang&age is -inite, it cannot have a conte#t -ree %ang&age as a s&bset'

g'3iven the conte#t -ree grammar, 6 6a6b A , the -o%%owing is a va%id %e-tmost derivation -or the string ababab' 6 6a6b 6a6ba6b 6a6ba6ba6b6 a6ba6ba6b6 aba6ba6b6 ababa6b6 ababab6 ababab h' 7he grammar in $art g! is ambig&o&s' i' 7here are conte#t -ree %ang&ages -or which it is not $ossib%e to constr&ct an &nambig&o&s grammar' j' 7he $rogram yacc &ses a to$Bdown $arsing a%gorithm with one symbo% %ookBahead' k' 7he set o- conte#t -ree %ang&ages over a sing%e character a%$habet is $recise%y the set oreg&%ar %ang&ages over that same a%$habet' %' 1&m$ing is the mechanism by which conte#t -ree grammars generate in-inite %ang&ages' m' 7here is an a%gorithm to constr&ct a rightmost derivation -rom a derivation tree and an a%gorithm to constr&ct derivation tree -rom a rightmost derivation'

n' 2onsider the 2"3 given by the -o%%owing r&%es: 6 66 A a6b A b6b A b6a A a6a A ' Es L 3! reg&%arF
o'

Es the intersection o- two reg&%ar %ang&ages a conte#t -ree %ang&ageF

$' E- a conte#t -ree %ang&age is inherent%y ambig&o&s, can it be deterministic conte#t -reeF 4' E- a 10. is in a -ina% state and it has an em$ty stack, then it acce$ts the in$&t string' r' . reg&%ar %ang&age can be a s&$erset o- a conte#t -ree %ang&age' s' 7he conte#t -ree $&m$ing theorem is &sed to $rove %ang&ages are conte#t -ree' t' Et is sometimes $ossib%e to in-er the entire 2"3 -or a 2"L -rom j&st the $arse tree o- a sing%e string in that %ang&age' &' .%% -inite %ang&ages are conte#t -ree' v' E- a %ang&age is not 2"L, it is not reg&%ar'

3ive a 2"3 -or L = {anbmc$: * n m G 2n}'

2onsider the -o%%owing 10., >' 3ive a -orma% de-inition o- L >!' 6how that L >! is deterministic conte#t -ree'

b//b a//a

b/a/

a/b/

2onvert the -o%%owing grammar to 2homsky /orma% "orm' 6how yo&r ste$s' . A c.( ( d A cd. A . A (

1rove that L = { w {a, b, c, d}*: Hb w! Hc w! Hd w! , } is not conte#t -ree &sing the conte#t -ree $&m$ing theorem' Io& may additiona%%y &se any re%evant c%os&re $ro$erties'

%hor& ans'er 'i&h (us&i#i)a&ion$


a' 0oes yacc recogniCe a wider c%ass o- %ang&ages than the deterministic conte#t -ree %ang&agesF b' :hat $ro$erty m&st a 2"3 have i- the %e-tmost derivation o- a string in L 3! is a%ways e#act%y the same as the rightmost derivation o- the same stringF c' :hen is nondeterminism &se-&% in a 10.F d' :hen is nondeterminism $rob%ematic in a 10.F e' 2an yacc be &sed to recogniCe an inherent%y ambig&o&s %ang&ageF -' :hat does it im$%y when yo& go to &se the 2"L $&m$ing theorem and yo& cannot -ind a string w, which is g&aranteed to be %ong eno&ghF

3ive a 2"3 -or L = { anb*cn+mamb* }

3ive a 2"3 -or L = { anbm: 2n )m }

3ive a 2"3 -or L = a ab!+a b ab!+b ab!+

3ive a 2"3 -or L = {#: w,y, w#y {anbn}}

3iven a 2"3 3, and a reg&%ar e#$ression , create a decision $roced&re -or determining whether there is any string w s&ch that w L 3! and w L !'

2reate a 10. that recogniCes the -o%%owing %ang&age: { #yC: y a* and #, C {b,c}* and AyA = A#A + ACA } Labe% the transitions o- yo&r 10. with n&mbers and &se the %abe%s to identi-y gro&$s o- transitions that make yo&r 10. nondeterministic' E- yo&r 10. is deterministic, say so'

You might also like