0% found this document useful (0 votes)
67 views101 pages

CFG Pda

The document summarizes the topics to be covered in the course "Formal Language and Automata Theory" taught by Soumyajit Dey at IIT Kharagpur. The topics include: context-free grammar, normal forms, derivations and ambiguities, pumping lemma for context-free languages, pushdown automata, parsing, context-free language properties, deterministic pushdown automata, deterministic context-free languages, membership problems, and context-sensitive languages.

Uploaded by

jasleen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views101 pages

CFG Pda

The document summarizes the topics to be covered in the course "Formal Language and Automata Theory" taught by Soumyajit Dey at IIT Kharagpur. The topics include: context-free grammar, normal forms, derivations and ambiguities, pumping lemma for context-free languages, pushdown automata, parsing, context-free language properties, deterministic pushdown automata, deterministic context-free languages, membership problems, and context-sensitive languages.

Uploaded by

jasleen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 101

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

Formal Language and Automata Theory Context Free


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
Announcements and Automata
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
Table of Contents and Automata
Theory (CS21004)

Soumyajit Dey
1 Context Free Grammar CSE, IIT
Kharagpur

2 Normal Forms
Context Free
Grammar
3 Derivations and Ambiguities Normal Forms

4 Pumping lemma for CFLs Derivations and


Ambiguities

Pumping lemma
5 PDA for CFLs

PDA
6 Parsing
Parsing

7 CFL Properties CFL Properties

DPDA, DCFL
8 DPDA, DCFL Membership

CSL
9 Membership
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
h...i |= | hassg-stmti CSE, IIT
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
Ambiguities
hstmt-listi |= hstmti | hstmti ; hstmt-listi
Pumping lemma
hassg-stmti |= hvari for CFLs

PDA
hvari |= harith-expri
Parsing
hbool-expri |= harith-exprihcompare-opiharith-expri CFL Properties

hcompare-opi |= < | > | ≤ | ≥= | =


6 DPDA, DCFL

Membership
harith-expri |= hvari | hconsti | harith-expriharith-opiharith-expri
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

Normal Forms
hwhile-stmti
Derivations and
while hbool-stmti do hstmti Ambiguities
while harith-exprihcompare-opiharith-expri do hstmti Pumping lemma
for CFLs
while hvarihcompare-opiharith-expri do hstmti
PDA
while hvari ≤ harith-expri do hstmti
Parsing
while hvari ≤ hvari do hstmti CFL Properties
while hxi ≤ hvari do hstmti DPDA, DCFL
while hxi ≤ hy i do hstmti Membership
while hxi ≤ hy i 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

N is a finite set (the non-terminal symbols), Derivations and


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

{an b n | 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
Table of Contents and Automata
Theory (CS21004)

Soumyajit Dey
1 Context Free Grammar CSE, IIT
Kharagpur

2 Normal Forms
Context Free
Grammar
3 Derivations and Ambiguities Normal Forms

4 Pumping lemma for CFLs Derivations and


Ambiguities

Pumping lemma
5 PDA for CFLs

PDA
6 Parsing
Parsing

7 CFL Properties CFL Properties

DPDA, DCFL
8 DPDA, DCFL Membership

CSL
9 Membership
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

Parsing
For any CFG G , there is a CFG G ′ in Chomsky Normal
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
Create grammar for ǫ free language and Automata
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

CFL Properties
1 if A → αBβ and B → ǫ are in P̂; then A → αβ is in P̂
DPDA, DCFL
2 if A → B and B → γ are in P̂, then A → γ is in P̂
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
Create grammar for ǫ free language and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

Context Free
Grammar

Normal Forms
Note that for the language Ĝ = (N, Σ, P̂, S), L(G ) = L(Ĝ )
Derivations and
L(G ) ⊆ L(Ĝ ) since P ⊆ P̂ Ambiguities

Pumping lemma
L(G ) = L(Ĝ ) : 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
Create grammar for ǫ free language and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

Context Free
Grammar

Can prove that, for any x 6= ǫ, ∃ a derivation S ⇒ x without Normal Forms

Ĝ Derivations and
ǫ and unit productions Ambiguities

Pumping lemma
Remove ǫ and unit productions from Ĝ for CFLs

Create G ′ = (N, Σ, P ′ , S) where P ′ is same as P̂ but w/o ǫ PDA

Parsing
and unit productions
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 ′ to CNF and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

Context Free
Grammar
For each terminal a ∈ Σ introduce a new nonterminal
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 A → B1 B2 · · · 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 ′ to CNF and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

Context Free
For any production Grammar

Normal Forms

A → B1 B2 · · · 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 → B1 C and Parsing
2 C → B2 B3 · · · 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
n n n n Kharagpur
{a b |n ≥ 0} − {ǫ} = {a b |n ≥ 1}
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)

