You are on page 1of 42

Equivalence of Context-free

Grammars & pushdown automata


• Context-free grammars and
pushdown automata are equivalent in
power.
• Both are capable of describing the
class of context-free languages.

1
Theorem:
A language is context free if and only
if some pushdown automaton
recognizes it.

2
Lemma 1: If a language is context free, then
some pushdown automaton recognizes it.

• Let A be a CFL and has a CFG - G,


generating it. Convert G into an
equivalent PDA, M.
• Use method of proof by construction for
the PDA M = (Q, ∑, , , q0, F),

3
The states of PDA M
Q = {qstart, qloop, qaccept} U E,
qstart - start state, no edge from any state goes
into it
qaccept - accept state, no edge into any state goes
from it
qloop - all stack activities are described with it
E - set of extra states.

4
The transition function

 Let q and r be states of the PDA and let a be in


∑ and s be in  .
 The transition function (r, u)  (q, a, s)
means that when q is the state of the automaton,
a is the next input symbol, s is the symbol on
the top of the stack, the PDA reads the a and
pop the s, (s→u a production rule), pushes the
string u = u1 . . . uL , writing onto the stack and
go on to state r.
5
Transitions for PDA M
1.Initializing the stack to contain the symbols $ and S.
let (qstart, , ) = {( qloop, S$)}.
2.Transitions for the main loop of PDA.
a.The top of the stack contains a variable.
Let ( qloop, , A) = {( qloop, w) | where A → w is a
rule in R}. $- empty stack
b.The stack contains a terminal. marker,
Let ( qloop, a, a) = {( qloop,  ) } . S- start variable
c.The empty stack marker $ is on the top of the stack.
Let ( qloop, , $) = {( qaccept, ) }.

6
State diagram of M

TOS ACTION
Variable PUSH the RHS of a rule for it
Terminal POP it (if it matches with the
input string symbol)

7
Steps of operation on stack for
PDA M
1. Place marker symbol $ and start variable S on the stack.
2. Repeat : If the top of stack is
a. Variable symbol A: non-deterministically select one of the
rules for A and replace A by the string on the right-hand side of
the rule. (The leftmost symbol of the string on the top of stack
to provide leftmost derivation of parse tree)
b. Terminal symbol a: remove it from stack, read the next
symbol from the input and compare it to a. If they match,
repeat. Otherwise reject on this branch of the nondeterminism.
c. Marker symbol $: enter the accept state. Accept the input
string if it has all been read.

8
Example: Stack activity for string
0011 from language 0n1n
Grammar rules
A  0A1,
A 
Content of stack:

9
State Diagram of PDA for language 0n1n
implementing the CFG: A →0A1 | 

10
Example: Construct a PDA from the CFG
S →aTb | b, T → Ta | 

11
Lemma 2: If a pushdown automaton recognizes
some language, then it is context free.

Let A be a CFL and has a PDA - M,


generating it. Convert M into an equivalent
CFG, G.

12
Proof idea
Idea: Provided a PDA M, make a CFG G that generates
all the strings that M accepts, viz., strings that cause the
PDA to go from its start state to an accept state.
 Design a grammar such that for each pair of states p
and q in M the grammar will have a variable Apq.
 This variable generates all the strings that can take P
from p with an empty stack to q with an empty stack.
 Also, such strings can take M from p to q, regardless
of the stack contents at p, leaving the stack at q in the
same condition as it was at p.

13
Possible computation of PDA M on
string x
1.The symbol popped at the end is the symbol
that was pushed at the beginning
 If so, the stack could be empty only at the start
and end of M's computation on string x.
 Simulate the possibility with the rule
Apq→ aArsb, where a is the input read at the
first move, b is the input read at the last move, r
is the state following p, and s is the state
preceding q.
14
Possible computation of PDA M on
string x (cont.)
2. The symbol popped at the end is not the
symbol that was pushed at the beginning ,
 If so, the initially pushed symbol must get
popped at some point before the end of x and
thus the stack becomes empty at this point.
 Simulate the possibility with the rule
Apq → AprArq, where r is the state when the
stack becomes empty.

15
PDA computation corresponding to the
rule Apq→ aArsb

16
PDA computation corresponding to the
rule Apq → AprArq

17
Claim #1
If rule Apq generates string x, then x can bring
PDA M from p with empty stack to q with empty
stack

Proof by induction on the number of steps in the


derivation of x from Apq.

18
Claim # 2
If string x can bring PDA M from state p with
empty stack to state q with empty stack, Apq
generates x.

Prove this claim by induction on the number of


steps in the computation of PDA that goes from
p to q with empty stacks on input x.

19
Construction of Grammar from
PDA
Let PDA M = (Q, ∑, , , q0, {qaccept} )
Construct G such that the variables of G
are {Apq | p, q  Q}.
The start variable is Aq0qaccept.

•The PDA guesses the leftmost derivation.

20
Steps of construction of Grammar
rules
1. For each p, q, r, s  Q, t  , and a, b  ∑ ,
if (p, a, ) contains (r, t) and (s, b, t)
contains (q, ), put the rule Apq → aArsb in G.
2. For each p, q, r  Q, put the rule
Apq → AprArq in G.
3. For each p  Q, put the rule App →  in G.

21
Conversion from PDA to CFG
The idea for the conversion from PDA to CFG is to
make each step in a derivation correspond to
a move by the PDA.
assume that:
(a) stack of the PDA M is empty if and only M is in
the accept state;
(b) every move is either a push of a single symbol or
a pop of a single symbol .

