Context Free Grammar Normal Forms Derivations and Ambiguities Pumping lemma for CFLs PDA Parsing CFL Properties

Formal Language and (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free 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. CFL Properties 3 Add a nonterminal C and replace S → ASB with DPDA, DCFL

S → AC and C → SB. Membership The final grammar in CNF is CSL S → AB|AC, C → SB, A → a, B → b.

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 Derive a CNF grammar for the set of non-null strings of CSE, IIT balanced parenthesis [ ] Kharagpur

Starting with the grammar Context Free Grammar S → [S]|SS|ǫ Normal Forms Derivations and Ambiguities

Pumping lemma 1 Remove ǫ productions to get S → [S]|SS|[] for CFLs 2 Add non terminals A,B and replace the productions PDA with S → SS|ASB|AB, A → [, B →]. Parsing 3 Add a nonterminal C and replace S → ASB with CFL Properties S → AC and C → SB. DPDA, DCFL 4 The final grammar in CNF is Membership S → SS|AB|AC, C → SB, A → [, B →]. 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) Consider the grammar Soumyajit Dey CSE, IIT G = ({S, A, B, C}, {a, b, c}, S, P) where Kharagpur

P = {S → ABC, A → aA, A → ǫ, B → bB, B → ǫ, Context Free C → cC, C → ǫ}. Grammar Normal Forms With this grammar, there is a choice of variables to Derivations and expand. If we always expanded the leftmost variable Ambiguities first, we would have a leftmost derivation: Pumping lemma for CFLs

S → ABC → aABC → aBC → abBC → abbBC → PDA

abbC → abbcC → abbc Parsing Conversely, if we always expanded the rightmost CFL Properties variable first, we would have a rightmost derivation: DPDA, DCFL S → ABC → ABcC → ABc → AbBc → AbbBc → Membership Abbc → aAbbc → abbc 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 A grammar G is ambiguous if ∃w ∈ L(G) for which Context Free there are two or more distinct derivation/parse trees, or Grammar

there are two or more distinct leftmost derivations, or Normal Forms

there are two or more distinct rightmost derivations. Derivations and Ambiguity is a property of a grammar, and it is usually Ambiguities Pumping lemma (but not always) possible to find an equivalent for CFLs unambiguous grammar. PDA An inherently ambiguous language is a language for Parsing which no unambiguous grammar exists. 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 Consider S → AS|ǫ, A → A1|0A1|01. The string 00111 CSE, IIT has the following two leftmost derivations from S: Kharagpur

1 S → AS → 0A1S → 0A11S → 00111S → 00111 Context Free 2 S → AS → A1S → 0A11S → 00111S → 00111 Grammar Normal Forms Intuitively, we can use A → A1 first or second to Derivations and generate the extra 1. The language of our example Ambiguities Pumping lemma grammar is not inherently ambiguous, even though the for CFLs

grammar is ambiguous. PDA Change the grammar to force the extra 1’s to be Parsing generated last. S → AS|ǫ, A → 0A1|B, B → B1|01 CFL Properties {ai bj ck |i = j or j = k} is an inherently ambiguous DPDA, DCFL language 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)

Soumyajit Dey CSE, IIT Kharagpur

For every CFL A, ∃k ≥ 0 such that ∀z ∈ A with |z|≥ k Context Free Grammar ∃u, v, w, x, y such that Normal Forms z = uvwxy Derivations and Ambiguities

vx 6= ǫ Pumping lemma |vwx|≤ k for CFLs PDA i i ∀i ≥ 0, uv wx y ∈ A 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 Theory (CS21004)

Soumyajit Dey CSE, IIT Intuitive (informal) idea : Kharagpur The set of a grows in one direction per Context Free production : standard form is strictly right/left linear Grammar A Context Free language grows in two directions per Normal Forms Derivations and production : Consider as standard form the CNF Ambiguities representation Pumping lemma for CFLs

For sufficiently long strings, non terminals will repeat PDA The subtree under the higher nonterminal instance can Parsing be copied for the lower instance CFL Properties DPDA, DCFL This creates two separate possibilities of pumping 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

n n Formal Language CNF grammar for {a b |n ≥ 1} : and Automata S → AB|AC, C → SB, A → a, B → b. Theory (CS21004) Soumyajit Dey CSE, IIT S Kharagpur  C Context Free a S B Grammar Normal Forms

 C b Derivations and Ambiguities a S B Pumping lemma for CFLs b  C PDA

Parsing a SB CFL Properties

 B b DPDA, DCFL

Membership a b CSL Figure: derivation tree for a4b4 :

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 Map the tree to uniform depth in all branches Theory (CS21004)

S Soumyajit Dey CSE, IIT  C Kharagpur

a S B Context Free Grammar

a  C b Normal Forms Derivations and a a S B b Ambiguities Pumping lemma a a  C b b for CFLs

a PDA a a SB b b Parsing a a a  B b b b CFL Properties DPDA, DCFL a a a a b b b b Membership CSL Figure: derivation tree for a4b4 :

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) Consider a path with a repeating non-terminal (S here) Soumyajit Dey CSE, IIT S Kharagpur

 C Context Free Grammar a S B Normal Forms a  C b Derivations and Ambiguities