Derive a CNF grammar for the set of non-null strings of Soumyajit Dey
CSE, IIT
balanced parenthesis [ ] Kharagpur

Starting with the grammar Context Free


Grammar

Normal Forms
S → [S]|SS|ǫ
Derivations and
Ambiguities

Pumping lemma
for CFLs
1 Remove ǫ productions to get S → [S]|SS|[ ]
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
Table of Contents and Automata
Theory (CS21004)

Soumyajit Dey
1 Context Free Grammar CSE, IIT
Kharagpur

2 Normal Forms
Context Free
Grammar
3 Derivations and Ambiguities Normal Forms

4 Pumping lemma for CFLs Derivations and


Ambiguities

Pumping lemma
5 PDA for CFLs

PDA
6 Parsing
Parsing

7 CFL Properties CFL Properties

DPDA, DCFL
8 DPDA, DCFL Membership

CSL
9 Membership
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

CSL
Abbc → aAbbc → abbc

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
Ambiguities
Ambiguity is a property of a grammar, and it is usually
Pumping lemma
(but not always) possible to find an equivalent for CFLs

unambiguous grammar. PDA

Parsing
An inherently ambiguous language is a language for
CFL Properties
which no unambiguous grammar exists.
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
Kharagpur
has the following two leftmost derivations from S:
1 S → AS → 0A1S → 0A11S → 00111S → 00111 Context Free
Grammar
2 S → AS → A1S → 0A11S → 00111S → 00111
Normal Forms
Intuitively, we can use A → A1 first or second to Derivations and
generate the extra 1. The language of our example Ambiguities

grammar is not inherently ambiguous, even though the Pumping lemma


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

DPDA, DCFL
{ai b j c k |i = j or j = k} is an inherently ambiguous
Membership
language
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
Table of Contents and Automata
Theory (CS21004)

Soumyajit Dey
1 Context Free Grammar CSE, IIT
Kharagpur

2 Normal Forms
Context Free
Grammar
3 Derivations and Ambiguities Normal Forms

4 Pumping lemma for CFLs Derivations and


Ambiguities

Pumping lemma
5 PDA for CFLs

PDA
6 Parsing
Parsing

7 CFL Properties CFL Properties

DPDA, DCFL
8 DPDA, DCFL Membership

CSL
9 Membership
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
for CFLs
|vwx| ≤ k
PDA
∀i ≥ 0, uv i wx i 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 regular language grows in one direction per Context Free
production : standard form is strictly right/left linear Grammar

Normal Forms
A Context Free language grows in two directions per
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

CNF grammar for {an b n |n ≥ 1} : Formal Language


and Automata
Theory (CS21004)
S → AB|AC , C → SB, A → a, B → b.
Soumyajit Dey
CSE, IIT
S Kharagpur
 C
Context Free
S Grammar
a B
Normal Forms
 C b Derivations and
Ambiguities
a B
S Pumping lemma
for CFLs
b
 C PDA

Parsing
a S B
CFL Properties

 B b DPDA, DCFL

Membership
a b CSL

Figure: derivation tree for a4 b 4 :

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

S Soumyajit Dey
CSE, IIT
 Kharagpur
C

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

PDA
a a a S B 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 a4 b 4 :

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

Parsing
a a a S B 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

a a
Derivations and
S B b
Ambiguities
a a  C b b Pumping lemma
for CFLs
a a a S B 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 x y
w
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)
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 A B b PDA

Parsing
a a b b CFL Properties




DPDA, DCFL
v x 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 S B b b
Pumping lemma
for CFLs
a a a  C b b b
PDA
a a a a S b b b Parsing
B






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

