Download presentation
Presentation is loading. Please wait.
Published byRandell Preston Modified over 9 years ago
1
CS 461 – Sept. 26 CFG correctness Section 2.2 – Pushdown Automata
2
Correctness Given a language L and a grammar G, how do we know if L = L(G)? Must show: 1.L L(G). All words in L can be derived by grammar. Tell how to derive the words. 2.L(G) L. All words derived by grammar are in L. Use induction.
3
First example Show that S ε | 0S1 is a CFG for the language { 0 n 1 n } Step #1: Any word in 0 n 1 n can be generated by the grammar. We explain the derivation. –Apply the rule 0S1 n times –Finally, apply the ε rule. Step #2: All words generated by the grammar are of the form 0 n 1 n. –Induction on the number of times we use a rule. –S ε for the basis, and S 0S1 for the inductive step.
4
Second example L is { even number of 0s } and G is S ε | S1 | S0S0 Step #1. Suppose w L. How can G generate w? If w = ε, we’re done. Do the following until w is ε: If w ends with 1s, take them off by virtue of “S1”. Now w ends with 0, so take 2 0’s away by virtue of “S0S0”. We know there must be 2 0’s in w because w had an even number to start with.
5
Illustration S ε | S1 | S0S0 Let’s see how we can derive “010100”: The last 2 0s: S S0S0 ε0ε0 = 00 0101 is 010 + 1 010 is S S0S0 ε0S10 ε0ε10 = 010 1 is S S1 ε1 = 1 We can reconstruct derivation from beginning in form of a tree.
6
L(G) is in L S ε | S1 | S0S0 Step #2: Need to show all generated words are in L. Base case: ε is in L. It has even number of 0s. Recursive case. Let w be a word generated by the grammar. Assume it has even # 0s. We can create a larger word by applying rule “S1” or “S0S0”. S1 adds no more 0s. S0S0 adds two 0s. Either way the # of 0s stays even.
7
Extra example Let’s show that this grammar G S ε | 0S1 | 00S111 generates the language L = { 0 i 1 j | i j 1.5i } Step #1: any word in L can be generated –This is the hard part, so let’s motivate the proof with an example or two. Step #2: all words generated are in L –Just induction again.
8
L is in L(G) S ε | 0S1 | 00S111 How could we generate 0 11 1 16 ? –We have 5 extra 1s. So we use rule 00S111 five times. –Undoing these steps, # 0s = 11-5*2 = 1 and # 1s = 16-5*3 = 1. Then we use 0S1 once and we are left with ε. Okay, how about 0 12 1 16 ? –We have 4 extra 1s, so use rule 00S111 four times. –Undoing these steps, # 0s = 12-4*2 = 4 and # 1s = 16-4*3 = 4. They match! So use “0S1” 4 times.
9
Thinking out loud… S ε | 0S1 | 00S111 Let w = 0 i 1 j L. In other words, i j 1.5i Consider the number j – i. This is the number of times to apply rule #3 (00S111). Note that using rule #3 (j – i) times will account for 2(j – i) zeros. Then apply rule #2 (0S1) the “appropriate number” of times. How many? Well, we want i 0’s and we’ve already got 2(j – i), so we need the difference: i – 2(j – i) = 3i – 2j.
10
Finishing step 1 S ε | 0S1 | 00S111 Let w = 0 i 1 j L. In other words, i j 1.5i Use rule #1 once to start with empty word. Use rule #3 (j – i) times. Use rule #2 (3i – 2j) times. Total # 0s = 3i – 2j + 2(j – i) = i Total # 1s = 3i – 2j + 3(j – i) = j Thus, the word can be generated.
11
L(G) is in L S ε | 0S1 | 00S111 Now the easy part. Need to show that when we apply any rule, we preserve: i j 1.5i Base case: ε has no 0s or 1s. 0 0 1.5*0 √ Recursive case. Let w be generated by the grammar with i 0s and j 1s satisfying i j 1.5i. If we apply either rule 0S1 or 00S111, we can show (i + 1) (j + 1) 1.5(i + 1) (i + 2) (j + 3) 1.5(i + 2) (Need to work out arithmetic.)
12
Think about… Can you write a CFG for { 0 i 1 j | 0.5i j 1.5i } ? in other words, the ratio of 1s to 0s must be between ½ and 3/2.
13
PDAs What is a Pushdown Automaton? Like an FA, but PDA has a stack –Finite number of states –Stack can grow to unlimited depth Transition function often non-deterministic 2 flavors –Accept by happy state –Accept by empty stack (this one has fewer states) Can be tedious to draw, so instead give a table. –What’s new is saying what to do with stack.
14
Formal definition A PDA has 6 things. –Same five from FA, plus: –Stack alphabet (can be different from Σ) Transition function δ(state, tos, input) = (push/pop, new state) both are optional
15
{ 0 n 1 n } While reading 0s, push them. If you read 1, change state. Then, as you read 1s, pop 0s off the stack. Watch out for bad input! –Unspecified transition crash (reject) Now, let’s write this in the form of a table.
16
PDA δ for { 0 n 1 n } StateReading 0Reading 1 Tos(don’t care)0Empty Input010101 ActionPush 0 Go to state “reading 1” CrashPopCrash Notes: Action depends on input symbol AND what’s on top of stack. Action includes manipulating stack AND/OR changing state.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.