Context Free Languages II Announcement Announcement
Total Page:16
File Type:pdf, Size:1020Kb
Announcement • About homework #4 Context Free Languages II – The FA corresponding to the RE given in class may indeed already be minimal. – Non-regular language proofs CFLs and Regular Languages • Use Pumping Lemma • Homework session after lecture Announcement Before we begin • Note about homework #2 • Questions from last time? – For a deterministic FA, there must be a transition for every character from every state. Languages CFLs and Regular Languages • Recall. • Venn diagram of languages – What is a language? Is there – What is a class of languages? something Regular Languages out here? CFLs Finite Languages 1 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: Σ, S, P) where – Means for defining: Context Free Grammar • V is a set of variables Σ – Machine for accepting: Pushdown Automata • is a set of terminals • V and Σ are disjoint (I.e. V ∩Σ= ∅) •S ∈V, is your start symbol Context Free Grammars Context Free Grammars • Let’s formalize this a bit: • Let’s formalize this a bit: – Production rules – Production rules →β •Of the form A where • We say that the grammar is context-free since this ∈ –A V substitution can take place regardless of where A is. – β∈(V ∪∑)* string with symbols from V and ∑ α⇒* γ γ α • We say that γ can be derived from α in one step: • We write if can be derived from in zero –A →βis a rule or more steps. α α α – = 1A 2 γ α βα – = 1 2 – α⇒γ Context Free Grammars CFLs and Regular Languages • The language generated by a CFG • Venn diagram of languages – Let G = (V, Σ, S, P) – The language generated by G, L(G) Regular Languages • L(G) = { x ∈Σ* | S ⇒* x} Context Free Languages – A language L is a Context Free Language Finite (CFL) iff there is a CFG G, such that Languages •L = L(G) Is there something in here? 2 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: –G = (V , Σ, S , P ) - If L and L are CFLs then 1 1 1 1 1 2 –G = (V , Σ, S , P ) such that -L∪ L is a CFL 2 2 2 2 1 2 –L(G) = L and L(G ) = L -LL is a CFL 1 1 2 2 1 2 ∩ ∅ * – Assume that V1 V2 = -L1 is a CFL - With the above shown, showing every Regular • We will define: –G = (V , Σ, 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, , Sc, Pc) such that L(Gc) = L1 L2 Σ * –Gk = (Vk, , Sk, Pk) such that L(Gc) = L1 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, , 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, , 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 3 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, , 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 {Sc S1Sc | } Λ can be derived from the start variable 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 r1 and r2 then ∪ – 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. 4 Union, Concatenation, and Kleene Star of CFLs CFLs and Regular Languages •RE -> CFG • Venn diagram 2 • Assume R1 and R2 are regular expressions that Regular Languages describe languages L1 and L2. Then, by the induction hypothesis, L and L are CFLs and as 1 2 Context Free such there are CFGs that describe L and L 1 2 Languages • Create CFGs that describe the the languages: Finite –L∪ L 1 2 Languages –L1 L2 * –L1 • Which we just did…We are done! Is there something in here? NO CFLs and Regular Languages CFLs and Regular Languages • Venn diagram 3 • What have we learned? Context Free Languages – CFLs are closed under union, concatenation, and Kleene Star Regular Languages – Every Regular Language is also a CFL Finite – We now have an algorithm, given a Regular Languages Expression, to construct a CGF that describes 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)* • (001 + 1) can be described by the CFG with • (01) can be described by the CFG with productions: productions: –D → 01 –A → 011 | 1 * • (001 + 1)* can be described by the CFG with • (01) can be described by the CFG with productions: productions: –C → DC | Λ –B → AB | Λ –D → 01 –A → 011 | 1 5 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 • (011 + 1)*(01)* can be described by the CFG with productions: – Example: • Find a CFL for L = {0i1j0k | j > i + k} –S → BC – Number of 1s is greater than the combined number of 0s –B → AB | Λ –A → 011 | 1 –C → DC | Λ – This language can be expressed as –D → 01 »L = {0i1i 1m 1k0k | m > 0} – Questions? Union, Concatenation, and Kleene Star of CFLs Union, Concatenation, and Kleene Star of CFLs – Example: –Example i i ≥ i j k •CFG for L1 = {0 1 | i 0} • Find a CFL for L = {0 1 0 | j > i + k} –A → 0A1 | Λ – This language can be expressed as m •CFG for L2 = {1 | m > 0} »L = {0i1i 1m 1k0k | m > 0} –B → 1B | 1 k k ≥ – This is concatenation of 3 languages L1L2L3 where •CFG for L3 = {1 0 | k 0} i i ≥ –C → 1C0 | Λ »L1 = {0 1 | i 0} m –CFG for L »L2 = {1 | m > 0} k k ≥ •S → ABC »L3 = {1 0 | k 0} •A → 0A1 | Λ •B → 1B | 1 •C → 1C0 | Λ Union, Concatenation, and Kleene Star of CFLs CFLs and Regular Languages •Example • Questions? –Formally • G = (V, Σ, S, P) where – V = {S, A, B, C} – Σ = {0, 1} –P = {S → ABC A → 0A1 | Λ B → 1B | 1 C → 1C0 | Λ} 6 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 ⇒ 0 1 – 001B 0 – ⇒ 0011B A 1 B C 1 – ⇒ 00110C 0 Regular Grammars Regular Grammars • Looks suspiciously like a grammar • Let’s Formalize this a bit Σ δ derivation where – Given an FA M = (Q, , qo, , A) –CFG Variables ↔ FA States – Define a CFG G = (V, Σ, S, P) – CFG Productions ↔ FA Transitions •V = Q – CFG Start Variable ↔ FA Start State •S = q0 • Σ = Σ 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 •D → a for each transition to a final state • Such a grammar is called a regular grammar • I.e when δ (D, a) = C and C ∈A 7 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 constructed.