a a Pumping lemma
 B b b for CFLs
a a a PDA
b b b

a
a

a
a  w
b

b
b

b
Parsing

CFL Properties

DPDA, DCFL




u 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
Proof and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

Let G be a grammar for A in CNF. Context Free


Grammar
k= 2n+1 , 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

DPDA, DCFL
By pigeonhole principle, some nonterminal must occur
Membership
more than once
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

lower ocurrence of X respectively Pumping lemma


for CFLs

Replacing t with T once we get a valid subtree of PDA

uv 2 wx 2 y Parsing

CFL Properties
Repeat it to produce uv i wx i y , i ≥1
DPDA, DCFL
Replace T with t to get uv 0 wx 0 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
So it cannot have more than 2n+1 = 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 = {an b n an |n ≥ 0} is not context free and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

Adversary picks k in step 1 Context Free


Grammar
You pick z = ak b k ak 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 2 wx 2 y 6∈ A PDA

Parsing
1 either v or x contain at least one a and at least one b
CFL Properties
2 v and x contains only a’s or only b’s
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}∗} is not context free and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
As CFLs are closed under intersection with regular sets, Kharagpur

it suffices to show that


Context Free
Grammar
A′ = A ∩ L(a∗ b ∗ a∗ b ∗ ) = {an b m an b m |m, n ≥ 0} Normal Forms

Derivations and
Ambiguities
is not context-free
Pumping lemma
Adversary picks k in step 1 for CFLs

You pick z = ak b k ak b k such that z ∈ A′ and |z| ≥ k. PDA

Parsing
call each of the four substrings of the form ak or b k 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 2 wx 2 y 6∈ A′
Context Free
If v and x are from the same block, then uv 2 wx 2 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
uv 2 wx 2 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
(if vx contains a b) or both. Either way uv 2 wx 2 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
Table of Contents and Automata
Theory (CS21004)

Soumyajit Dey
1 Context Free Grammar CSE, IIT
Kharagpur

2 Normal Forms
Context Free
Grammar
3 Derivations and Ambiguities Normal Forms

4 Pumping lemma for CFLs Derivations and


Ambiguities

Pumping lemma
5 PDA for CFLs

PDA
6 Parsing
Parsing

7 CFL Properties CFL Properties

DPDA, DCFL
8 DPDA, DCFL Membership

CSL
9 Membership
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

PDA
finite control B stack Parsing
C CFL Properties
B 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
Membership
some input symbol a with some stack top element A, the
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

Normal Forms
element A, the PDA moves to another state q, pops A,
Derivations and
pushes B1 · · · Bk (Bk first) Ambiguities

((p, ǫ, A), (q, B1 · · · Bk )) ∈ δ: from a state p with some Pumping lemma


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 A
Derivations and
Ambiguities

Pumping lemma
B for CFLs

A PDA

Parsing
C 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

Normal Forms
1
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

PDA
If ((p, a, A), (q, γ)) ∈ δ, then for any y ∈ Σ∗ and
1 Parsing
β ∈ Γ∗ , (p, ay , Aβ) → (q, y , γβ); CFL Properties
M
DPDA, DCFL
and if ((p, ε, A), (q, γ)) ∈ δ,then for any y ∈ Σ∗ and
1 Membership
β ∈ Γ∗ , (p, y , Aβ) → (q, y , γβ) CSL
M

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
Acceptance and Automata
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 Pumping lemma
M for CFLs
bottom element when string exhausted
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
PDA for balanced parenthesis and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

Q = {q}, Context Free


