CFG Pda
CFG Pda
Properties
Formal Language
and Automata
Theory (CS21004)
Soumyajit Dey
CSE, IIT
Kharagpur
Derivations and
Ambiguities
Pumping lemma
Soumyajit Dey for CFLs
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
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
Pumping lemma
5 PDA for CFLs
PDA
6 Parsing
Parsing
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
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
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)
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
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
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
Pumping lemma
5 PDA for CFLs
PDA
6 Parsing
Parsing
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
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
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
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
Ĝ Derivations and
ǫ and unit productions Ambiguities
Pumping lemma
Remove ǫ and unit productions from Ĝ for CFLs
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
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
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
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
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
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
Pumping lemma
5 PDA for CFLs
PDA
6 Parsing
Parsing
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)
Normal Forms
With this grammar, there is a choice of variables to
Derivations and
expand. If we always expanded the leftmost variable Ambiguities
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
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
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
Pumping lemma
5 PDA for CFLs
PDA
6 Parsing
Parsing
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
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
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
Parsing
a S B
CFL Properties
B b DPDA, DCFL
Membership
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
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
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
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
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
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
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
Derivations and
Ambiguities
is not context-free
Pumping lemma
Adversary picks k in step 1 for CFLs
Parsing
call each of the four substrings of the form ak or b k as CFL Properties
blocks DPDA, DCFL
|vwx| ≤ k 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
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
Derivations and
If v and x are on different blocks, then the blocks must Ambiguities
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
Pumping lemma
5 PDA for CFLs
PDA
6 Parsing
Parsing
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)
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
Normal Forms
element A, the PDA moves to another state q, pops A,
Derivations and
pushes B1 · · · Bk (Bk first) Ambiguities
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
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
NPDA M that has a single final state t and M can empty its CFL Properties
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
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
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
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
Derivations and
q is the only state. Ambiguities
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
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
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
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
Derivations and
S (q, [[[] ] [ ] ], S) Ambiguities
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
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
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 with one state has an equivalent CFG Pumping lemma
for 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
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
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
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
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
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
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
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
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
Pumping lemma
5 PDA for CFLs
PDA
6 Parsing
Parsing
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
U =⇒ ¬, Derivations and
Ambiguities
Parsing
The words well-parenthesized means that there must be CFL Properties
parentheses around any compound expression. E.g. DPDA, DCFL
Membership
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
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
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
Derivations and
Ambiguities
Pumping lemma
_ for CFLs
Scan ) and reduce. PDA
Q
Pop the stack until ( and Parsing
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
Derivations and
Ambiguities
^ Pumping lemma
for CFLs
PDA
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
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)
Pumping lemma
Consider the following example for CFLs
PDA
a + b.c + d Parsing
CFL Properties
DPDA, DCFL
containing ⊥ CSL
Formal Language
Parsing Example contd. and Automata
Theory (CS21004)
Soumyajit Dey
CSE, IIT
Kharagpur
Context Free
Grammar
Normal Forms
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
? 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
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
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
Pumping lemma
5 PDA for CFLs
PDA
6 Parsing
Parsing
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
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
Parsing
CFL Properties
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
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
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
Derivations and
P = {S → aSb, S → ǫ} Ambiguities
Pumping lemma
We call such languages as linear languages. Recall, the for CFLs
special cases PDA
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
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
PDA
linear languages are closed under intersection with Parsing
regular sets CFL Properties
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
Pumping lemma
5 PDA for CFLs
PDA
6 Parsing
Parsing
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
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
Pumping lemma
5 PDA for CFLs
PDA
6 Parsing
Parsing
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
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
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
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
Pumping lemma
5 PDA for CFLs
PDA
6 Parsing
Parsing
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
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
Derivations and
→aabCBC Ambiguities
→aabbCC PDA
→aabbcC Parsing
CFL Properties
→aabbcc DPDA, DCFL
Membership
CSL
Soumyajit Dey CSE, IIT Kharagpur Formal Language and Automata Theory (CS21004)