homework
play

Homework Assignment #6 (Due 10/27) From textbook CFLs and - PDF document

Homework Assignment #6 (Due 10/27) From textbook CFLs and Regular Languages Exercise 7.1.5 a d Exercise 7.2.1 b,e Exercise 7.3.2 a Other problems Find a regular grammar that generates the language accepted by


  1. Homework • Assignment #6 (Due 10/27) – From textbook CFLs and Regular Languages • Exercise 7.1.5 a – d • Exercise 7.2.1 b,e • Exercise 7.3.2 a – Other problems • Find a regular grammar that generates the language accepted by the FA on the next slide. • Define CFGs that generate the following langauges: – {a i b j c k | j = i + k } – {a i b j c k | j = i or j = k } Homework More schedule mangling • The schedule on the Web has been adjusted again (last time I think) – This week: Chapter 7 (though topic rearranged) • Today: CFLs and RL / Pumping Lemma • Wednesday: Closure & Decision Properties – Next week • Monday: Turing Machines • Wednesday: Exam 2 Before we begin Languages • Questions from last time? • Future Exam Question – What is a language? – What is a class of languages?

  2. Context Free Languages Context Free Grammars • Context Free Languages(CFL) is the next • Let’s formalize this a bit: class of languages outside of Regular – A context free grammar (CFG) is a 4-tuple: (V, Languages: T, S, P) where • V is a set of variables – Means for defining: Context Free Grammar • T is a set of terminals – Machine for accepting: Pushdown Automata • V and Σ are disjoint (I.e. V ∩ Σ = ∅ ) • S ∈ V, is your start symbol Context Free Grammars Now our picture looks like • Let’s formalize this a bit: Context Free Languages – Production rules Deterministic Context Free Languages • Of the form A → β where – A ∈ V Regular Languages – β ∈ (V ∪ ∑ ) * string with symbols from V and ∑ • We say that γ can be derived from α in one step: Finite – A → β is a rule Languages – α = α 1 A α 2 – γ = α 1 β α 2 – α ⇒ γ CFLs and Regular Languages CFLs and Regular Languages • Will show that all Regular Languages are • We showed last time that every RL is also a CFL CFLs – Since a NFA can simply be described as a PDA that doesn’t use the stack. - If L 1 and L 2 are CFLs then • We can also show by only using the structural - L 1 ∪ L 2 is a CFL representations of the language classes - L 1 L 2 is a CFL * is a CFL - L 1 – I.e. Regular Expressions / Context Free Grammars - With the above shown, showing every Regular – That is what we will do in this first half. Language is also a CFL can be shown using a • Note: Much of this lecture is not in the text! basic inductive proof.

  3. Union, Concatenation, and Kleene Star of CFLs Union, Concatenation, and Kleene Star of CFLs • Formally, Let L 1 and L 2 be CFLs. Then there • Union exists CFGs: – Basic Idea – G 1 = (V 1 , T, S 1 , P 1 ) • Define the new CFG so that we can either – G 2 = (V 2 , T, S 2 , P 2 ) such that – start with the start variable of G 1 and follow the – L(G 1 ) = L 1 and L(G 2 ) = L 2 production rules of G 1 or – Assume that V 1 ∩ V 2 = ∅ – start with the start variable of G 2 and follow the • We will define: production rules of G 2 – G u = (V u , T, S u , P u ) such that L(G u ) = L 1 ∪ L 2 – The first case will derive a string in L 1 – G c = (V c , T, S c , P c ) such that L(G c ) = L 1 L 2 – The second case will derive a string in L 2 – G k = (V k , T, S k , P k ) such that L(G c ) = L 1 * Union, Concatenation, and Kleene Star of CFLs Union, Concatenation, and Kleene Star of CFLs • Union • Concatenation – Formally – General Idea • G u = (V u , T, S u , P u ) • Define the new CFG so that – V u = V 1 ∪ V 2 ∪ {S u } – We force a derivation staring from the start variable of G 1 using the rules of G 1 – S u = S u – After that… – P u = P 1 ∪ P 2 ∪ {S u → S 1 | S 2 } – We force a derivation staring from the start variable of G 2 using the rules of G 2 Union, Concatenation, and Kleene Star of CFLs Union, Concatenation, and Kleene Star of CFLs • Concatenation • Kleene Star – Formally – General Idea • G c = (V c , T, S c , P c ) • Define the new CFG so that – V c = V 1 ∪ V 2 ∪ {S c } – We can repeatedly concatenate derivations of strings in L 1 • Since L * contains ε , we must be careful to assure – S c = S c – P u = P 1 ∪ P 2 ∪ {S c → S 1 S 2 } that there are productions in our new CFG such that ε can be derived from the start variable

  4. CFLs and Regular Languages Union, Concatenation, and Kleene Star of CFLs • Kleene star • Now we can complete the proof – Formally – Use an inductive proof • G k = (V k , T, S k , P k ) – V k = V 1 ∪ {S k } – S k = S k – P k = P 1 ∪ {S k → S 1 S k | ε } Regular Expression Regular Expression 4. If L 1 and L 2 are regular languages with regular • Recursive definition of regular languages / expressions r 1 and r 2 then expression over Σ : -- L 1 ∪ L 2 is a regular language with regular expression 1. ∅ is a regular language and its regular (r 1 + r 2 ) expression is ∅ -- L 1 L 2 is a regular language with regular expression (r 1 r 2 ) 2. { ε } is a regular language and ε is its regular * is a regular language with regular expression (r 1 -- L 1 * ) expression 3. For each a ∈ Σ , { a } is a regular language and Only languages obtainable by using rules 1-4 are regular its regular expression is a languages . CFLs and Regular Languages Union, Concatenation, and Kleene Star of CFLs • RE -> CFG • RE -> CFG – Base cases • Assume R 1 and R 2 are regular expressions that describe languages L 1 and L 2 . Then, by the 1. ∅ can be expressed as a CFG with no productions induction hypothesis, L 1 and L 2 are CFLs and as 2. { ε } can be expressed by a CFG with the such there are CFGs that describe L 1 and L 2 single production S → ε • Create CFGs that describe the the languages: 3. For each a ∈ Σ , { a } can be expressed by a – L 1 ∪ L 2 CFG with the single production S → a – L 1 L 2 – L 1 * • Which we just did…We are done!

  5. CFLs and Regular Languages CFLs and Regular Languages • Venn diagram 3 • What have we learned? – CFLs are closed under union, concatenation, Context Free Languages and Kleene Star – Every Regular Language is also a CFL Regular Languages – We now have an algorithm, given a Regular Finite Expression, to construct a CGF that describes Languages the same language CFLs and Regular Languages CFLs and Regular Languages • Example • Example – Find a CFG for the L = (011 + 1) * (01) * – Find a CFG for the L = (011 + 1) * (01) * • (011 + 1) can be described by the CFG with • (01) can be described by the CFG with productions: productions: – D → 01 – A → 011 | 1 • (01) * can be described by the CFG with productions: • (011 + 1) * can be described by the CFG with – C → DC | ε productions: – D → 01 – B → AB | ε – A → 011 | 1 CFLs and Regular Languages Union, Concatenation, and Kleene Star of CFLs • Example • You can use proof of closure properties in – Find a CFG for the L = (011 + 1) * (01) * building CFLs: – Putting it all together – Example: • (011 + 1) * (01) * can be described by the CFG with productions: • Find a CFL for L = {0 i 1 j 0 k | j > i + k} – S → BC – Number of 1s is greater than the combined number of 0s – B → AB | ε – A → 011 | 1 – This language can be expressed as – C → DC | ε » L = {0 i 1 i 1 m 1 k 0 k | m > 0} – D → 01 – Questions?

  6. Union, Concatenation, and Kleene Star of CFLs Union, Concatenation, and Kleene Star of CFLs – Example – Example: • CFG for L 1 = {0 i 1 i | i ≥ 0} • Find a CFL for L = {0 i 1 j 0 k | j > i + k} – A → 0A1 | ε – This language can be expressed as • CFG for L 2 = {1 m | m > 0} » L = {0 i 1 i 1 m 1 k 0 k | m > 0} – B → 1B | 1 • CFG for L 3 = {1 k 0 k | k ≥ 0} – This is concatenation of 3 languages L 1 L 2 L 3 where » L 1 = {0 i 1 i | i ≥ 0} – C → 1C0 | ε – CFG for L » L 2 = {1 m | m > 0} • S → ABC » L 3 = {1 k 0 k | k ≥ 0} • A → 0A1 | ε • B → 1B | 1 • C → 1C0 | ε CFLs and Regular Languages Union, Concatenation, and Kleene Star of CFLs • Example • Questions? – Formally • G = (V, T, S, P) where – V = {S, A, B, C} – Σ = {0, 1} – P = {S → ABC A → 0A1 | ε B → 1B | 1 C → 1C0 | ε } Regular Grammars Regular Grammars • Another means to show that all Regular • Sequence of states to accept 00110 Languages are CFL is to start with the FA – A ⇒ 0A for the regular language. ⇒ 00A – ⇒ 001B – 0 1 0 ⇒ 0011B – 1 A B C ⇒ 00110C – 1 0

  7. Regular Grammars Regular Grammars • Looks suspiciously like a grammar • Let’s Formalize this a bit derivation where – Given an FA M = ( Q , Σ , q o , δ , A ) – CFG Variables ↔ FA States – Define a CFG G = (V, T, S, P) – CFG Productions ↔ FA Transitions • V = Q • S = q 0 – CFG Start Variable ↔ FA Start State • T = Σ Regular Grammars Regular Grammars • Productions • Note that all productions are of the form – Normal – A → aB • Define a production – A → a – D → aB for each transition from D to B on input a – I.e. when δ (D, a) = B – Final State • Such a grammar is called a regular grammar • D → a for each transition to a final state • I.e when δ (D, a) = C and C ∈ A Regular Grammars Regular Grammars • Regular Grammars accept the class of • Example regular languages 0 – L is regular iff there is a regular grammar that C accepts it. 0 – This regular grammar is the one we just 0 1 constructed. A 1 1 B

Recommend


More recommend