Grammar
Σ = {[, ]},
Normal Forms
Γ = {⊥, [}, Derivations and
Ambiguities
start state = q Pumping lemma
for CFLs
initial stack symbol =⊥,
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
for CFLs
→ (q, ] ] [ ], [[ ⊥) transition (2)
PDA
→ (q, ] [ ], [ ⊥) transition (3)
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
CFG to NPDA and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
All productions of G = (Σ, N, P, S) are of the form: Kharagpur

A → cB1 B2 .....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

CFL Properties
q is the start state
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
Conversion Rule and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

For each production A → cB1 B2 .....Bk in P Context Free


Grammar
The transition relation δ is defined as Normal Forms
((q, c, A), (q, B1 B2 ....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 B1 B2 .....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,
CFL Properties
without scanning an input symbol, pop A off the stack,
DPDA, DCFL
push B1 B2 .....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
Example Conversion and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

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


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

DPDA, DCFL
5 B →] ((q, ], B), (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
Example Derivation for the i/p ‘x = [ [ [ ] ] [ ] ]’ and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Rule Sentential forms Configuration of M Kharagpur

applied in a leftmost in an accepting


Context Free
derivation of x in G computation of M Grammar

on input x Normal Forms

Derivations and
S (q, [[[] ] [ ] ], S) Ambiguities

(3) [SB (q, [[] ] [ ] ], SB) Pumping lemma


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

would have z = [ [ [, 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
CFG G converted to NPDA M and Automata
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
NPDA to CFG and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

Context Free
Grammar

Every NPDA can be simulated by an NPDA with one Normal Forms

state. Derivations and


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
Single state NPDA to CFG and Automata
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 → cB1 B2 ...Bk for every PDA

transition ((q, c, A), (q, B1 B2 ...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
Arbitrary NPDA to single state NPDA and Automata
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

PDA
Equivalent NPDA M ′ = ({∗}, Σ, Γ′ , δ′ , ∗, hs ⊥ ti, Ø)
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 , B1 B2 ...Bk )) ∈ δ, where
c ∈ Σ ∪ {ǫ} Context Free
Grammar
((∗, c, hpAqk i), (∗, hq0 B1 q1 ihq1 B2 q2 i....hqk−1 Bk qk i)) ∈ Normal Forms

δ′ . For all possible choice of q1 , q2 , ....., qk . Derivations and


Ambiguities
For k=0, ((∗, c, hpAq0 i), (∗, ǫ)) ∈ δ′ 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
NPDA to CFG - another option and Automata
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
γ ∈ Γ or (q ′ , ǫ). Consider the equivalent grammar GP = Ambiguities

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
NPDA to CFG and Automata
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

PDA
We can prove this theorem by induction on the number of
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
NPDA to CFG and Automata
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 x1 x2 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

Membership
state p, on reading a the PDA pushes a symbol X to
CSL
stack, while it pops X in state s on reading b and goes
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
Table of Contents and Automata
Theory (CS21004)

Soumyajit Dey
1 Context Free Grammar CSE, IIT
Kharagpur

2 Normal Forms
Context Free
Grammar
3 Derivations and Ambiguities Normal Forms

4 Pumping lemma for CFLs Derivations and


Ambiguities

Pumping lemma
5 PDA for CFLs

PDA
6 Parsing
Parsing

7 CFL Properties CFL Properties

DPDA, DCFL
8 DPDA, DCFL Membership

CSL
9 Membership
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

U =⇒ ¬, Derivations and
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
A parser and Automata
Theory (CS21004)

Soumyajit Dey
Start with the initial stack symbol ⊥ and scan the expression CSE, IIT
Kharagpur
from left to right.
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
CSL
another node.
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
Parsing Example and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

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

Normal Forms

Derivations and
Q Ambiguities

_ Pumping lemma
for CFLs
Push the 3 (’s in the stack.(i)
Push P in the stack.(iv)
 PDA

 Parsing

Push ∨ in the stack.(ii)  CFL Properties

DPDA, DCFL
Push Q in the stack.(iv)  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
Parsing Example contd. and Automata
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.   PDA
Q
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
Parsing Example contd. and Automata
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
_ Parsing
Push ∧ and R in the stack   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
Parsing Example contd. and Automata
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
Parsing Example contd. and Automata
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

Parsing
Push everything  CFL Properties
until the next ).
_ 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
Parsing Example contd. and Automata
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
Parsing Example contd. and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

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

Normal Forms

Derivations and
^ Ambiguities

 Pumping lemma
for CFLs
_ Q  PDA
Scan the ^ Parsing
next ) and
reduce.   CFL Properties

_ R DPDA, DCFL
? 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
Parsing Example contd. and Automata
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.
_ Q 
R 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
Operator Precedence and Automata
Theory (CS21004)
Consider the grammar for arithmetic expressions. Soumyajit Dey
CSE, IIT
Kharagpur
E → E + E |E − E |E · E |E /E | − E |C |V |(E ),
Context Free
C → 0|1, Grammar

V → a|b|c. Normal Forms

Derivations and
Ambiguities
This grammar is ambiguous. The equivalent unambiguous
Pumping lemma
grammar is for CFLs

PDA
E → E + F |E − F |F , Parsing

F → F · G |F /G |G , CFL Properties

DPDA, DCFL
G → −G |H,
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
Parsing Example and Automata
Theory (CS21004)

Given a precedence relation,on the operators, we modify the Soumyajit Dey


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

Start with the stack Membership

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

Soumyajit Dey
CSE, IIT
Kharagpur

Context Free
Grammar

Normal Forms

Scan +. Check that ⊥ b Derivations and


Ambiguities
has lower precedence so  Pumping lemma
push +.  for CFLs

PDA
Scan and push b  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
Parsing Example contd. and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

Context Free
Grammar
c Normal Forms

Scan ·. Check that + has


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

Soumyajit Dey
CSE, IIT
Kharagpur

 Context Free
Grammar
Scan the second +. 
Check that · has higher b c Normal Forms

 Derivations and
precedence so reduce. Ambiguities
? Pumping lemma
for CFLs

PDA
 Parsing
Check that + has equal ?   CFL Properties

precedence so reduce. DPDA, DCFL


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

Soumyajit Dey
CSE, IIT
Kharagpur
d
Check that ⊥ has lower  Context Free
Grammar

precedence so push +.  Normal Forms

Scan and push d ?   Derivations and


Ambiguities

b c Pumping lemma
for CFLs

PDA

 Parsing

CFL Properties
?  d
End of expression so DPDA, DCFL

reduce.   Membership

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

Soumyajit Dey
CSE, IIT
Kharagpur

Context Free
Grammar

Normal Forms
Why reduce in case of equal precedence ? Derivations and
Ambiguities
Eventually you have to reduce everything
Pumping lemma
No point keeping things in stack when it can be reduced for CFLs

PDA
Unnecessary Push n Pop operations in that case
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
Table of Contents and Automata
Theory (CS21004)

Soumyajit Dey
1 Context Free Grammar CSE, IIT
Kharagpur

2 Normal Forms
Context Free
Grammar
3 Derivations and Ambiguities Normal Forms

4 Pumping lemma for CFLs Derivations and


Ambiguities

Pumping lemma
5 PDA for CFLs

PDA
6 Parsing
Parsing

7 CFL Properties CFL Properties

DPDA, DCFL
8 DPDA, DCFL Membership

CSL
9 Membership
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
Closure Properties of CFL and Automata
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

CFL Properties
5 Substitution
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
Closure Properties of CFLs and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

Context Free
Grammar

Normal Forms

Derivations and
Ambiguities
CFL are closed under intersection with regular sets.
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
Closure Properties of CFL and Automata
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

L1 = {0n 1n 2m : n, m ≥ 0} and Pumping lemma


for CFLs
L2 = {0m 1n 2n : n, m ≥ 0} PDA

Parsing

CFL Properties

Both languages are CFLs. DPDA, DCFL

Membership
T CSL
What is L1 L2 ?

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
Closure Properties of CFL and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

Proof: contd... Context Free


Grammar

Normal Forms
L2 = {0n 1n 2n : n ≥ 0} and it is not a CFL.
T
L = L1 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
Linear Grammars n languages and Automata
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 b i | 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
Linear Grammars n languages and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

Context Free
Grammar
All linear languages are context-free
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

PDA
Thus
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
Linear Grammars n languages and Automata
Theory (CS21004)

Soumyajit Dey
CSE, IIT
Kharagpur

Linear languages that are regular are deterministic Context Free


Grammar
(DPDA acceptable naturally)
Normal Forms
there exist linear languages that are nondeterministic Derivations and
Ambiguities
Ex : the language of even-length palindromes 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

Membership
inverse homomorphism.
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
Table of Contents and Automata
Theory (CS21004)

Soumyajit Dey
1 Context Free Grammar CSE, IIT
Kharagpur

2 Normal Forms
Context Free
Grammar
3 Derivations and Ambiguities Normal Forms

4 Pumping lemma for CFLs Derivations and


Ambiguities

Pumping lemma
5 PDA for CFLs

PDA
6 Parsing
Parsing

7 CFL Properties CFL Properties

DPDA, DCFL
8 DPDA, DCFL Membership

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

Soumyajit Dey
CSE, IIT
Kharagpur

Context Free
Theorem: Grammar

Normal Forms
Every regular language can be accepted by a deterministic
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
Closure Properties of DCFLs and Automata
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
Ambiguities
not vice versa.
Pumping lemma
DCFL are not closed under union for CFLs

PDA
DCFL are not closed under reversal
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
Table of Contents and Automata
Theory (CS21004)

Soumyajit Dey
1 Context Free Grammar CSE, IIT
Kharagpur

2 Normal Forms
Context Free
Grammar
3 Derivations and Ambiguities Normal Forms

4 Pumping lemma for CFLs Derivations and


Ambiguities

Pumping lemma
5 PDA for CFLs

PDA
6 Parsing
Parsing

7 CFL Properties CFL Properties

DPDA, DCFL
8 DPDA, DCFL Membership

CSL
9 Membership
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
CYK Algorithm and Automata
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

Parsing
B → b,
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
CYK Algorithm contd. and Automata
Theory (CS21004)

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

| a | a | b | b | a | b | Context Free
0 1 2 3 4 5 6 Grammar

For 0 ≤ i < j ≤ n, xij denote the substring of x between Normal Forms

lines i and j. Build a table T with n2 entries, one for each


 Derivations and
Ambiguities
pair i , j. Pumping lemma
0 for CFLs

PDA
– 1
Parsing
– – 2
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

Parsing
– – – B 4
CFL Properties
– – – – A 5
DPDA, DCFL
– – – – – B 6
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
Grammar
table entries
Normal Forms
0
Derivations and
A 1 Ambiguities

φ A 2 Pumping lemma
for CFLs
– S B 3
PDA
– – φ B 4
Parsing
– – – S A 5 CFL Properties
– – – – S 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)
Now proceed to strings of length 3. For each substring Soumyajit Dey
c = xi ,i +3 , we have two ways to proceed: we break the CSE, IIT
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,1 x1,3 = x0,2 x2,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

φ S B 3 CFL Properties

– – φ B 4 DPDA, DCFL

Membership
– – – S A 5
CSL
– – – – S B 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
Kharagpur
For x1,4 = x1,2 x2,4 = x1,3 x3,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
Grammar
labeled with C .
Normal Forms
0
Derivations and
A 1 Ambiguities

φ A 2 Pumping lemma
for CFLs
φ S B 3
PDA
– C φ B 4
Parsing
– – – S A 5 CFL Properties
– – – – S 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
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
and every one must be checked. the final result is Context Free
Grammar
0 Normal Forms
A 1 Derivations and
φ A 2 Ambiguities

Pumping lemma
φ S B 3 for CFLs
S C φ B 4 PDA

D S φ S A 5 Parsing

S C φ C S B 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
DPDA, DCFL
to breakup the string */
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
Table of Contents and Automata
Theory (CS21004)

Soumyajit Dey
1 Context Free Grammar CSE, IIT
Kharagpur

2 Normal Forms
Context Free
Grammar
3 Derivations and Ambiguities Normal Forms

4 Pumping lemma for CFLs Derivations and


Ambiguities

Pumping lemma
5 PDA for CFLs

PDA
6 Parsing
Parsing

7 CFL Properties CFL Properties

DPDA, DCFL
8 DPDA, DCFL Membership

CSL
9 Membership
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 unrestricted grammar is a 4-tuple G = (V , Σ, S, P),
Soumyajit Dey
where V and Σ are disjoint sets of variables and CSE, IIT
Kharagpur
terminals, respectively. S is an element of V called the
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
for CFLs
variable.
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 = {an b n c n |n ≥ 1}
Soumyajit Dey
CSE, IIT
Kharagpur
S → SABC |ABC
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

CFL Properties
bB → bb
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)

You might also like