CS 461 Oct 3 Converting a CFG into









- Slides: 9
CS 461 – Oct. 3 • Converting a CFG into a PDA • Union, intersection, complement of CFL’s • Pumping lemma for CFL’s
CFG PDA Algorithm described in book • Have state 1 push S on stack, and go to state 2. • State 2 actions: – (Note that any symbol in grammar could be tos, and any terminal could be input. ) – When tos is variable, ignore input. Action is to replace variable with its rules on stack. • Multiple rules non-determinism • Push symbols out of order. • Push ε means leave stack alone. – When tos is terminal, need action for when input=tos: just pop it. Don’t need action for !=, just let nondeterministic instance disappear.
Example: S AB A ε | 1 A 2 B ε | B 3 State 1 2 Tos ignore Input ignore Action Push Pop S Pop A S goto Push 2 AB S A B 1 2 3 ignore 1 2 3 Pop Pop A Push 1 A 2 Pop B Push B 3 Notice how the grammar rules are encoded into the machine!
Let’s trace 1122333 State 1 2 Tos ignore Input ignore Action Push Pop S Pop A S goto Push 2 AB S 1 1 A A A 2 2 2 S B B A B 1 2 3 ignore 1 2 3 Pop Pop A Push 1 A 2 Pop B A 2 2 B 2 2 2 B 3 B B 3 3 Pop B Push B 3 3 3 3 3
Combining CFLs • Let’s write grammars for L 1 = { 1 i 2 i 3 j } and L 2 = { 1 i 2 j 3 j } • Grammar for L 1 U L 2 ? • How about ∩ ? Simple technique. L 1 ∩ L 2 = { 1 n 2 n 3 n } Soon, we’ll prove this is not CFL. What can we conclude? • Complement? Remember: A ∩ B = (A’ U B’)’
Pumping lemma L is a CFL implies: There is a p, such that for any string s, |s| >= p We can break up s into 5 parts uvxyz: | v y | >= 1 | v x y | <= p and uvixyiz L. • How does this differ from the first pumping lemma?
In other words • If L is a CFL, then for practically any word w in L, we should be able to find 2 substrings within w located near each other that you can simultaneously pump in order to create more words in L. • Contrapositive: If w is a word in L, and we can always create a word not in L by simultaneously pumping any two substrings in w that are near each other, then L is not a CFL.
Gist • A string can grow in 2 places. – When we write recursive rules, we can add symbols to left or right of a variable. • Ex. S #A# A ε | 1 A 2 Look at derivation of: #111… 222# • Even a complex rule like S S 1 S 1 S 2 can be “simplified”, and still only need 2 places to grow the string. – This is because any CFG can be written in such a way that there are no more than 2 symbols on the right side of .
Example • L = { 1 n 2 n } should satisfy the pumping lemma. Let p = 2. Strings in L of length >= 2 are: { 12, 1122, 111222, … }. All these words have both a 1 and a 2, which we can pump. Just make sure the 2 places to pump are within p symbols of each other. So let’s say they are in the middle. u v x y z 11111… 1 1 ε 2 22222…. 2 Could p = 1 work?