Formal Language and Automata Theory (CS21004)
Total Page:16
File Type:pdf, Size:1020Kb
Context Free Grammar Normal Forms Derivations and Ambiguities Pumping lemma for CFLs PDA Parsing CFL Properties Formal Language and Automata Theory (CS21004) Soumyajit Dey CSE, IIT Kharagpur Context Free Formal Language and Automata Theory Grammar (CS21004) Normal Forms Derivations and Ambiguities Pumping lemma Soumyajit Dey for CFLs CSE, IIT Kharagpur PDA Parsing CFL Properties DPDA, DCFL Membership CSL Soumyajit Dey CSE, IIT Kharagpur Formal Language and Automata Theory (CS21004) Context Free Grammar Normal Forms Derivations and Ambiguities Pumping lemma for CFLs PDA Parsing CFL Properties Formal Language and Automata Announcements Theory (CS21004) Soumyajit Dey CSE, IIT Kharagpur Context Free Grammar Normal Forms Derivations and The slide is just a short summary Ambiguities Follow the discussion and the boardwork Pumping lemma for CFLs Solve problems (apart from those we dish out in class) PDA Parsing CFL Properties DPDA, DCFL Membership CSL Soumyajit Dey CSE, IIT Kharagpur Formal Language and Automata Theory (CS21004) Context Free Grammar Normal Forms Derivations and Ambiguities Pumping lemma for CFLs PDA Parsing CFL Properties Formal Language and Automata Table of Contents Theory (CS21004) Soumyajit Dey 1 Context Free Grammar CSE, IIT Kharagpur 2 Normal Forms Context Free Grammar 3 Derivations and Ambiguities Normal Forms 4 Derivations and Pumping lemma for CFLs Ambiguities 5 Pumping lemma PDA for CFLs 6 Parsing PDA Parsing 7 CFL Properties CFL Properties DPDA, DCFL 8 DPDA, DCFL Membership 9 Membership CSL 10 CSL Soumyajit Dey CSE, IIT Kharagpur Formal Language and Automata Theory (CS21004) Context Free Grammar Normal Forms Derivations and Ambiguities Pumping lemma for CFLs PDA Parsing CFL Properties Formal Language and Automata Theory (CS21004) hstmti |= hif-stmti | hwhile-stmti | hbegin-stmti Soumyajit Dey CSE, IIT h...i |= | hassg-stmti Kharagpur hif-stmti |= if hbool-expri then hstmti else hstmti Context Free hwhile-stmti |= while hbool-expri do hstmti Grammar Normal Forms hbegin-stmti |= begin hstmt-listi end Derivations and hstmt-listi |= hstmti | hstmti ; hstmt-listi Ambiguities Pumping lemma hassg-stmti |= hvari for CFLs hvari |= harith-expri PDA Parsing hbool-expri |= harith-exprihcompare-opiharith-expri CFL Properties hcompare-opi |= < | > |≤|≥= | 6= DPDA, DCFL harith-expri |= hvari | hconsti | harith-expriharith-opiharith-exprMembershipi CSL harith-opi |= + |−|∗| / hconsti |= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 hconsti |= a | b | c | ··· | x | y | z Soumyajit Dey CSE, IIT Kharagpur Formal Language and Automata Theory (CS21004) Context Free Grammar Normal Forms Derivations and Ambiguities Pumping lemma for CFLs PDA Parsing CFL Properties Formal Language and Automata Theory (CS21004) For the string Soumyajit Dey CSE, IIT while x ≤ y do begin x = x + 1; y = y − 1 end Kharagpur The first few sentential forms in its derivation are Context Free hstmti Grammar hwhile-stmti Normal Forms Derivations and while hbool-stmti do hstmti Ambiguities while harith-exprihcompare-opiharith-expri do hstmti Pumping lemma while hvarihcompare-opiharith-expri do hstmti for CFLs while hvari ≤ harith-expri do hstmti PDA Parsing while hvari ≤ hvari do hstmti CFL Properties while hxi ≤ hvari do hstmti DPDA, DCFL while hxi ≤ hyi do hstmti Membership while hxi ≤ hyi do hbegin-stmti CSL Soumyajit Dey CSE, IIT Kharagpur Formal Language and Automata Theory (CS21004) Context Free Grammar Normal Forms Derivations and Ambiguities Pumping lemma for CFLs PDA Parsing CFL Properties Formal Language and Automata Theory (CS21004) Soumyajit Dey CSE, IIT Kharagpur Formally, a context-free grammar (CFG) is a quadruple G = (N, Σ, P, S) Context Free Grammar where Normal Forms Derivations and N is a finite set (the non-terminal symbols), Ambiguities Σ is a finite set (the terminal symbols) disjoint from N, Pumping lemma ∗ for CFLs P is a finite subset of N × (N ∪ Σ) (the productions), PDA and Parsing S ∈ N (the start symbol) CFL Properties DPDA, DCFL Membership CSL Soumyajit Dey CSE, IIT Kharagpur Formal Language and Automata Theory (CS21004) Context Free Grammar Normal Forms Derivations and Ambiguities Pumping lemma for CFLs PDA Parsing CFL Properties Formal Language and Automata Theory (CS21004) Soumyajit Dey CSE, IIT Kharagpur n n {a b | n ≥ 0} is a CFL with CFG S → aSb | ǫ Context Free Grammar Palindromes over {a, b} is a CFL with CFG Normal Forms S → aSa | bSb | a | b | ǫ Derivations and Ambiguities Balanced parentheses over {(, )} is a CFL with CFG Pumping lemma S → (S) | SS | ǫ for CFLs PDA How do you prove that each grammar generates the Parsing corresponding language ?? CFL Properties DPDA, DCFL Membership CSL Soumyajit Dey CSE, IIT Kharagpur Formal Language and Automata Theory (CS21004) Context Free Grammar Normal Forms Derivations and Ambiguities Pumping lemma for CFLs PDA Parsing CFL Properties Formal Language and Automata Table of Contents Theory (CS21004) Soumyajit Dey 1 Context Free Grammar CSE, IIT Kharagpur 2 Normal Forms Context Free Grammar 3 Derivations and Ambiguities Normal Forms 4 Derivations and Pumping lemma for CFLs Ambiguities 5 Pumping lemma PDA for CFLs 6 Parsing PDA Parsing 7 CFL Properties CFL Properties DPDA, DCFL 8 DPDA, DCFL Membership 9 Membership CSL 10 CSL Soumyajit Dey CSE, IIT Kharagpur Formal Language and Automata Theory (CS21004) Context Free Grammar Normal Forms Derivations and Ambiguities Pumping lemma for CFLs PDA Parsing CFL Properties Formal Language and Automata Theory (CS21004) It helps to restrict formal grammars to ‘standard forms’. You Soumyajit Dey CSE, IIT can write algorithms that work on grammars assuming this Kharagpur standard Context Free A CFG is in Chomsky Normal Form (CNF) if all Grammar productions are of the form Normal Forms 1 A → BC or Derivations and Ambiguities 2 A → a Pumping lemma where A, B, C ∈ N and a ∈ Σ. Note that CNF form for CFLs grammars cannot generate ǫ PDA For any CFG G, there is a CFG G ′ in Chomsky Normal Parsing CFL Properties Form such that DPDA, DCFL ′ Membership L(G )= L(G) − {ǫ} CSL Soumyajit Dey CSE, IIT Kharagpur Formal Language and Automata Theory (CS21004) Context Free Grammar Normal Forms Derivations and Ambiguities Pumping lemma for CFLs PDA Parsing CFL Properties Formal Language and Automata Create grammar for ǫ free language Theory (CS21004) Soumyajit Dey CSE, IIT Kharagpur ′ For any CFG G = (N, Σ, P, S), there is a CFG G with Context Free no ǫ or unit productions (A → B) such that Grammar Normal Forms ′ L(G )= L(G) − {ǫ} Derivations and Ambiguities Pumping lemma . for CFLs Proof: Let Pˆ be the smallest set of productions PDA containing P and closed under the rules Parsing 1 if A → αBβ and B → ǫ are in Pˆ; then A → αβ is in Pˆ CFL Properties 2 if A → B and B → γ are in Pˆ, then A → γ is in Pˆ DPDA, DCFL Membership Point to note : α, β, γ ∈ (N ∪ Σ)∗ CSL Soumyajit Dey CSE, IIT Kharagpur Formal Language and Automata Theory (CS21004) Context Free Grammar Normal Forms Derivations and Ambiguities Pumping lemma for CFLs PDA Parsing CFL Properties Formal Language and Automata Create grammar for ǫ free language Theory (CS21004) Soumyajit Dey CSE, IIT Kharagpur Context Free Grammar Note that for the language Gˆ = (N, Σ, Pˆ, S), L(G)= L(Gˆ) Normal Forms Derivations and L(G) ⊆ L(Gˆ) since P ⊆ Pˆ Ambiguities ˆ ˆ Pumping lemma L(G)= L(G) : P only contains those rules as extra for CFLs over P which are simulatable in two steps by existing PDA rules of P Parsing CFL Properties DPDA, DCFL Membership CSL Soumyajit Dey CSE, IIT Kharagpur Formal Language and Automata Theory (CS21004) Context Free Grammar Normal Forms Derivations and Ambiguities Pumping lemma for CFLs PDA Parsing CFL Properties Formal Language and Automata Create grammar for ǫ free language Theory (CS21004) Soumyajit Dey CSE, IIT Kharagpur Context Free Grammar Can prove that, for any x 6= ǫ, ∃ a derivation S ⇒ x without Normal Forms ˆ G Derivations and ǫ and unit productions Ambiguities Pumping lemma Remove ǫ and unit productions from Gˆ for CFLs ′ ′ ′ Create G = (N, Σ, P , S) where P is same as Pˆ but w/o ǫ PDA and unit productions Parsing CFL Properties DPDA, DCFL Membership CSL Soumyajit Dey CSE, IIT Kharagpur Formal Language and Automata Theory (CS21004) Context Free Grammar Normal Forms Derivations and Ambiguities Pumping lemma for CFLs PDA Parsing CFL Properties Formal Language G ′ and Automata to CNF Theory (CS21004) Soumyajit Dey CSE, IIT Kharagpur Context Free For each terminal a ∈ Σ introduce a new nonterminal Grammar Normal Forms Aa and production Aa → a and replace all occurrences Derivations and of a on the right hand side of old productions (except Ambiguities productions of the form B → a) with Aa Pumping lemma for CFLs Now all productions are of the form PDA 1 A → a or Parsing 2 1 2 A → B B ··· Bk , k ≥ 2 CFL Properties where the Bi are nonterminals. DPDA, DCFL Membership CSL Soumyajit Dey CSE, IIT Kharagpur Formal Language and Automata Theory (CS21004) Context Free Grammar Normal Forms Derivations and Ambiguities Pumping lemma for CFLs PDA Parsing CFL Properties Formal Language G ′ and Automata to CNF Theory (CS21004) Soumyajit Dey CSE, IIT Kharagpur Context Free For any production Grammar Normal Forms A → B1B2 · · · Bk Derivations and Ambiguities Pumping lemma with k ≥ 3, introduce a new nonterminal C and replace for CFLs this production with the two productions PDA 1 A → B1C and Parsing 2 C → B2B3 ··· Bk CFL Properties until all right hand sides are of length at most 2. DPDA, DCFL Membership CSL Soumyajit Dey CSE, IIT Kharagpur Formal Language and Automata Theory (CS21004) Context Free Grammar Normal Forms Derivations and Ambiguities Pumping lemma for CFLs PDA Parsing CFL Properties Formal Language and Automata Theory (CS21004) Derive a CNF grammar for Soumyajit Dey CSE, IIT {anbn|n ≥ 0} − {ǫ} = {anbn|n ≥ 1} Kharagpur Context Free Starting with the grammar Grammar Normal Forms S → aSb|ǫ Derivations and Ambiguities Pumping lemma for CFLs 1 Remove ǫ productions to get S → aSB|ab PDA 2 Add non terminals A,B and replace the productions Parsing with S → ASB|AB, A → a, B → b.