Forml Lnguges implifictions of CFGs
ubstitution Rule Equivlent grmmr b bc ubstitute b bc bbc b 2
ubstitution Rule b bc bbc ubstitute b bc bbc bc Equivlent grmmr 3
In generl: xz y 1 ubstitute y 1 xz xy1z equivlent grmmr 4
Lnguge? 5
Nullble Vribles production: Nullble Vrible: 6
Removing Nullble Vribles Exmple Grmmr: Mb M Mb M Nullble vrible 7
Finl Grmmr Mb M Mb M ubstitute M Mb b M Mb M b 8
Unit-Productions Unit Production: (single vribles on both sides) 9
Removing Unit Productions Observtion: Is removed immeditely 10
Exmple Grmmr: bb 11
ubstitute bb bb 12
Remove bb bb 13
ubstitute bb bb 14
Remove repeted productions Finl grmmr bb bb 15
Lnguge? 16
Useless Productions b Useless Production ome derivtions never terminte... 17
nother grmmr: b Useless Production Not rechble from 18
In generl: contins only terminls if xy w wl(g) then vrible is useful otherwise, vrible is useless 19
production x is useless if ny of its vribles is useless b Productions Vribles useless useless useless useless C useless useless C D useless 20
Removing Useless Productions Exmple Grmmr: C C Cb 21
Remove useless productions 22
First: find ll vribles tht cn produce strings with only terminls Round 1: C {, } C Cb Round 2: {,, } 23
Keep only the vribles tht produce terminl symbols: {,, } (other vribles re useless) C C Cb Remove useless productions 24
econd: Find ll vribles rechble from Use Dependency Grph not rechble 25
Keep only the vribles rechble from (the other vribles re useless) Finl Grmmr Remove useless productions 26
Removing ll tep 1: Remove Nullble Vribles tep 2: Remove Unit-Productions tep 3: Remove Useless Vribles 27
Norml Forms for Context-free Grmmrs 28
Chomsky Norml Form Ech production hs form: C or vrible vrible terminl 29
Exmples: b Chomsky Norml Form Not Chomsky Norml Form 30
Conversion to Chomsky Norml Form Exmple: b c Not Chomsky Norml Form 31
32 c b Introduce vribles for terminls: c T b T T T T T T T c b c b c b T T T,,
33 Introduce intermedite vrible: c T b T T T T T T T c b c b c T b T T T T T T T V V c b c b 1 1 V 1
34 Introduce intermedite vrible: c T b T T T T T V V T T V V c b c b 2 2 1 1 V 2 c T b T T T T T T T V V c b c b 1 1
35 Finl grmmr in Chomsky Norml Form: c T b T T T T T V V T T V V c b c b 2 2 1 1 c b Initil grmmr
In generl: From ny context-free grmmr (which doesn t produce ) not in Chomsky Norml Form we cn obtin: n equivlent grmmr in Chomsky Norml Form 36
The Procedure First remove: Nullble vribles Unit productions 37
Then, for every symbol : dd production T In productions: replce with T New vrible: T 38
Replce ny production C C 1 2 C n with C V 1 1 V 1 C V 2 2 V n2 C n1 C n New intermedite vribles: V1, V2,, V n 2 39
Theorem: For ny context-free grmmr (which doesn t produce ) there is n equivlent grmmr in Chomsky Norml Form 40
Observtions Chomsky norml forms re good for prsing nd proving theorems It is very esy to find the Chomsky norml form for ny context-free grmmr 41
exercise Find CNF for this grmmr: -> 00 11 -> C -> C -> epsilon (exercise 7.1.3, Hopcroft, Motwni, Ullmn) 42
Greibch Norml Form ll productions hve form: V V 1 2 V k k 0 terminl vribles 43
Exmples: c bb b b b Greibch Norml Form Not Greibch Norml Form 44
Conversion to Greibch Norml Form: T b T b bb T T T b b Greibch Norml Form 45
Theorem: For ny context-free grmmr (which doesn t produce ) there is n equivlent grmmr in Greibch Norml Form 46
Observtions Greibch norml forms re very good for prsing It is hrd to find the Greibch norml form of ny context-free grmmr 47
Try to compute Greibch Norml Form for grmmr in CNF exmple 48
Compilers 49
Progrm v = 5; if (v>5) x = 12 + v; while (x!=3) { x = x - 3; v = 10; }... Compiler Mchine Code dd v,v,0 cmp v,5 jmplt ELE THEN: dd x, 12,v ELE: WHILE: cmp x,3... 50
Compiler Lexicl nlyzer prser input progrm mchine code output 51
prser knows the grmmr of the progrmming lnguge 52
PROGRM TMT_LIT TMT Prser TMT_LIT TMT; TMT_LIT TMT; EXPR IF_TMT WHILE_TMT { TMT_LIT } EXPR IF_TMT WHILE_TMT EXPR + EXPR EXPR - EXPR INT if (EXPR) then TMT if (EXPR) then TMT else TMT while (EXPR) do TMT 53
The prser finds the derivtion of prticulr input input 10 + 2 * 5 Prser E -> E + E E * E INT derivtion E => E + E => E + E * E => 10 + E*E => 10 + 2 * E => 10 + 2 * 5 54
derivtion derivtion tree E E => E + E => E + E * E => 10 + E*E => 10 + 2 * E => 10 + 2 * 5 E 10 + E E * E 2 5 55
derivtion tree E mchine code E + E mult, 2, 5 10 E * E dd b, 10, 2 5 56
Prsing 57
input string Prser grmmr derivtion 58
Exmple: input bb Prser b b derivtion? 59
Prsing lgorithm? 60
Exhustive erch b b Phse 1: Find derivtion of b bb b ll possible derivtions of length 1 61
bb b b 62
Phse 2 b b b bb Phse 1 b + 2 more b b b b bb b bb b b 63
Phse 2 b b b bb b b b bb Phse 3 b bb bb 64
Finl result of exhustive serch (top-down prsing) Prser input b bb b derivtion b bb bb 65
Is exhustive serch good prsing lgorithm? 66
Time complexity of exhustive serch uppose there re no productions of the form Number of phses for string : w 2 w 67
For grmmr with k rules Time for phse 1: k k possible derivtions 68
Time for phse 2: k 2 k 2 possible derivtions 69
Time for phse : 2 w k 2 w k 2 w possible derivtions 70
Totl time needed for string : w k k 2 2 w k phse 1 phse 2 phse 2 w Extremely bd!!! 71
There exist fster lgorithms for specilized grmmrs -grmmr: x symbol string of vribles Pir (, ) ppers once 72
-grmmr exmple: b c Ech string hs unique derivtion b bc bcc 73
For -grmmrs: In the exhustive serch prsing there is only one choice in ech phse Time for phse: 1 Totl time for prsing string w : w 74
For generl context-free grmmrs: There exists prsing lgorithm tht prses string in time 3 w w (we will show this in the next clss) 75
The CYK Prser 76
Input: The CYK Membership lgorithm Grmmr G in Chomsky Norml Form tring w Output: find if wl(g) 77
Input exmple: The lgorithm Grmmr G: b tring : w bbb 78
bbb b b b b bb bb b bb bbb bb bbb bbb 79
b b b b bb bb b b bb bbb bb bbb bbb 80
b b b b b bb, b bb bbb bb bb bbb bbb 81
b b, b, bb b bb bbb, b bb b bb bbb, bbb, 82
Therefore: bbb L(G) Time Complexity: 3 w Observtion: The CYK lgorithm cn be esily converted to prser (bottom up prser) 83
The following slides re courtesy of Professor Ppp, University of Debrecen. 84
1,7 1,1 7,7 b b b
Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b
Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b
Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b
Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b
Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b
Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b
Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b
Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b
Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b
Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b
Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b
,C Word is in lnguge if ppers here,d D, C, D D,C,C,C b b b
exercise Prse bb for this grmmr -> C -> -> CC b C -> (Hopcroft, Motwni, Ullmn, p301) 98