a a S B b Pumping lemma for CFLs

a a  C b b PDA

a Parsing a a SB b b CFL Properties

a a a  B b b b DPDA, DCFL

Membership

a a a a b b b b 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) Note the decomposition of the string Soumyajit Dey CSE, IIT S Kharagpur  C Context Free a S B Grammar

a  C b Normal Forms Derivations and a a S b B Ambiguities

a a  C b b Pumping lemma for CFLs a a a SB b b PDA

a a a  B b b b Parsing

CFL Properties a a a a b b b b DPDA, DCFL  

  u v Membership

 x y w 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) Check the subtree with two occurrences of S Soumyajit Dey CSE, IIT T S Kharagpur

Context Free A C Grammar

Normal Forms

Derivations and a S B Ambiguities

Pumping lemma for CFLs a AB b PDA

Parsing ab b a CFL Properties

 v  x 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) Pumping up Soumyajit Dey CSE, IIT S Kharagpur  C

a S B Context Free Grammar a  C b Normal Forms a a S B b Derivations and a a  C b b Ambiguities

a a a SB b b Pumping lemma for CFLs a a a  C b b b PDA

a a a a S B b b b Parsing

   u v a  B b x y  CFL Properties a a b b DPDA, DCFL   

x Membership  w 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) Pumping down Soumyajit Dey CSE, IIT S Kharagpur

 C Context Free a S B Grammar Normal Forms a  C b Derivations and a a S B b Ambiguities Pumping lemma a a  B b b for CFLs a a a b b b PDA Parsing a a b b

 CFL Properties a a w b b DPDA, DCFL

 u  Membership y 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 Proof Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Let G be a grammar for A in CNF. Context Free Grammar n+1 k = 2 , where n is the number of nonterminals of G Normal Forms Suppose z ∈ A and |z|≥ k Derivations and Ambiguities

Any parse tree in G for z must be of depth n + 1 Pumping lemma for CFLs The longest path in the tree is of length at least n + 1 PDA

It must contain at least n + 1 occurrences of Parsing nonterminals. CFL Properties By pigeonhole principle, some nonterminal must occur DPDA, DCFL more than once 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 Say X is the nonterminal that appears more than once Kharagpur

Break z up into substrings uvwxy such that Context Free Grammar 1 w is the string generated by the lower occurrence of X Normal Forms 2 vwx is the string generated by the upper occurrence Derivations and Let T and t be the subtree rooted at the upper and Ambiguities Pumping lemma lower ocurrence of X respectively for CFLs Replacing t with T once we get a valid subtree of PDA 2 2 uv wx y Parsing Repeat it to produce uv i wxi y, i ≥ 1 CFL Properties 0 0 DPDA, DCFL Replace T with t to get uv wx y 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

Context Free Note that vx 6= ǫ; otherwise T = t Grammar Also |vwx|≤ k as we chose the first repeated Normal Forms Derivations and occurrence of a nonterminal from the bottom. Ambiguities In the longest path, the depth of the subtree under the Pumping lemma for CFLs upper occurrence of X is at most n + 1 PDA n+1 So it cannot have more than 2 = k terminals. 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 A anbnan n and Automata = { | ≥ 0} is not context free Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Adversary picks k in step 1 Context Free Grammar k k k You pick z = a b a such that z ∈ A and |z| = 3k ≥ k Normal Forms Adversary picks z = uvwxy such that vx 6= ǫ, and Derivations and Ambiguities |vwx|≤ k Pumping lemma You pick i = 2 and in all cases you can ensure for CFLs uv 2wx2y 6∈ A PDA 1 either v or x contain at least one a and at least one b Parsing 2 v and x contains only a’s or only b’s CFL Properties 3 one of v or x contains only a’s and the other contains DPDA, DCFL only b’s 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 A ww w a b ∗ and Automata = { | ∈{ , } } is not context free Theory (CS21004)

Soumyajit Dey CSE, IIT As CFLs are closed under intersection with regular sets, Kharagpur it suffices to show that Context Free Grammar ′ ∗ ∗ ∗ ∗ n m n m A = A ∩ L(a b a b )= {a b a b |m, n ≥ 0} Normal Forms

Derivations and is not context-free Ambiguities Pumping lemma Adversary picks k in step 1 for CFLs You pick z = ak bk ak bk such that z ∈ A′ and |z|≥ k. PDA Parsing k k call each of the four substrings of the form a or b as CFL Properties

blocks DPDA, DCFL Adversary picks z = uvwxy such that vx 6= ǫ, and Membership |vwx|≤ k CSL You pick i = 2 and in all cases you can win

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 If one of v or x contains both a’s and b’s then Kharagpur uv 2wx2y 6∈ A′ 2 2 Context Free If v and x are from the same block, then uv wx y has Grammar one block longer than the other three Normal Forms Derivations and If v and x are on different blocks, then the blocks must Ambiguities be adjacent; otherwise |vwx| would be greater than k. Pumping lemma for CFLs Thus one of the blocks containing v or x must be a PDA

block of a’s and the other a block of b’s. Then Parsing 2 2 uv wx y either has two blocks of a’s of different size (if CFL Properties

vx contains an a) or two blocks of b’s of different size DPDA, DCFL 2 2 ′ (if vx contains a b) or both. Either way uv wx y 6∈ A . 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)