22
The Variables are Triples

The variables of the CFG will be all the


ordered triples [q A p] where q, p are states
of the PDA,
and A is a symbol from the stack alphabet.

23
Invariant for Triples
Construct the CFG such that for all strings w:
•[q A p] ⇒∗ w, M can go from state q to p while reading
w with the net effect of popping symbol A.
•[q0, $, qa] is the start variable for the CFG: it generates
string w if and only if M can go from q0 to qa while
reading w and popping the empty stack symbol $; that
is, w is accepted.

24
The Productions: Pops

• For a pop move; (p, ε) ∈ δ(q, a, A).


( in state q the PDA can pop symbol A while
reading a and changing to state p (a can be ε)).
Add a production: [q A p] → a

25
The Productions: Pushes
• For the push move; (p, AB) ∈ δ(q, a, A).
(in state q with A on stack-top, push symbol B
while reading a and changing to state p).
Add the collection of productions:
[q A r] → a [p B s] [s A r] for every state r and
s.

• Two ways to erase the stack-top symbol: (a)


pop it, or (b) push something, and then later
erase both symbols.
26
Example Conversion
The PDA for {0n1n }

27
Example CFG
Start symbol: [F $ H]
•Popping
[F x G] → 1
[G x G] → 1
[G $ H] → ε

•Pushing
yields 18 productions of the form
[F A r] → 0 [F x s] [s A r] for each A ∈ {x, $}
and r, s ∈ {F, G, H}.

28
Simplification
Omit productions that produce triples that are never
LHS
1) [F x F] → 0 [F x F] [F x F]
2) [F x G] → 0 [F x F] [F x G]
3) [F x H] → 0 [F x F] [F x H]
4) [F $ F] → 0 [F x F] [F $ F]
5) [F $ G] → 0 [F x F] [F $ G]
6) [F $ H] → 0 [F x F] [F $ H]
7) [F x G] → 0 [F x G] [G x G]
8) [F $ H] → 0 [F x G] [G $ H]
[F x F] is also unusable ; omit its rules.
29
Final CFG
CFG with start variable
[F $ H] : T→1
U→1
[F x G] → 1
V→ε
[G x G] → 1 T → 0TU
[G $ H] → ε S0 → 0TV
[F x G] → 0 [F x G] [G x G]
[F $ H] → 0 [F x G] [G $ H]

30
Summary: Steps of conversion

a) Add a rule S → <sf> for the start


state, s, and each final state, f.

For example:
S → <sf1>
S → <sf2>

31
Steps of conversion (cont.)
b) Add a rule <qq> →  for each state q.
For example:
<q1q1> → 
<q2q2> → 
c) For each transition, that pushes a single character
(including ), such as
δ(q,u,A) = (r,B)
add rules of the form
<qAp> → u<rBp>
for each state p in the machine.
The letter u can be  (in which case it disappears).

32
Steps of conversion (cont.)
d) For each state in the PDA that pushes two (or more)
characters, such as
δ(q,u,A) = (r,BC)
add rules of the form
<qAp> → u<rBt><tCp>
for all possible combinations of states p and t in the
machine.
This pattern extends to arbitrary length strings
pushed onto the stack:
<qAp> → u<rBt><tCn><nDp>, etc.
33
Steps of conversion (cont.)
e. For every transition that does not inspect the stack
(i.e., the pop character is e), add one transition that
pops a single character and pushes it back again, for
each letter in the stack alphabet.
Thus if the stack alphabet is {X, Y}, and transition
δ(q,u,e) = (r,X)
then add the transitions
δ(q,u,X) = (r,XX)
δ(q,u,Y) = (r,XY)
thus leaving the new X on the top of the stack.
34
Steps of conversion (cont.)
Example
If the stack alphabet is {X, Y}, and the transition
δ(q,u,) = (r,X)
then add the transitions
δ(q,u,X) = (r,XX)
δ(q,u,Y) = (r,XY)
thus leaving the new X on the top of the stack.

35
Simplification of the grammar
• Exclude non-terminals <qAr> where it is not
possible in the PDA to go from state q to state
r,
• Combine transitions with the same start term
separating destination terms with “|”,
• Eliminate unit productions, dead-ends.

36
Example: PDA for the language of balanced
parentheses
Σ = {l,r} for “left” and l,X
r,X 
“right”
Γ = {X}
Rules:
(1) δ(s,l,) = (s,X)
(2) δ(s,r,X) = (s, )
Add
(3) δ(s,l,X) = (s,XX)
37
Construction of Grammar
• From rules type a and b the start state
S → <ss>
<ss> → 
• For rule 1 get the type c rule
<ss> → l<sXs>
• For rule 2 get
<sXs> → r<ss>
• For rule 3 get
<sXs> → l<sXs><sXs>

38
The generated grammar
Renaming <ss> = S and <sXs> = B,
The grammar becomes:
S → lB | 
B → rS | lBB

39
Verification of grammar
1. Generate “lrllrlrr” as follows:
S => lB => lrS => lrlB => lrllBB => lrllrSB =>
lrllrlBB => lrllrlrSB => lrllrlrB => lrllrlrrS =>
lrllrlrr

40
Corollary: Every regular language is
context free.
 The pushdown automata recognize the class of
context free languages.
 Because every regular language is recognized
by a finite automaton and every finite
automaton is automatically a pushdown
automaton that simply ignores its stack, so
every regular language is also a context-free
language.

41
Relationship of the regular and
context-free languages

42

You might also like