CI 3104 /Winter 2011: Introduction to Forml Lnguges Chter 13: Grmmticl Formt Chter 13: Grmmticl Formt I. Theory of Automt II. Theory of Forml Lnguges III. Theory of Turing Mchines Dr. Neji Zgui CI3104-W11 1 Chter 13: Grmmticl Formt Theorem. All regulr lnguges re context-free lnguges. Proof. We show tht for ny FA, there is CFG such tht the lnguge generted y the grmmr is the sme s the lnguge cceted y the FA. By constructive lgorithm. Inut: finite utomton. Outut: CF grmmr. Dr. Neji Zgui CI3104-W11 2 Chter 13: Grmmticl Formt The lhet of terminls is the lhet of the FA. Nonterminls re the stte nmes. (The strt stte is renmed.) For every edge, crete roduction: For every finl stte, crete roduction: Λ Dr. Neji Zgui CI3104-W11 3 1
Chter 13: Grmmticl Formt Exmle: M / M F/ F F/ F/ Λ M M F F F M F M M F F F, Dr. Neji Zgui CI3104-W11 4 Chter 13: Grmmticl Formt Definition: A semiword is sequence of terminls (ossily none) followed y exctly one nonterminl. (terminl)(terminl) (terminl)(nonterminl) Definition: A CFG is regulr grmmr if ll of its roductions hve the form: Nonterminl semiword or Nonterminl word ( sequence of terminls or Λ) Dr. Neji Zgui CI3104-W11 5 Chter 13: Grmmticl Formt Theorem. All lnguges generted y regulr grmmrs re regulr. Proof. By constructive lgorithm. We uild trnsition grh. The lhet of the trnsition grh is the set of terminls. One stte for ech nonterminl. The stte nmed is the strt stte. We dd one finl stte. Trnsitions: N x w y N z N w q N x w y N N w q z Dr. Neji Zgui CI3104-W11 6 2
Chter 13: Grmmticl Formt Exmle: L L ()* Alterntive Algorithm: 1. N wq whenever wq is not Λ 2. For ech trnsition of the form N Λ, we mrk the stte for N with.. Dr. Neji Zgui CI3104-W11 7 N w q Chter 13: Grmmticl Formt Exmle / / / / Λ / / / L,, EVEN-EVEN Dr. Neji Zgui CI3104-W11 8 Chter 13: Grmmticl Formt If the emty word is in the lnguge, roduction of the form N Λ (clled Λ-roduction) is necessry. The existence of roduction of the form N Λ des not necessrily men tht Λ is rt of the lnguge. Λ Theorem. Let L e lnguge generted y CFG. There exists CFG without roductions of the form Λ such tht: 1. If L L, L is generted y the new grmmr. 2. If L L, ll words of L excet for L re generted y the new grmmr. Dr. Neji Zgui CI3104-W11 9 3
Chter 13: Grmmticl Formt Definition. A roduction of the form: Nonterminl one Nonterminl is clled unit roduction. Theorem. Let L e lnguge generted y CFG tht hs no Λ- roductions. Then there is nother CFG without Λ - roductions nd without unit roductions tht genertes L. Theorem. Let L e lnguge generted y CFG. The there exists nother CFG tht genertes ll words of L (excet Λ) such tht ll the roductions re of the form: Nonterminl sequence of Nonterminls Nonterminl one terminl Dr. Neji Zgui CI3104-W11 10 Chter 13: Grmmticl Formt Definition. A CFG in is sid to e in Chomsky Norml Form (CNF) if ll the roductions hve the form: Nonterminl (Nonterminl)(Nonterminl) Nonterminl terminl Theorem. Let L e lnguge generted y CFG. There there is nother grmmr which is in CNF tht genertes ll the words of L (excet Λ). Dr. Neji Zgui CI3104-W11 11 Chter 13: Grmmticl Formt Definition. In sequence of terminls nd nonterminls in derivtion (clled working string), if there is t lest one nonterminl, then the first one is clled the leftmost nonterminl. Definition. A leftmost derivtion is derivtion where t ech ste, roduction is lied to the leftmost nonterminl in the working string. Exmle. Dr. Neji Zgui CI3104-W11 12 4
Chter 13: Grmmticl Formt Exmle Derivtion I Derivtion II Dr. Neji Zgui CI3104-W11 13 Chter 13: Grmmticl Formt Dr. Neji Zgui CI3104-W11 14 Chter 13: Grmmticl Formt Dr. Neji Zgui CI3104-W11 15 5
Chter 13: Grmmticl Formt Theorem. Any word tht is in the lnguge generted y CFG hs leftmost derivtion. ( ) Exmle. () ~ q () ( ) ( ) ( ()) ( ( )) ( (~ )) ( (~ )) ( (~ q)) ( ) ~ q Dr. Neji Zgui CI3104-W11 16 6