Soumyajit Dey input tape CSE, IIT a1 a2 a3 a4 a5 a6 a7 a8 ::: an Kharagpur Context Free left to right, read only Grammar Normal Forms

Derivations and Ambiguities

Q Pumping lemma push / pop A for CFLs fi PDA nite control B stack C Parsing B CFL Properties ? DPDA, DCFL Membership

CSL Figure: Non deterministic Pushdown Automata

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 A non deterministic PDA is a 7 tuple Theory (CS21004) M = (Q, Σ, Γ, δ, s, ⊥, F ), Soumyajit Dey CSE, IIT Q is a finite set (the states), Kharagpur Σ is a finite set (the input alphabet), Context Free Grammar

Γ is a finite set (the stack alphabets), Normal Forms s ∈ Q (the start state), Derivations and Ambiguities

⊥∈ Γ (the initial stack symbol), and Pumping lemma for CFLs

F ⊆ Q(the final or accept states), PDA ∗ δ ⊆ (Q × (Σ ∪ ε) × Γ) × (Q × Γ ), δ : NPDA has ǫ Parsing transitions (can move w/o input) CFL Properties DPDA, DCFL ((p, a, A), (q, B1 · · · Bk )) ∈ δ: from a state p while reading some input symbol a with some stack top element A, the Membership CSL PDA moves to another state q, pops A, pushes B1 · · · Bk (Bk first)

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

((p, a, A), (q, B1 · · · Bk )) ∈ δ: from a state p while Context Free reading some input symbol a with some stack top Grammar element A, the PDA moves to another state q, pops A, Normal Forms Derivations and pushes B1 · · · Bk (Bk first) Ambiguities ((p, ǫ, A), (q, B1 · · · B )) ∈ δ: from a state p with some Pumping lemma k for CFLs

stack top element A, the PDA moves to another state PDA

q, pops A, pushes B1 · · · Bk (Bk first) w/o reading any Parsing

input 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) a b a b b a a a b b a Soumyajit Dey CSE, IIT Kharagpur

Context Free Grammar

Normal Forms p Derivations and A Ambiguities Pumping lemma B for CFLs A PDA C Parsing ? CFL Properties DPDA, DCFL

Membership

Figure: Example of Non deterministic Pushdown Automata CSL

Config : (p, baaabba, ABAC⊥) : (state, unread tape, stack content)

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) The start configuration on input x is (s, x, ⊥). That is, Soumyajit Dey CSE, IIT the machine always starts in its start state s with its Kharagpur

read head pointing to the leftmost input symbol and Context Free the stack containing only the symbol ⊥. Grammar 1 Normal Forms The next configuration relation → describes how the Derivations and M Ambiguities machine can move from one configuration to another in Pumping lemma one step. for CFLs If ((p, a, A), (q, γ)) ∈ δ, then for any y ∈ Σ∗ and PDA 1 Parsing β ∈ Γ∗, (p, ay, Aβ) → (q, y, γβ); M CFL Properties and if ((p, ε, A), (q, γ)) ∈ δ,then for any y ∈ Σ∗ and DPDA, DCFL 1 Membership β ∈ Γ∗, (p, y, Aβ) → (q, y, γβ) M 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 Acceptance Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free ∗ Grammar by final state : (s, x, ⊥) → (q, ǫ, γ) : get to some M Normal Forms q ∈ F when string exhausted Derivations and ∗ Ambiguities by empty stack : (s, x, ⊥) → (q,ǫ,ǫ) : pop stack M Pumping lemma bottom element when string exhausted for CFLs PDA Both kinds of M/Cs can be converted to an equivalent Parsing NPDA M that has a single final state t and M can empty its CFL Properties stack after it enters state t. 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 PDA for balanced parenthesis Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Q = {q}, Context Free Grammar Σ= {[, ]}, Normal Forms

