Download presentation
Presentation is loading. Please wait.
Published byOliver Clarke Modified over 8 years ago
1
CS 461 – Sept. 23 Context-free grammars Derivations Ambiguity Proving correctness
2
Derivations A sequence of steps showing how a word is generated using grammar rules. Drawing a diagram or tree can help. Example: derivation of 1110 from this grammar: S ABS A ε | 1AAB B ε | 1B0 1A1B0 11A1ε0 11ε10 11
3
Derivations, con’d There are several ways to express a derivation –Tree –Leftmost derivation = as you create string, replace leftmost variable. Example: S AB 1AB 11AB 11B 111B0 1110 –Rightmost derivation Often, grammars are ambiguous –For some string, 2+ derivation trees. Or, equivalently: 2+ leftmost derivations. –Example?
4
More examples Even number of 0’s Words of form { 0 i 1 j } where j is constrained. i j 2i (not to be confused with { 0 n 1 2n } ) i j 2i + 3 i j 1.5i Next topic: how do we show that our grammar is correct?
5
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.
6
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.
7
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.
8
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.
9
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.
10
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.
11
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.
12
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.
13
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.
14
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.)
15
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.