Cfls and Regular Languages Homework Schedule Mangling
Total Page:16
File Type:pdf, Size:1020Kb
Homework • Assignment #6 (Due 10/26) – From textbook CFLs and Regular Languages • Exercise 6.2.1 b,c / 6.2.2 b (use JFLAP) • Exercise 6.3.2 (by empty stack or final state) • Exercise 6.3.4 • Exercise 7.1.5 – Other problems • Define CFGs that generate the following langauges: –{aibjck | j = i + k } –{aibjck | j = i or j = k } Schedule mangling Before we begin • The schedule on the Web has been adjusted • Questions from last time? – This week: Chapter 7 • Finish Context Free Languages – Next week • Start Turing Machines Languages Context Free Languages • Future Exam Question • Context Free Languages(CFL) is the next – What is a language? class of languages outside of Regular – What is a class of languages? Languages: – Means for defining: Context Free Grammar – Machine for accepting: Pushdown Automata 1 Context Free Grammars Context Free Grammars • Let’s formalize this a bit: • Let’s formalize this a bit: – A context free grammar (CFG) is a 4-tuple: (V, – Production rules T, S, P) where •Of the form A →βwhere –A ∈V • V is a set of variables – β∈(V ∪∑)* string with symbols from V and ∑ • T is a set of terminals • We say that γ can be derived from α in one step: • V and Σ are disjoint (I.e. V ∩Σ= ∅) –A →βis a rule – α = α A α •S ∈V, is your start symbol 1 2 – γ = α1 βα2 – α⇒γ Now our picture looks like CFLs and Regular Languages Context Free Languages • We showed last time that every RL is also a CFL – Since a NFA can simply be described as a PDA that Deterministic Context Free Languages doesn’t use the stack. • We can also show by only using the structural Regular Languages representations of the language classes Finite – I.e. Regular Expressions / Context Free Grammars Languages – That is what we will do in this first half. • Note: Much of this lecture is not in the text! CFLs and Regular Languages Union, Concatenation, and Kleene Star of CFLs • Will show that all Regular Languages are • Formally, Let L1 and L2 be CFLs. Then there CFLs exists CFGs: –G1 = (V1, T, S1, P1) - If L1 and L2 are CFLs then –G2 = (V2, T, S2, P2) such that -L1 ∪ L2 is a CFL –L(G1) = L1 and L(G2) = L2 -L1 L2 is a CFL * – Assume that V1 ∩ V2 = ∅ -L1 is a CFL - With the above shown, showing every Regular • We will define: –G = (V , T, S , P ) such that L(G ) = L ∪ L Language is also a CFL can be shown using a u u u u u 1 2 basic inductive proof. –Gc = (Vc, T, Sc, Pc) such that L(Gc) = L1 L2 * –Gk = (Vk, T, Sk, Pk) such that L(Gc) = L1 2 Union, Concatenation, and Kleene Star of CFLs Union, Concatenation, and Kleene Star of CFLs • Union • Union – Basic Idea –Formally • Define the new CFG so that we can either •Gu = (Vu, T, Su,Pu) – start with the start variable of G1 and follow the –Vu = V1 ∪ V2 ∪ {Su} production rules of G or 1 –Su = Su – start with the start variable of G and follow the 2 –Pu = P1 ∪ P2 ∪ {Su → S1 | S2 } production rules of G2 – The first case will derive a string in L1 – The second case will derive a string in L2 Union, Concatenation, and Kleene Star of CFLs Union, Concatenation, and Kleene Star of CFLs • Concatenation • Concatenation – General Idea –Formally • Define the new CFG so that •Gc = (Vc, T, Sc, Pc) – We force a derivation staring from the start variable of G1 –Vc = V1 ∪ V2 ∪ {Sc} using the rules of G 1 –Sc = Sc – After that… –Pu = P1 ∪ P2 ∪ {Sc → S1S2 } – We force a derivation staring from the start variable of G2 using the rules of G2 Union, Concatenation, and Kleene Star of CFLs Union, Concatenation, and Kleene Star of CFLs • Kleene Star • Kleene star – General Idea –Formally • Define the new CFG so that •Gk = (Vk, T, Sk, Pk) – We can repeatedly concatenate derivations of strings in L1 –Vk = V1 ∪ {Sk} * • Since L contains ε, we must be careful to assure –Sk = Sk that there are productions in our new CFG such that –Pk = P1 ∪ {Sk → S1Sk | ε } ε can be derived from the start variable 3 CFLs and Regular Languages Regular Expression • Now we can complete the proof • Recursive definition of regular languages / – Use an inductive proof expression over Σ : 1. ∅ is a regular language and its regular expression is ∅ 2. {ε} is a regular language and ε is its regular expression 3. For each a ∈ Σ, {a} is a regular language and its regular expression is a Regular Expression CFLs and Regular Languages 4. If L1 and L2 are regular languages with regular •RE -> CFG expressions r and r then 1 2 – Base cases -- L1 ∪ L2 is a regular language with regular expression 1. ∅ can be expressed as a CFG with no productions (r1 + r2) -- L1L2 is a regular language with regular expression 2. {ε} can be expressed by a CFG with the (r1r2) single production S →ε * * -- L1 is a regular language with regular expression (r1 ) 3. For each a ∈ Σ, {a} can be expressed by a CFG with the single production S → a Only languages obtainable by using rules 1-4 are regular languages. Union, Concatenation, and Kleene Star of CFLs CFLs and Regular Languages •RE -> CFG • Assume R1 and R2 are regular expressions that Context Free Languages describe languages L1 and L2. Then, by the induction hypothesis, L1 and L2 are CFLs and as Regular Languages such there are CFGs that describe L1 and L2 • Create CFGs that describe the the languages: Finite –L1 ∪ L2 Languages –L1 L2 * –L1 • Which we just did…We are done! 4 CFLs and Regular Languages CFLs and Regular Languages • What have we learned? •Example – CFLs are closed under union, concatenation, – Find a CFG for the L = (011 + 1)*(01)* and Kleene Star • (011 + 1) can be described by the CFG with – Every Regular Language is also a CFL productions: – We now have an algorithm, given a Regular –A → 011 | 1 • (011 + 1)* can be described by the CFG with Expression, to construct a CGF that describes productions: the same language –B → AB | ε –A → 011 | 1 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)* – Putting it all together • (011 + 1)*(01)* can be described by the CFG with productions: • (01) can be described by the CFG with productions: –D → 01 –S → BC • (01)* can be described by the CFG with productions: –B → AB | ε –A → 011 | 1 –C → DC | ε –C → DC | ε –D → 01 –D → 01 – Questions? Union, Concatenation, and Kleene Star of CFLs Union, Concatenation, and Kleene Star of CFLs • You can use proof of closure properties in – Example: building CFLs: • Find a CFL for L = {0i1j0k | j > i + k} – This language can be expressed as – Example: »L = {0i1i 1m 1k0k | m > 0} i j k • Find a CFL for L = {0 1 0 | j > i + k} – This is concatenation of 3 languages L1L2L3 where – Number of 1s is greater than the combined number of 0s i i »L1 = {0 1 | i ≥ 0} m »L2 = {1 | m > 0} – This language can be expressed as k k »L3 = {1 0 | k ≥ 0} »L = {0i1i 1m 1k0k | m > 0} 5 Union, Concatenation, and Kleene Star of CFLs Union, Concatenation, and Kleene Star of CFLs –Example •Example i i •CFG for L1 = {0 1 | i ≥ 0} –A → 0A1 | ε –Formally •CFG for L= {1m | m > 0} 2 • G = (V, T, S, P) where –B → 1B | 1 k k – V = {S, A, B, C} •CFG for L3 = {1 0 | k ≥ 0} –C → 1C0 | ε – Σ = {0, 1} –CFG for L –P = {S → ABC •S → ABC A → 0A1 | ε •A → 0A1 | ε B → 1B | 1 •B → 1B | 1 C → 1C0 | ε} •C → 1C0 | ε CFLs and Regular Languages Regular Grammars • Questions? • Another means to show that all Regular Languages are CFL is to start with the FA for the regular language. 0 1 0 A 1 B C 1 0 Regular Grammars Regular Grammars • Sequence of states to accept 00110 • Looks suspiciously like a grammar –A ⇒ 0A derivation where – ⇒ 00A –CFG Variables ↔ FA States – ⇒ 001B – CFG Productions ↔ FA Transitions – ⇒ 0011B – CFG Start Variable ↔ FA Start State – ⇒ 00110C 6 Regular Grammars Regular Grammars • Let’s Formalize this a bit • Productions – Normal – Given an FA M = (Q, Σ, qo, δ, A) – Define a CFG G = (V, T, S, P) • Define a production –D → aB for each transition from D to B on input a •V = Q – I.e. when δ (D, a) = B •S = q0 •T = Σ – Final State •D → a for each transition to a final state • I.e when δ (D, a) = C and C ∈A Regular Grammars Regular Grammars • Note that all productions are of the form • Regular Grammars accept the class of –A → aB regular languages –A → a – L is regular iff there is a regular grammar that accepts it. – This regular grammar is the one we just constructed. • Such a grammar is called a regular grammar Regular Grammars Regular Grammars •Example • G = (V, T, S, P) – V = {A, B, C} 0 C –S = A 0 – T = {0,1} A 01 1 1 B 7 Regular Grammars Regular Grammars • Productions P • Derivation using the CFL for 0101 – Regular –A ⇒ 0A •A → 0A B→ 1A – ⇒ 01B •A → 1B C → 0A – ⇒ 010C •B → 0C C → 1B – ⇒ 0101 –Final •A → 1 •C → 1 • Questions? Regular Grammars Summary • Means of specifying a regular language • Context Free Languages 1.