Γ= {⊥, [}, Derivations and Ambiguities start state = q Pumping lemma initial stack symbol =⊥, for CFLs PDA and let δ consist of the following transitions: Parsing 1 ((q, [, ⊥), (q, [⊥)); CFL Properties 2 ((q,[,[), (q,[[)); DPDA, DCFL 3 ((q, ], [), (q,ε)); Membership 4 ((q, ε, ⊥), (q,ε)). 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) Configuration Transition Soumyajit Dey CSE, IIT (q, [[[]][]][], ⊥) start configuration Kharagpur → (q, [[]][]][], [ ⊥) transition(1) Context Free → (q, []][]][], [[ ⊥) transition(2) Grammar → (q, ]][]][], [[[ ⊥) transition(2) Normal Forms Derivations and → (q, ][]][], [[ ⊥) transition(3) Ambiguities

→ (q, []][], [ ⊥) transition(3) Pumping lemma → (q, ]][], [[ ⊥) transition(2) for CFLs → (q, ] [ ], [ ⊥) transition(3) PDA Parsing → (q, [ ], ⊥) transition(3) CFL Properties → (q, ], [ ⊥) transition(1) DPDA, DCFL

→ (q, ǫ, ⊥) transition(2) Membership

→ (q, ǫ, ǫ) transition(4) CSL Design an NPDA for {a, b}∗ − {ww | w ∈ {a, b}∗}

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 CFG to NPDA Theory (CS21004)

Soumyajit Dey CSE, IIT All productions of G = (Σ, N, P, S) are of the form: Kharagpur A → cB1B2.....Bk , Where c ∈ Σ ∪ {ǫ} and k ≥ 0. An Context Free equivalent NPDA M with only one state that accepts by Grammar empty stack is M = ({q}, Σ, N, δ, q, S, Ø) where Normal Forms Derivations and q is the only state. Ambiguities Σ is the input alphabet of M Pumping lemma for CFLs

N is the set of stack alphabet of M PDA δ is the transition relation Parsing q is the start state CFL Properties DPDA, DCFL

S, is the start symbol of G, is the initial stack symbol of Membership

M CSL Ø, the null set, is the set of final states

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 Conversion Rule Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

For each production A → cB1B2.....Bk in P Context Free Grammar The transition relation δ is defined as Normal Forms ((q, c, A), (q, B1B2....Bk )) Derivations and 1 δ has one transition of each production of G Ambiguities 2 When in state q scanning input symbol c with A on top Pumping lemma for CFLs of the stack, pop A off the stack, push B1B2.....Bk onto PDA the stack (Bk first) and enter in q state Parsing 3 For c = ǫ, when in state q with A on top of the stack, without scanning an input symbol, pop A off the stack, CFL Properties DPDA, DCFL push B1B2.....Bk onto the stack (Bk first) and enter in q state 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 Example Conversion Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free Consider the set of nonnull balanced strings of parentheses [ Grammar

]. The list of production rules of the grammar and Normal Forms

corresponding transition of NPDA for this set are as follows: Derivations and Ambiguities 1 S → [BS ((q, [, S), (q, BS)) Pumping lemma for CFLs 2 S → [B ((q, [, S), (q, B)) PDA 3 S → [SB ((q, [, S), (q, SB)) Parsing 4 S → [SBS ((q, [, S), (q, SBS)) CFL Properties 5 B →] ((q, ], B), (q,ǫ)) 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 Example Derivation for the i/p ‘x = [ [ [ ] ] [ ] ]’ Theory (CS21004)

Soumyajit Dey CSE, IIT Rule Sententialforms ConfigurationofM Kharagpur applied inaleftmost inanaccepting Context Free derivation of x in G computation of M Grammar on input x Normal Forms Derivations and S (q,[[[]][]], S) Ambiguities Pumping lemma (3) [SB (q, [[]][]], SB) for CFLs

(4) [[SBSB (q, []][]], SBSB) PDA

(2) [[[BBSB (q, ]][]], BBSB) Parsing

(5) [[[]BSB (q, ][]], BSB) CFL Properties (5) [[[]]SB (q, []], SB) DPDA, DCFL (2) [[[]][BB (q, ]], BB) Membership (5) [ [ [ ] ] [ ]B (q, ], B) CSL (5) [[[]][]] (q, ǫ, ǫ)

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 Lemma: For any z, y ∈ Σ , γ ∈ N , and A ∈ N, A → zγ via Context Free G Grammar n a leftmost derivation iff (q, zy, A) → (q, y, γ) Normal Forms M Derivations and For example, in the fourth row of the table above, we Ambiguities wouldhavez=[[[,y=]][]], γ = BBSB, A = S Pumping lemma for CFLs

and n=3 PDA Proof: Try Yourself 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 M and Automata CFG converted to NPDA Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Language Equivalence : L(M) = L(G) Context Free Grammar

Proof: Normal Forms

Derivations and x ∈ L(G) Ambiguities ∗ Pumping lemma ⇔ S → x by a leftmost derivation. [definition of L(G)] for CFLs G ∗ PDA ⇔ (q, x, S) → (q,ǫ,ǫ) using the last Lemma Parsing M CFL Properties

⇔ x ∈ L(M) definition of L(M) 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 NPDA to CFG Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free Grammar Every NPDA can be simulated by an NPDA with one Normal Forms Derivations and state. Ambiguities Every NPDA with one state has an equivalent CFG Pumping lemma for CFLs Conclusion : With NPDA → CFG and CFG → NPDA we PDA have established that ‘NPDAs and CFGs are equivalent in Parsing expressive power’ 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 Single state NPDA to CFG Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free Grammar

Consider a NPDA M with one state that accepts by Normal Forms

empty stack Derivations and M = ({q}, Σ, Γ, δ, q, ⊥, Ø) Ambiguities Pumping lemma The equivalent CFG be G = (Γ, Σ, P, ⊥), where P for CFLs contains a production A → cB1B2...Bk for every PDA transition ((q, c, A), (q, B1B2...Bk )) ∈ δ Parsing CFL Properties

(the conversion we discussed is invertible). 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 Arbitrary NPDA to single state NPDA Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Consider a NPDA M = (Q, Σ, Γ, δ, s, ⊥, {t}) Context Free Grammar

M has a single final state t and M can empty its stack Normal Forms

after it enters state t. Derivations and def Ambiguities Let Γ′ = Q × Γ × Q. Elements of Γ′ are written hpAqi, Pumping lemma where p, q ∈ Q and A ∈ Γ for CFLs Equivalent NPDA M′ = ({∗}, Σ, Γ′, δ′, ∗, hs ⊥ ti, Ø) PDA ′ Parsing M has one state * and accepts by empty stack. CFL Properties ∗ ∗ (∗, x, pAq) → (∗,ǫ,ǫ) iff (p, x, A) → (q,ǫ,ǫ) DPDA, DCFL M′ M 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 For each transition ((p, c, A), (q0, B1B2...Bk )) ∈ δ, where c ∈ Σ ∪ {ǫ} Context Free Grammar ((∗, c, hpAqk i), (∗, hq0B1q1ihq1B2q2i....hqk−1Bk qk i)) ∈ Normal Forms ′ δ . For all possible choice of q1, q2, ....., qk . Derivations and ′ Ambiguities For k=0, ((∗, c, hpAq0i), (∗,ǫ)) ∈ δ iff Pumping lemma ((p, c, A), (q0,ǫ)) ∈ δ for CFLs PDA M′ nondeterministically guesses a computation sequence, Parsing

saves the guessed sequence in stack CFL Properties

verifies later in case of an actual run. 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 NPDA to CFG - another option Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Consider a PDA P = (Q, Σ, Γ, δ, q0, ⊥, {qF }) with the re- Context Free Grammar striction that every transition either pushes a symbol or pops a ′ Normal Forms symbol from the stack, i.e. δ(q, a, X ) contains either (q , γX ), Derivations and ′ Ambiguities γ ∈ Γ or (q ,ǫ). Consider the equivalent grammar GP = Pumping lemma (V , T , P, S) such that V = {Ap,q : p, q ∈ Q}, T = Σ, for CFLs

S = Aq0,qF and P has transitions of the following form: PDA ∀q ∈ Q (Aq,q → ǫ) ∈ P Parsing CFL Properties ∀p, q, r ∈ Q (Ap,q → Ap,r Ar,q) ∈ P DPDA, DCFL

(Ap,q → aAr,s b) ∈ P if δ(p, a,ǫ) contains (r, X) and Membership

δ(s, b, X ) contains (q,ǫ) 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 NPDA to CFG Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free Grammar ∗ Theorem: If Ap,q ⇒ x then x can bring the PDA P from Normal Forms state p on empty stack to state q on empty stack. Derivations and Ambiguities

Pumping lemma Proof: for CFLs We can prove this theorem by induction on the number of PDA Parsing steps in the derivation of x from Ap,q. 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 NPDA to CFG Theory (CS21004)

Soumyajit Dey ∗ CSE, IIT Base case: If Ap,q ⇒ x in one step, then the only rule to Kharagpur generate a terminal string in one step is Ap,p → ǫ ∗ Context Free Inductive step: If Ap,q ⇒ x in n + 1 steps. The first step in Grammar the derivation must be Ap,q → Ap,r Ar,q or Ap,q → aAr,s b. Normal Forms Derivations and If it is Ap,q → Ap,r Ar,q, then the string x can be broken Ambiguities ∗ ∗ into two parts x1x2 such that Ap,r ⇒ x1 and Ar,q ⇒ x2 Pumping lemma for CFLs in at most n steps. The theorem easily follows in this PDA

case. Parsing If it is Ap,q → aAr,s b, then the string x can be broken CFL Properties ∗ as ayb such that Ar,s ⇒ y in n steps. Notice that from DPDA, DCFL state p, on reading a the PDA pushes a symbol X to Membership stack, while it pops X in state s on reading b and goes CSL to state q.

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)

The following grammar generates the well-parenthesized Soumyajit Dey CSE, IIT propositional expressions Kharagpur

E =⇒ (EBE)|(UE)|C|V , Context Free Grammar

B =⇒ ∨|∧|→|↔, Normal Forms

Derivations and U =⇒ ¬, Ambiguities C =⇒ ⊤|⊥, Pumping lemma for CFLs

V =⇒ P|Q|R|··· . PDA

Parsing

The words well-parenthesized means that there must be CFL Properties

parentheses around any compound expression. E.g. DPDA, DCFL

Membership

(((P ∨ Q) ∧ R) ∨ (Q ∧ (¬P))) 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 A parser Theory (CS21004)

Soumyajit Dey Start with the initial stack symbol ⊥ and scan the expression CSE, IIT from left to right. Kharagpur 1 If the symbol is a (, push it in the stack. Context Free Grammar 2 If the symbol is an operator, push it in the stack. Normal Forms 3 If the symbol is a constant, push it in the stack. Derivations and Ambiguities

4 If the symbol is a variable, push it in the stack. Pumping lemma for CFLs 5 If the symbol is a ), do a reduce PDA 1 Create a new node. Parsing 2 Pop the top. It should be a constant, variable or CFL Properties another node. DPDA, DCFL 3 Pop the top. It should be an operand. Membership 4 Pop the top. It should be a constant, variable or another node. CSL 5 Pop the top. It should be a (. 6 Push the node in the stack.

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 Parsing Example Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free (((P ∨ Q) ∧ R) ∨ (Q ∧ (¬P))) Grammar

Normal Forms

Derivations and Q Ambiguities _ Pumping lemma Push the 3 (’s in the stack.(i) for CFLs  PDA Push P in the stack.(iv)  Parsing Push ∨ in the stack.(ii)  CFL Properties Push Q in the stack.(iv) 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 Parsing Example contd. Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free Grammar (((P ∨ Q) ∧ R) ∨ (Q ∧ (¬P))) Normal Forms Derivations and Ambiguities

Pumping lemma _ for CFLs Scan ) and reduce.   Q PDA Pop the stack until ( and  Parsing create a new node.  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 Parsing Example contd. Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free (((P ∨ Q) ∧ R) ∨ (Q ∧ (¬P))) Grammar Normal Forms

Derivations and R Ambiguities Pumping lemma ^ for CFLs _ PDA Push ∧ and R in the stack  Parsing  Q 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 Parsing Example contd. Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free Grammar (((P ∨ Q) ∧ R) ∨ (Q ∧ (¬P))) Normal Forms Derivations and Ambiguities

^ Pumping lemma for CFLs  PDA _ Scan ) and reduce.  R Parsing CFL Properties  Q 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 Parsing Example contd. Theory (CS21004)

Soumyajit Dey CSE, IIT (((P ∨ Q) ∧ R) ∨ (Q ∧ (¬P))) Kharagpur

Context Free  Grammar  Normal Forms Derivations and  Ambiguities Pumping lemma ^ for CFLs

Q PDA Push everything  Parsing until the next ). CFL Properties _ DPDA, DCFL

^ Membership  CSL _ ? R  Q 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 Parsing Example contd. Theory (CS21004)

Soumyajit Dey CSE, IIT (((P ∨ Q) ∧ R) ∨ (Q ∧ (¬P))) Kharagpur

Context Free  Grammar Normal Forms  Derivations and  Ambiguities ^ Pumping lemma Q for CFLs PDA Scan the first of  Parsing the final 3 ) and _ CFL Properties reduce. ^ DPDA, DCFL  Membership _ CSL ? R  Q

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 Parsing Example contd. Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free (((P ∨ Q) ∧ R) ∨ (Q ∧ (¬P))) Grammar

Normal Forms

Derivations and ^ Ambiguities  Pumping lemma _ Q  for CFLs Scan the ^ PDA next ) and Parsing CFL Properties reduce.   _ DPDA, DCFL ? R Membership  Q 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 Parsing Example contd. Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free (((P ∨ Q) ∧ R) ∨ (Q ∧ (¬P))) Grammar Normal Forms

Derivations and Ambiguities

_ Pumping lemma ? for CFLs ^ ^ PDA Scan the final 3 )  Parsing and reduce. _ R Q CFL Properties DPDA, DCFL  Q  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 Operator Precedence Theory (CS21004)

Consider the grammar for arithmetic expressions. Soumyajit Dey CSE, IIT E → E + E|E − E|E · E|E/E|− E|C|V |(E), Kharagpur Context Free C → 0|1, Grammar V → a|b|c. Normal Forms Derivations and This grammar is ambiguous. The equivalent unambiguous Ambiguities Pumping lemma grammar is for CFLs

PDA

E → E + F |E − F |F , Parsing F → F · G|F /G|G, CFL Properties G →−G|H, DPDA, DCFL Membership H → C|V |(E), CSL C → 0|1, V → a|b|c.

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 Parsing Example Theory (CS21004)

Soumyajit Dey Given a precedence relation,on the operators, we modify the CSE, IIT parsing rules as follows. When we scan a binary operator B Kharagpur

1 Check if top of the stack is an operand Context Free Grammar 2 Look at the stack symbol A immediately below Normal Forms

If A has lower precedence than B push B Derivations and Otherwise, reduce Ambiguities Pumping lemma Consider the following example for CFLs

PDA

a + b.c + d Parsing

CFL Properties

DPDA, DCFL

Membership Start with the stack  containing ⊥ CSL  Scan and push a in the stack

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 Parsing Example contd. Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free Grammar b Normal Forms Scan +. Check that ⊥ Derivations and Ambiguities has lower precedence so  Pumping lemma push +.  for CFLs Scan and push b  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 Parsing Example contd. Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free c Grammar Normal Forms

 Derivations and Scan ·. Check that + has Ambiguities

lower precedence so push b Pumping lemma ·.  for CFLs  PDA Scan and push c 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 Parsing Example contd. Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

 Context Free Grammar Scan the second +.  b c Normal Forms Check that · has higher  Derivations and precedence so reduce. Ambiguities ? Pumping lemma for CFLs

PDA

 Parsing Check that + has equal ?   CFL Properties precedence so reduce. b c 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 Parsing Example contd. Theory (CS21004)

Soumyajit Dey CSE, IIT d Kharagpur  Context Free Check that ⊥ has lower Grammar precedence so push +.  Normal Forms ?   Derivations and Scan and push d Ambiguities b c Pumping lemma for CFLs

PDA  Parsing ?  d CFL Properties End of expression so  DPDA, DCFL reduce.  Membership b c 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 Parsing Example contd. Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free Grammar

Normal Forms Why reduce in case of equal precedence ? Derivations and Eventually you have to reduce everything Ambiguities Pumping lemma No point keeping things in stack when it can be reduced for CFLs Unnecessary Push n Pop operations in that case 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 Closure Properties of CFL Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context-free languages are closed under the following opera- Context Free Grammar tions: Normal Forms 1 Union Derivations and Ambiguities

2 Concatenation Pumping lemma for CFLs 3 Kleene closure PDA 4 Homomorphism Parsing 5 Substitution CFL Properties DPDA, DCFL 6 Inverse-homomorphism Membership

7 Reverse 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 Closure Properties of CFLs Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free Grammar

Normal Forms

Derivations and CFL are closed under intersection with regular sets. Ambiguities Pumping lemma CFL are not closed under intersection. for CFLs 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 Closure Properties of CFL Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context-free languages are not closed under intersection and Context Free Grammar complementation. Normal Forms Proof: Derivations and Consider the languages Ambiguities n n m 1 Pumping lemma L = {0 1 2 : n, m ≥ 0} and for CFLs m n n L2 = {0 1 2 : n, m ≥ 0} PDA

Parsing

CFL Properties Both languages are CFLs. DPDA, DCFL Membership What is L1 T L2 ? 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 Closure Properties of CFL Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Proof: contd... Context Free Grammar

n n n Normal Forms 1 2 L = L T L = {0 1 2 : n ≥ 0} and it is not a CFL. Derivations and Ambiguities

Pumping lemma Hence CFLs are not closed under intersection. for CFLs

PDA

Parsing

CFL Properties Use Demorgans law to prove non-closure under complemen- DPDA, DCFL tation. 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 Linear Grammars n languages Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur A linear grammar is a CFG that has at most one nonterminal Context Free in the R.H.S. of each of its productions. Grammar Consider {ai bi | i ≥ 0} Normal Forms Derivations and P = {S → aSb, S → ǫ} Ambiguities Pumping lemma We call such languages as linear languages. Recall, the for CFLs

special cases PDA left-linear grammars : a type of linear grammar with Parsing R.H.S. nonterminals strictly at the left ends; CFL Properties DPDA, DCFL right-linear grammars : a type of linear grammar with Membership R.H.S. nonterminals strictly at the right ends; 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 Linear Grammars n languages Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free All linear languages are context-free Grammar Normal Forms

There are CFLs that are non-linear : balanced Derivations and parenthesis (the famous ”Dyck language”) Ambiguities Pumping lemma S → (S)|SS|ǫ for CFLs Thus PDA Parsing regular languages are a proper subset of linear languages CFL Properties linear languages are a proper subset of CFLs 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 Linear Grammars n languages Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Linear languages that are regular are deterministic Context Free (DPDA acceptable naturally) Grammar Normal Forms

there exist linear languages that are nondeterministic Derivations and Ambiguities

Ex : the language of even-length on {0, 1} has Pumping lemma the linear grammar S → 0S0|1S1|ǫ. Require NPDA for CFLs PDA

linear languages are closed under intersection with Parsing

regular sets CFL Properties linear languages are closed under homomorphism and DPDA, DCFL inverse homomorphism. 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 Deterministic Pushdown Automata Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free Grammar

A PDA P = (Q, Σ, Γ, δ, q0, ⊥, F ) is deterministic if Normal Forms δ(q, a, X ) has at most one member for every q ∈ Q, Derivations and Ambiguities

a ∈ Σ or a = ǫ, and X ∈ Γ. Pumping lemma If δ(q, a, X ) is nonempty for some a ∈ Σ then δ(q, ǫ, X ) for CFLs must be empty. PDA Parsing n n Example: L = {0 1 : n ≥ 1}. 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 Deterministic Pushdown Automata Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free Theorem: Grammar Every regular language can be accepted by a deterministic Normal Forms Derivations and pushdown automata that accepts by final states. Ambiguities

Pumping lemma for CFLs

PDA

Parsing Theorem (DPDA 6= PDA): CFL Properties There are some CFLs, for instance {ww R } that can not DPDA, DCFL

be accepted by a DPDA. 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 Closure Properties of DCFLs Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur

Context Free Grammar

A deterministic context free language is a language accepted Normal Forms

by a deterministic PDA(DPDA). Every DCFL is a CFL but Derivations and not vice versa. Ambiguities Pumping lemma DCFL are not closed under union for CFLs DCFL are not closed under reversal PDA Parsing

DCFL are closed under complementation 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 CYK Algorithm Theory (CS21004)

Soumyajit Dey CSE, IIT Kharagpur Cubic-time Algorithm to check if a string x belongs to a Context Free grammar G in CNF. Grammar

Consider the following grammar for the set of all non-null Normal Forms

strings with equal number of a’s and b’s. Derivations and Ambiguities

Pumping lemma S → AB|BA|SS|AC|BD, for CFLs A → a, PDA B → b, Parsing CFL Properties

C → SB, DPDA, DCFL D → SA. 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 CYK Algorithm contd. Theory (CS21004)

Soumyajit Dey CSE, IIT We’ll run the algorithm on the string Kharagpur

| a | a | b | b | a | b | Context Free 0123456 Grammar For 0 ≤ i < j ≤ n, xij denote the substring of x between Normal Forms n Derivations and lines i and j. Build a table T with 2 entries, one for each Ambiguities

pair i, j. Pumping lemma 0 for CFLs – 1 PDA – – 2 Parsing CFL Properties – – – 3 Ti,j refers to substring xi,j . DPDA, DCFL ––––4 Membership

–––––5 CSL ––––––6

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 We will fill each entry Ti,j ∈ T with the set of nonterminals Kharagpur of G that produce substring xi,j . We start with substring of Context Free length 1. For each substring c = xi,i+1, if there is a Grammar production X → c ∈ G, we write X in Ti,j . Normal Forms 0 Derivations and Ambiguities

A 1 Pumping lemma – A 2 for CFLs – – B 3 PDA – – – B 4 Parsing – – – – A 5 CFL Properties ––– –– B 6 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 For each substring c = xi,i+2, we break the substring into two non-null substrings xi,i+1 and xi+1,i+2 and check the Context Free table entries Grammar 0 Normal Forms Derivations and A 1 Ambiguities

φ A 2 Pumping lemma – SB 3 for CFLs PDA – – φ B 4 Parsing – – – SA 5 CFL Properties

– – – – SB 6 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)

Now proceed to strings of length 3. For each substring Soumyajit Dey CSE, IIT c = xi,i+3, we have two ways to proceed: we break the Kharagpur substring into two non-null substrings xi,i+1 and xi+1,i+3 or Context Free xi,i+2 and xi+2,i+3. For example,x0,3 = x0,1x1,3 = x0,2x2,3. Grammar

For the first, A ∈ T0,1 and S ∈ T1,3 but AS is not present in Normal Forms

the right side of any production. Similarly nothing produces Derivations and Ambiguities T0,2. So T0,3 is φ Pumping lemma 0 for CFLs A 1 PDA φ A 2 Parsing φ SB 3 CFL Properties – – φ B 4 DPDA, DCFL – – – SA 5 Membership – – – – SB 6 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 For x1,4 = x1,2x2,4 = x1,3x3,4, A ∈ T1,2 and φ ∈ T2,4. But S ∈ T1,3 and B ∈ T3,4 and C → SB ∈ G. So T1,4 is Context Free labeled with C. Grammar 0 Normal Forms Derivations and A 1 Ambiguities

φ A 2 Pumping lemma φ SB 3 for CFLs PDA – C φ B 4 Parsing – – – SA 5 CFL Properties

– – – – SB 6 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 Continue in this fashion for stings of length three,four etc. CSE, IIT Kharagpur For strings of length four there are 3 ways to break them up Context Free and every one must be checked. the final result is Grammar

0 Normal Forms

A 1 Derivations and φ A 2 Ambiguities Pumping lemma φ SB 3 for CFLs

S C φ B 4 PDA DS φ SA 5 Parsing S C φ CSB 6 CFL Properties We see that T0,6 is labeled with S so we conclude that x is DPDA, DCFL generated by G. 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 Algorithm 1: CYK Algorithm and Automata Theory (CS21004) for i := 0 to n − 1 do /* 1 length strings first */ Soumyajit Dey CSE, IIT Ti,i+1 := Φ /* Initialize to Φ */ Kharagpur

Context Free for A → a ∈ G do Grammar if a = xi,i+1 then Normal Forms Ti,i+1 := Ti,i+1 ∪ {A} Derivations and for m := 2 to n do /* for each length m ≥ 2 */ Ambiguities

Pumping lemma for i := 0 to n − m do /* for each substring */ for CFLs PDA

Ti,i+m := Φ /* of length m */ Parsing

CFL Properties for j := i + 1 to i + m − 1 do /* for all ways to breakup the string */ DPDA, DCFL Membership for A → BC ∈ G do CSL if B ∈ Ti,j ∧ C ∈ Tj,i+m then Ti,i+m := Ti,i+m ∪ {A}

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) An is a 4-tuple G = (V , Σ, S, P), Soumyajit Dey where V and Σ are disjoint sets of variables and CSE, IIT terminals, respectively. S is an element of V called the Kharagpur start symbol, and P is a set of productions of the form Context Free Grammar α → β Normal Forms Derivations and ∗ Ambiguities where α, β ∈ (V ∪ Σ) and α contains at least one Pumping lemma variable. for CFLs PDA

A context-sensitive grammar (CSG) is an unrestricted Parsing

grammar in which no production is length-decreasing. CFL Properties

In other words, every production is of the form α → β, DPDA, DCFL

where |β| ≥ |α|. Membership A language is a context-sensitive language (CSL) if it CSL can be generated by a context-sensitive grammar.

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) A CSG Generating L = {anbncn|n ≥ 1} Soumyajit Dey CSE, IIT S → SABC|ABC Kharagpur BA → AB Context Free Grammar

CA → AC Normal Forms CB → BC Derivations and Ambiguities

A→ a Pumping lemma for CFLs

aA → aa PDA aB → ab Parsing bB → bb CFL Properties DPDA, DCFL bC → bc Membership cC → cc 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 The derivation for the string aabbcc is Kharagpur

Context Free S →aSBC Grammar →aaBCBC Normal Forms Derivations and →aabCBC Ambiguities →aabBCC Pumping lemma for CFLs →aabbCC PDA →aabbcC Parsing CFL Properties →aabbcc DPDA, DCFL

Membership

CSL

Soumyajit Dey CSE, IIT Kharagpur Formal Language and Automata Theory (CS21004)