Professional Documents
Culture Documents
Equivalence of Context-Free Grammars & Pushdown Automata
Equivalence of Context-Free Grammars & Pushdown Automata
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.
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
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.
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
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.
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.
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
•
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
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.
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
For example:
S → <sf1>
S → <sf2>
31
Steps of conversion (cont.)
b) Add a rule <qq> → for each state q.
For example:
<q1q1> →
<q2q2> →
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 → <ss>
<ss> →
• For rule 1 get the type c rule
<ss> → l<sXs>
• For rule 2 get
<sXs> → r<ss>
• For rule 3 get
<sXs> → l<sXs><sXs>
38
The generated grammar
Renaming <ss> = 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