Norml Forms for Context-free Grmmrs 1
Linz 6th, Section 6.2 wo Importnt Norml Forms, pges 171--178 2
Chomsky Norml Form All productions hve form: A BC nd A vrile vrile terminl 3
Exmples: S AS S AS S S AAS A SA A SA A A Chomsky Norml Form Not Chomsky Norml Form 4
Conversion to Chomsky Norml Form Exmple: S AB A B Ac Not Chomsky Norml Form 5
6 Ac B A AB S Introduce vriles for terminls: c A B A AB S c c c,,
7 Introduce intermedite vrile: c A B A AB S c c c A B A B V AV S c c 1 1 V 1
8 Introduce intermedite vrile: c A B V V A B V AV S c c 2 2 1 1 V 2 c A B A B V AV S c c 1 1
9 Finl grmmr in Chomsky Norml Form: c A B V V A B V AV S c c 2 2 1 1 Ac B A AB S Initil grmmr
In generl: From ny context-free grmmr not in Chomsky Norml Form we cn otin: An equivlent grmmr in Chomsky Norml Form 10
he Procedure First remove: Nullle vriles Unit productions 11
For every symol : Add production In productions: replce with New vrile: 12
Replce ny production A C C 1 2 C n with A V 1 C V 1 C 2 1 V 2 V n2 C n1 C n New intermedite vriles: V1, V2,, V n 2 13
heorem: For ny context-free grmmr G such tht the empty string is not in L(G), there is n equivlent grmmr in Chomsky Norml Form Linz, 6 th, heorem 6.6, pge 172. 14
Oservtions Chomsky norml forms re good for prsing nd proving theorems It is very esy to find the Chomsky norml form of ny context-free grmmr 15
Greich Norml Form All productions hve form: A V V 1 2 V k k 0 symol vriles 16
Exmples: S cab S S A A B S B Greich Norml Form Not Greich Norml Form 17
Conversion to Greich Norml Form: S S S S S S Greich Norml Form 18
heorem: For ny context-free grmmr G such tht the empty string is not in L(G), there is n equivlent grmmr in Greich Norml Form Linz, 6 th, heorem 6.7, pge 176. Proof not given ecuse it is too complicted. 19
Oservtions Greich norml forms re very good for prsing It is hrd to find the Greich norml form of ny context-free grmmr 20
An Appliction of Chomsky Norml Forms 21
he CYK Memership Algorithm J. Cocke, D. H. Younger, nd. Ksmi Input: Grmmr in Chomsky Norml Form G String w Output: find if w L(G) 22
Considers every possile consecutive susequence of letters nd sets K [i,j] if the sequence of letters strting from i to j cn e generted from the non-terminl K. Once it hs considered sequences of length 1, it goes on to sequences of length 2, nd so on. 23
For susequences of length 2 nd greter, it considers every possile prtition of the susequence into two hlves, nd checks to see if there is some production A-> BC such tht B mtches the first hlf nd C Mtches the second hlf. If so, it records A s mtching the whole susequence. Once this process is completed, the sentence is recognized y the grmmr if the entire string is mtched y the strt symol. 24
Input exmple: he Algorithm Grmmr : G S AB A BB A B AB B String : w 25
[0:1] [1:2] [2:3] [3:4] [4:5] [0:2] [1:3] [2:4] [3:5] [0:3] [1:4] [2:5] [0:4] [1:5] [0:5] 26
27
S A A B B AB BB AB A A B B B 28
S A A B B AB BB AB A A B S,B A B A B 29
S A A B B AB BB AB A S,B A S,B A B A S,B B A B A S,B S,B 30
herefore: L(G) ime Complexity: 3 w Oservtion: he CYK lgorithm cn e esily converted to prser 31
Pushdown Automt PDAs 32
Pushdown Automton -- PDA Input String Stck Sttes 33
Initil Stck Symol Stck Stck $ z ottom specil symol 34
he Sttes Input symol Pop symol Push symol, c q 1 q 2 35
, c q 1 q 2 input stck top Replce c e h $ e h $ 36
, c q 1 q 2 input stck c top Push e h $ e h $ 37
, q 1 q 2 input stck top Pop e h $ e h $ 38
, q 1 q 2 input stck top No Chnge e h $ e h $ 39
Non-Determinism, c q 2 q 1, c q 1 q 2, c q 3 trnsition hese re llowed trnsitions in Non-deterministic PDA (NPDA) 40
NPDA: Non-Deterministic PDA Exmple:,,,,, $ $ q 0 q 1 q 2 q 3 41
Execution Exmple: Input ime 0 $ Stck current stte,,,,, $ $ q q 1 q 2 q 3 0 42
Input ime 1 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 43
Input ime 2 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 44
Input ime 3 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 45
Input ime 4 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 46
Input ime 5 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 47
Input ime 6 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 48
Input ime 7 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 49
Input ime 8 $ Stck,, ccept,,, $ $ q 0 q 1 q 2 q 3 50
A string is ccepted if there is computtion such tht: All the input is consumed he lst stte is finl stte At the end of the computtion, we do not cre out the stck contents 51
he input string is ccepted y the NPDA:,,,,, $ $ q 0 q 1 q 2 q 3 52
In generl, L n n { : n 0} is the lnguge ccepted y the NPDA:,,,,, $ $ q 0 q 1 q 2 q 3 53
Another NPDA exmple NPDA M L( M ) R { ww },,,, q 0,, $ $ q 1 q 2 54
Execution Exmple: Input ime 0 $,, Stck,, q 0,, $ $ q 1 q 2 55
Input ime 1 $,, Stck,, q 0,, $ $ q 1 q 2 56
ime 2 Input $,, Stck,, q 0,, $ $ q 1 q 2 57
ime 3 Input Guess the middle of string $,, Stck,, q 0,, $ $ q 1 q 2 58
ime 4 Input $,, Stck,, q 0,, $ $ q 1 q 2 59
Input ime 5 $,, Stck,, q 0,, $ $ q q 2 1 60
Input ime 6 $,, Stck,, ccept q 0, q 1, $ $ q 2 61
Rejection Exmple: Input ime 0 $,, Stck,, q 0,, $ $ q 1 q 2 62
Input ime 1 $,, Stck,, q 0,, $ $ q 1 q 2 63
Input ime 2 $,, Stck,, q 0,, $ $ q 1 q 2 64
Input ime 3 Guess the middle of string $,, Stck,, q 0,, $ $ q 1 q 2 65
Input ime 4 $,, Stck,, q 0,, $ $ q 1 q 2 66
Input ime 5 here is no possile trnsition. Input is not consumed $,, Stck,, q 0,, $ $ q q 2 1 67
Another computtion on sme string: Input ime 0 $,, Stck,, q 0,, $ $ q 1 q 2 68
Input ime 1 $,, Stck,, q 0,, $ $ q 1 q 2 69
Input ime 2 $,, Stck,, q 0,, $ $ q 1 q 2 70
Input ime 3 $,, Stck,, q 0,, $ $ q 1 q 2 71
Input ime 4 $,, Stck,, q 0,, $ $ q 1 q 2 72
Input ime 5 No finl stte is reched $,, Stck,, q 0,, $ $ q 1 q 2 73
here is no computtion tht ccepts string L(M ),,,, q 0,, $ $ q 1 q 2 74